【问题标题】:How to diagnose passport.authenticate?如何诊断passport.authenticate?
【发布时间】:2018-10-21 19:06:39
【问题描述】:

由于函数没有返回值 - 我如何确定导致函数失败的原因。我有以下代码:

function test(req : IncomingMessage, res :ServerResponse, next:(err:any)=>void) {

  passport.authenticate('google', {scope:['https://www.googleapis.com/auth/userinfo.email']})(req, res,next);

  //how do I get details here
}

我的意思是我知道它失败了,因为下一个处理程序没有被调用,但是我如何获得更详细的信息?

这个函数的实例如下:

  export const usersRouter = express
  .Router()

.post('/googleLogin', test, async (req, res) => {
    const user: User = req.user;
    if (!user) {
      res.send(401);
    }

    // const token = getJwtToken(user._id.toHexString());
    // res.send({ token });
  })

【问题讨论】:

    标签: typescript oauth-2.0 passport.js


    【解决方案1】:

    这样的身份验证工作流程(oauth / openid)涉及多个步骤。

    1. 将用户重定向到 google 的身份验证服务

    2. google 对用户进行身份验证并要求用户确认以接受/拒绝您的服务

    3. 如果用户接受,Google 会使用访问代码将用户重定向回您服务的回调 uri。

    4. 您的服务器(不是用户的浏览器)使用您的应用程序的 ID 和密码(在注册应用程序时由 google 发出)以及之前收到的访问代码向 google 的服务器请求访问/刷新令牌。

    5. 如果一切顺利,谷歌将向您的服务发出访问/刷新令牌,您必须将其关联到您身边的用户(使用此 google id 的现有用户,或创建一个新用户作为注册,或与已登录的用户关联,如连接帐户)。

    为了检查发生了什么,您可以检查您身边的每个步骤:

    这只会将您的用户重定向到谷歌的身份验证服务

    app.get('/auth/google',
          passport.authenticate('google', {
            scope: ['https://www.googleapis.com/auth/userinfo.email']
          });
    

    您将在用户界面中使用此端点,例如:

    <a href="/auth/google">Connect with google</a>
    

    一旦 google 对您的用户进行身份验证并将您的用户重定向回您的服务,这就是用户将被重定向到的端点:

    app.get('/auth/google/callback',
      (req, res, next) => {
        // You can inspect google's redirection here
        return next();
      }, 
      passport.authenticate('google', { failureRedirect: '/login' }),
      function(req, res) {
        res.redirect('/');
      });
    

    此时,passport 将进行身份验证流程以向 google 请求访问/刷新令牌。 当您的服务器向 google 请求访问/刷新令牌时,将调用您的策略定义的回调:

    passport.use(new GoogleStrategy({
        consumerKey: GOOGLE_CONSUMER_KEY,
        consumerSecret: GOOGLE_CONSUMER_SECRET,
        callbackURL: "http://www.example.com/auth/google/callback"
      },
      function(token, tokenSecret, profile, done) {
      // you can debug google's response here (this is from your server's request)
          User.findOrCreate({ googleId: profile.id }, function (err, user) {
            return done(err, user);
          });
      }
    ));
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      也许带有自定义回调。 (From the documentation.)

      在此示例中,请注意从路由处理程序中调用 authenticate(),而不是用作路由中间件。这使回调通过闭包访问 req 和 res 对象。

      app.get('/googleLogin', function(req, res, next) {
        passport.authenticate('google', function(err, user, info) {
          if (err) { return next(err); }
          if (!user) { return res.redirect('/login'); }
          req.logIn(user, function(err) {
            if (err) { return next(err); }
            return res.redirect('/users/' + user.username);
          });
        })(req, res, next);
      });
      

      【讨论】:

      • 已尝试添加回调,但仍未实例化。
      猜你喜欢
      • 2011-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多