connect-ensure-login icon indicating copy to clipboard operation
connect-ensure-login copied to clipboard

Do not manage to be automatically redirected to original URL

Open gridpocketuser opened this issue 10 years ago • 6 comments

I'm using the following middleware for the oauth/authorize route:

app.get('/oauth/authorize',
  login.ensureLoggedIn(),
  server.authorize(function(clientId, redirectURI, done) {

  Client.findOne({clientId: clientId}, function(err, client) {
    if (err) { return done(err); }
    if (!client) { return done(null, false); }
    if (client.redirectURI != redirectURI) { return done(null, false); }
    return done(null, client, client.redirectURI);
  });
}),
server.errorHandler(),
function(req, res) {
  res.render('dialog', { transactionID: req.oauth2.transactionID,
                         user: req.user,
                         client: req.oauth2.client
  });
}
);

The connection URL is the following one:

http://localhost:1337/oauth/authorize?client_id=O3UTGRFNI1&response_type=code&redirect_uri=http://localhost:1339&scope=http://localhost:1337

When the user is not logged, he is redirected towards the login page but when he logs he is redirected towards the index page (instead of being redirected towards the above URL). Any idea why this is happening ? I've certainly missed something but I cannot figure out what (probably linked to returnTo).

I've also set up a complete example in the following question: http://stackoverflow.com/questions/25833533/secure-nodejs-app-with-oauth2-not-automatically-redirected-to-allow-deny-page

gridpocketuser avatar Sep 14 '14 13:09 gridpocketuser

Did you ever resolve this issue? I'm having the same problem.

nodeit avatar May 20 '15 15:05 nodeit

For anyone else running into this issue, it was indeed something very simple and is clearly in the docs:

app.post('/login', passport.authenticate('local', { successReturnToOrRedirect: '/', failureRedirect: '/login' }));

You need to give your passport route the successReturnToOrRedirect parameter so that it knows what to do after you login.

nodeit avatar May 20 '15 19:05 nodeit

Thanks a lot, I didn't got this.

gridpocketuser avatar May 20 '15 19:05 gridpocketuser

@nodeit thanks

aegyed91 avatar Aug 31 '15 18:08 aegyed91

@nodeit thanks, that answer is gold.

Although the option is documented, it's not intuitive at all. successReturnToOrRedirect: '/' might indicate a redirect to root rather than the original url.

mihaifm avatar Mar 16 '20 19:03 mihaifm

I did this and noticed the user is logged in only after the third attempt... I removed the package and it worked well... How do i get around this? github.com/miracle0403/newsitemlm.git

miracle0403 avatar Apr 12 '21 13:04 miracle0403