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

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.
LVL 1
Black SulfurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael VasilevskySolutions ArchitectCommented:
Doesn't the lastPageOn session variable need to be set in the product detail page route?
leakim971PluritechnicianCommented:
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
Black SulfurAuthor 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?
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

Black SulfurAuthor 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

leakim971PluritechnicianCommented:
I only put it on the login page

you should put it only when you go to the login page
Black SulfurAuthor Commented:
Could you perhaps expand on that, I still don't know what you mean.
leakim971PluritechnicianCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
leakim971PluritechnicianCommented:
you can also use a parameter in your login link

<a href="/path/to/login?redirect=<currentPageURL>">Login</a>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Node.js

From novice to tech pro — start learning today.