Passport.authenticate Google OAuth 2.0 识别后的成功重定向条件

Passport.authenticate successRedirect condition after Google OAuth 2.0 identification

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

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

我想将一般用户重定向到“/dashboard/”,但管理员(使用 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';
            }
    }
}));

但我不知道如何在失败后插入 (req, res)Redirect: '/', line

还需要"return"吗?

有什么帮助吗?

为什么不使用包 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)
);