gpt4 book ai didi

node.js - 从passportjs中的OAuth回调中获取状态参数

转载 作者:行者123 更新时间:2023-12-04 14:18:48 24 4
gpt4 key购买 nike

我正在尝试将状态参数发送到 Oauth,然后在回调中捕获它们,但我无法使其工作。那么passportjs支持这样的功能吗?

我的想法是将 id 作为状态参数发送到 Oauth,然后根据发送回我的应用程序的状态参数中的 id 进行回调,我想做一个正确的重定向。

在我启用的 Twitter 策略中

passReqToCallback: true,
state: true

我的请求应该是这样的:
app.get('/auth/twitter/:gameId/:url',
function(req, res){
try {
var json = JSON.stringify({gameId: req.params.gameId, url: req.params.url});
var encodedValues = base64url(json);
console.log('encodedValues:'+encodedValues)
passport.authenticate('twitter', {state:encodedValues})
}catch (e){
console.log(e);
}
}
);

然后在回调
app.get('/auth/twitter/callback', passport.authenticate('twitter', function(req, res, next) {
try{
//get the state params from the res uri/body
//decode the state params
//redirect depending on the state.gameId
}catch(e){
console.log('twitter exception:'+e);
}}));

我已经知道我可以在 session 中保存 id,但我想知道是否有 session 较少的方法通过从 url 传递此信息来做到这一点,因为它不敏感。

谢谢

最佳答案

使用 oAuth 2.0,您不必为此依赖任何类型的 session 。您在 /auth/twitter/:gameId/:url 上通过状态的方式路线,您将能够使用 req.query.state 访问回调中的状态.
万一其他人遇到问题,当尝试通过 oAuth 进程传递应用程序状态时,这就是我在我的 Node-app 中解决它的方法:
由于我想将用户重定向到不同的位置,根据用户在进入身份验证屏幕之前来自的位置,我构建了不同的路由来访问身份验证屏幕,并根据命中的路由传递不同的状态。像这样:

router.get('/google/io', passport.authenticate('google', {
scope: ['email', 'profile'],
state: 'io'
}));

router.get('/google/de', passport.authenticate('google', {
scope: ['email', 'profile'],
state: 'de'
}));
因此,如果我的用户使用 .io 域访问来自我的页面的身份验证屏幕,则我的状态参数将携带值“io”,如果用户访问来自我页面的 .de 版本的身份验证屏幕,则状态参数将携带“de”的值。成功验证后,我可以从 req.query.state 中提取值,并将它们重定向回 mypage.demypage.io

关于node.js - 从passportjs中的OAuth回调中获取状态参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29534351/

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