You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been trying to get passport to authenticate a user via Twitter, but return them to the protected resource that triggered the authentication request in the first place. I'm failing to make this work, sadly. It would be very useful if some of the documentation would make it clear how to achieve this. I'll even volunteer to update the documentation if someone could tell me how this is done.
Thanks.
The text was updated successfully, but these errors were encountered:
app.get('/auth/twitter/callback',function(req,res,next){passport.authenticate('twitter',function(err,user,info){varredirectUrl='/account';// This is the default destination upon successful login.if(err){returnnext(err);}if(!user){returnres.redirect('/');}// If we have previously stored a redirectUrl, use that, otherwise, use the default.if(req.session.redirectUrl){redirectUrl=req.session.redirectUrl;req.session.redirectUrl=null;}req.logIn(user,function(err){if(err){returnnext(err);}});res.redirect(redirectUrl);})(req,res,next);});
Now, define your middleware for authenticated routes to store the original URL in the session like this:
ensureAuthenticated=function(req,res,next){if(req.isAuthenticated()){returnnext();}// If the user is not authenticated, then we will start the authentication// process. Before we do, let's store this originally requested URL in the// session so we know where to return the user later.req.session.redirectUrl=req.url;// Resume normal authentication...logger.info('User is not authenticated.');req.flash("warn","You must be logged-in to do that.");res.redirect('/');}
Greetings,
I've been trying to get passport to authenticate a user via Twitter, but return them to the protected resource that triggered the authentication request in the first place. I'm failing to make this work, sadly. It would be very useful if some of the documentation would make it clear how to achieve this. I'll even volunteer to update the documentation if someone could tell me how this is done.
Thanks.
The text was updated successfully, but these errors were encountered: