Passport.authenticate successGoogle OAuth 2.0识别后的重定向条件

时间:2019-05-07 19:48:35

标签: node.js passport.js

我在Google OAuth 2.0识别后使用此回调路由

// Original version working:
// Callback route
router.get( '/google/callback', 
    passport.authenticate( 'google', { 
        failureRedirect: '/', 
        successRedirect: '/dashboard',
}));

我想将普通用户重定向到'/ dashboard /,但是将admin(使用类似admin@admin.com的电子邮件)重定向到'/ admin'

我正在尝试这样的事情:

// Callback route
router.get( '/google/callback', 
    passport.authenticate( 'google', { 
        failureRedirect: '/', 
          {
            if (req.user.mail === 'admin@admin.com') {
                return successRedirect: '/admin';
            } else 
                {
                return successRedirect: '/dashboard';
            }
    }
}));

但是我不知道如何在failRedirect后插入(req,res):'/',行

还需要“返回”吗?

有帮助吗?

1 个答案:

答案 0 :(得分:1)

为什么不使用软件包here所规定的自定义回调。

实施

router.get('/google/callback', (req, res, next) =>
  passport.authenticate('google', (err, user, info) => {
    if (err) return next(err);

    if  (!user) return res.redirect('/login');

    req.logIn(user, err => {
      if (err) return next(err);

      if (user.mail === 'admin@admin.com') return res.redirect('/admin');

      return res.redirect('/dashboard');
    });
  })(req, res, next)
);