redirect to last page user was on after login node.js and express (no passport)

Black Sulfur
Black Sulfur used Ask the Experts™
on
I know this question is asked a lot so please don't downvote or mark as duplicate. I have looked on google and on stack overflow but most answers talk about using passport which I am not using and a lot of the questions I looked at are for php.

I have a login button on a product detail page. If a user clicks on it they are redirected to the login page and after login I want to send them back to the product detail page.

In my starting point (app.js) I am setting a session variable for what I think is the referrer. (Should be like php $_SERVER['REQUEST_URI')

app.use((req, res, next) => {
    req.session.lastPageOn = req.get('Referrer');
    next();
});

Open in new window


In my login script I have:

if (req.session.lastPageOn) {
    res.redirect(req.session.lastPageOn)
} else {
    res.redirect('account/dashboard');
}

Open in new window


But every time I login in I get redirected to the login page as that was the last page I was on before logging in! So, it doesn't seem like req.get('Referrer'); is the right thing to use or my implementation is incorrect.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Michael VasilevskySolutions Architect

Commented:
Doesn't the lastPageOn session variable need to be set in the product detail page route?
leakim971Multitechnician
Top Expert 2014

Commented:
you run the following EVERYTIME (app.js) :
app.use((req, res, next) => {
    req.session.lastPageOn = req.get('Referrer');
    next();
});

Open in new window

mean req.session.lastPageOn always NOT NULL

you should put it only when you go to the login page

Author

Commented:
@Michael, I don't want it for only the product detail page, that is why I put it in app.js. If the user was on any other page, it should be remembered and the user should go back to that page after login. They could have been doing a search, and be 4 pages into pagination and after login they should go back there.

@leakim971, could you maybe explain that a bit more? If I only put it on the login page how will it know the previous page I visited?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I am probably going to have to put a condition in, something like:

 app.use((req, res, next) => {
     if (req.originalUrl !== '/login') {
         req.session.lastPageOn = req.protocol + '://' + req.get('host') + req.originalUrl;
     }
     next();
 });

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
I only put it on the login page

you should put it only when you go to the login page

Author

Commented:
Could you perhaps expand on that, I still don't know what you mean.
Multitechnician
Top Expert 2014
Commented:
your link should be something like :
<a href="/path/to/ImGoingToLoginPage">Login</a>
and not
<a href="/path/tologin">Login</a>

your page "ImGoingToLoginPage" have this :
app.use((req, res) => {
    req.session.lastPageOn = req.get('Referrer');
    res.redirect('account/login');
});

after every login you should clear req.session.lastPageOn
leakim971Multitechnician
Top Expert 2014

Commented:
you can also use a parameter in your login link

<a href="/path/to/login?redirect=<currentPageURL>">Login</a>

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial