gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 23:21:59 28 4
gpt4 key购买 nike

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

  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。这很好用。但是当他们返回 /auth/github/callback 时,我仍然需要能够识别它是哪个用户。

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

最佳答案

在/auth/github/callback 中尝试 req.user 的控制台日志。我相信 req.user 仍然会存储他们的用户信息。如 this tutorial 中所示他们通过传入req.user来传递要显示的用户信息。

关于node.js - 如何配置 passport-github 以在回调时确认用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29710671/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com