Return to Last Page after Successful Login

Suggest improvements and new features for Gargoyle.

Moderator: Moderators

Post Reply
User avatar
twf85
Posts: 20
Joined: Tue Nov 14, 2017 3:59 pm

Return to Last Page after Successful Login

Post by twf85 »

Without looking into the source code (which I plan to do this weekend), I won't assume that it would be easy to make this happen, but essentially what I would do is this:

1) Upon receiving the "invalid" response from "gargoyle_session_validator", append redirected URL (login.sh, I believe) with "?last=LASTPAGENAME.sh" (e.g. ROUTER_IP://login.sh?last=bandwidth).

<edit>
2) Add a listener to the validator to check for appended URLs.
</edit>

3) Upon successful login, redirect to the reference in the URL and remove the appended reference.

I may be able to accomplish this with injected JS, and if I do, I will post my code for those who want the same capability.

Lantis
Moderator
Posts: 6920
Joined: Mon Jan 05, 2015 5:33 am
Location: Australia

Re: Return to Last Page after Successful Login

Post by Lantis »

I’m interested in this and happy to look at implementing.
Any starting code is a good help
https://lantisproject.com/downloads/gargoylebuilds for the latest releases
Please be respectful when posting. I do this in my free time on a volunteer basis.

User avatar
twf85
Posts: 20
Joined: Tue Nov 14, 2017 3:59 pm

Re: Return to Last Page after Successful Login

Post by twf85 »

As promised, albeit a little quick and dirty. I haven't thoroughly tested this, but it seems to work pretty good in Chrome (62.0.3202.94 OB 64-bit):

JS:

Code: Select all

/*

GARGOYLE REDIRECTOR: LAST PAGE VIEWED

Decide how you want the script to behave.

You may disable the script, or choose to redirect only from within the
existing browsing session or between browsing sessions (i.e. from within
the same browser tab or even after a tab has been closed).

Set "redirectOpt" to one of the following:

0 = Disabled (default)
1 = Redirect back to the last page within existing browsing session
2 = Redirect back to the last page between browsing sessions

NOTE: No support for multiple browsing sessions open simultaneously;
however, it should function from the last page viewed in any open
browser tab.

*/

var redirectOpt = 0,
    loginPage = 'login.sh',
    defaultPage = 'overview.sh';

if (redirectOpt !== 0) {
    
    var currentPage = 
        location.href.split('/')[location.href.split('/').length-1];
    
    // Trim appended logout caller to make identifying login page easier    
    if (currentPage.indexOf('?logout=1') > -1) {
        currentPage = currentPage.split('?')[0];
    }
    
    if (isNaN(redirectOpt) === false) {
        if (browserTest(redirectOpt) === true) {
            
            if (currentPage !== loginPage && 
            currentPage !== defaultPage && 
            currentPage !== '') {
                
                setStorageVal(redirectOpt, 'lastPage', currentPage);
            }
            else {
                if (currentPage === loginPage || currentPage === '') {
                    if (checkStorageVal(redirectOpt, 'lastPage') !== false) {
                        setStorageVal(redirectOpt, 'triggerRedirect', '1');
                    }
                }
                else {
                    if (checkStorageVal(redirectOpt, 'lastPage') !== false && 
                    checkStorageVal(redirectOpt, 'triggerRedirect') !== false) {
                        if (checkStorageVal(redirectOpt, 'triggerRedirect', 'y') === '1') {
                            
                            window.stop();
                            
                            setStorageVal(redirectOpt, 'triggerRedirect', '0');
                            
                            window.location.href = 
                            location.protocol + "//" + 
                            location.hostname + 
                            (location.port > 0 ? ":" + location.port : '') + 
                            '/' + checkStorageVal(redirectOpt, 'lastPage', 'y');
                        }
                    }
                }
            }
        }
        else {
            alert('You must have a browser that supports "'+ 
            (redirectOpt === 1 ? 'sessionStorage' : 'localStorage') + 
            '" and it must be enabled to utilize this script.');
        }
    }
    else {
        alert('Variable "redirectOpt" must be set to a valid number (e.g. 0, 1, or 2).');
    }
}

function browserTest(option) {
    if (option === 1) {
        if (typeof(sessionStorage) !== 'undefined') {
            return wrapCheck(option);
        }
        else {
            return false;
        }
    }
    else {
        if (typeof(localStorage) !== 'undefined') {
            return wrapCheck(option);
        }
        else {
            return false;
        }
    }
    
    function wrapCheck(option) {
        
        setStorageVal(option, 'testStore', 'success');
        if (checkStorageVal(option, 'testStore') !== false && 
        checkStorageVal(option, 'testStore', 'y') === 'success') {
            return true;
        }
        else {
            return false;
        }
    }
}

function setStorageVal(option, key, val) {
    if (option === 1) {
        sessionStorage[key] = val;
    }
    else {
        localStorage[key] = val;
    }
}

function checkStorageVal(option, key, get) {
    if (option === 1) {
        if (typeof(sessionStorage[key]) !== 'undefined') {
            if (!get) {
                return true;
            }
            else {
                return sessionStorage[key];
            }
        }
        else {
            return false;
        }
    }
    else {
        if (typeof(localStorage[key]) !== 'undefined') {
            if (!get) {
                return true;
            }
            else {
                return localStorage[key];
            }
        }
        else {
            return false;
        }
    }
}
Injected using this Chrome extension (Custom JavaScript for websites).

***

I don't think I used any jQuery, so I didn't load any external script(s). To make things easier, I relied on sessionStorage / localStorage, but this accomplishes mostly the same thing. There is the unfortunate initial load of "overview.sh", but it doesn't take too long.

Post Reply