how to redirect to original page after successful authentication using passport-google-oauth how to redirect to original page after successful authentication using passport-google-oauth express express

how to redirect to original page after successful authentication using passport-google-oauth


Can also store the first URL in the session, only if the variable is currently not set. Then post login if this var is set, then re direct to that.

This is better than a header which might not be accurate from a 3rd party oauth.

In Java I would do it in a filter (check for a session variable to, say redirect_to or first_url, if its null then set it, if not the login/ sign up page). In node js can use middleware as explained in https://stackoverflow.com/a/13336055/1643558

Quoting that answer:I have a middleware I use with app.get('/account', auth.restrict, routes.account) that sets redirect_to in the session...then I redirect to /login

auth.restrict = function(req, res, next){    if ( !req.session.userid ) {        req.session.redirect_to = '/account';            res.redirect('/login');    } else {        next();    }};

Then in routes.login.post I do the following:

var redirect_to = req.session.redirect_to ? req.session.redirect_to : '/';delete req.session.redirect_to;//is authenticated ?res.redirect(redirect_to);


I use

res.redirect(req.headers.referer); 

but problem is sometimes it works sometimes it doesn't as if the user will be first time logging through facebook, facebook will first display the permission dialog which overwrites the referer header var but if user has already granted permission to your app he/she will be redrected back to the url from wherever they are coming.

SBE - Sorry for Bad English ;)