Redirecting to previous page after authentication in node.js using passport.js

前端 未结 7 1218
悲&欢浪女
悲&欢浪女 2020-11-30 17:13

I\'m trying to establish a login mechanism using node.js, express and passport.js. The Login itself works quite nice, also sessions are stored nicely with redis but I do hav

7条回答
  •  不知归路
    2020-11-30 17:52

    If you are using connect-ensure-login there is a super-easy, integrated way to do this with Passport using the successReturnToOrRedirect parameter. When used, passport will send you back to the originally requested URL or fallback to the URL you provide.

    router.post('/login', passport.authenticate('local', {
      successReturnToOrRedirect: '/user/me',
      failureRedirect: '/user/login',
      failureFlash: true
    }));
    

    https://github.com/jaredhanson/connect-ensure-login#log-in-and-return-to

提交回复
热议问题