如何配置 passport-github 以在回调时确认用户?

How can I configure passport-github to acknowledge the user on callback?

我有一项服务,我的目标是让用户通过 OAuth 向我们授权他/她的 Github 帐户。 API 层是无状态的,所以我不想维护任何会话信息。到目前为止我的代码是:

  app.use passport.initialize()
  app.use passport.session()

  passport.use new GitHubStrategy
    clientID: global.config.oauth.github.clientID
    clientSecret: global.config.oauth.github.clientSecret
    callbackURL: "http://localhost:9500/auth/github/callback"
    passReqToCallback: true
  , (req, accessToken, refreshToken, profile, done) ->
    console.log req.params
    serviceQuery =
      service: 'github'
      provider_id: "#{profile.id}"
      UserId: 13

    global.db.Service.find
      where: serviceQuery
    .then (dbService) ->
      if not dbService
        newService =
          service: 'github'
          token: accessToken
          secret: null
          provider_id: profile.id
          raw: profile._json
          UserId: 13
        global.db.Service.create newService
      else
        dbService.token = accessToken
        dbService.raw = profile._json
        dbService.save()
    .then ->
      done null, true


  app.get '/auth/github/:sessionToken', (req, res, next) ->
    console.log req.params
    next()
  , passport.authenticate 'github',
    scope: 'repo'
    session: false

  app.get '/auth/github/callback', passport.authenticate('github',
    scope: 'repo'
    session: false
  ), (req, res) ->
    res.redirect 'http://localhost:8080/app'

所以当用户被定向到 /auth/github/:sessionToken 时,我希望 passport 发挥它的魔力并将用户重定向到 github。这很好用。但是当他们 return 到 /auth/github/callback 时,我仍然需要能够识别它是哪个用户。

因为这是 API 服务,它应该是无状态的,所以正如我之前提到的,我正在寻找某种方式将该令牌传递给 /auth/github/callback。我怎样才能做到这一点?

在您的 /auth/github/callback 中尝试 req.user 的控制台日志。我相信 req.user 仍会存储他们的用户信息。正如在 this tutorial 中显示的那样,他们通过传入 req.user.

来传递要显示的用户信息