gpt4 book ai didi

node.js - NodeJS Passport - Facebook 身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 23:35:24 24 4
gpt4 key购买 nike

我创建了一个 Facebook 应用程序,并且只有我一个人使用。现在我已经上线了,但除了我之外没有人能够登录。

他们收到以下错误。

FacebookTokenError: This authorization code has been used.
at Strategy.parseErrorResponse (/var/www/html/project/node_modules/passport-facebook/lib/strategy.js:199:12)
at Strategy.OAuth2Strategy._createOAuthError (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/lib/strategy.js:345:16)
at /var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/lib/strategy.js:171:43
at /var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:177:18
at passBackControl (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:123:9)
at IncomingMessage.<anonymous> (/var/www/html/project/node_modules/passport-facebook/node_modules/passport-oauth2/node_modules/oauth/lib/oauth2.js:143:7)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)

这里可能有什么问题。应用程序已上线。

最佳答案

我在 Express 中有一个 REST api,它使用来自 Jeroen Pelgrims 的信息进行无 session Facebook 登录。它的作用是:

使用 Facebook 策略登录将 token 和信息保存在用户数据库中使用该 token 进行承载登录Passport-facebook 验证在回调页面中使用的是这样的:Passport.authenticate('策略', 选项, 用户, 错误)

该行为正确地引发了该错误!正如 @jsilveira 在评论中所说,如果用户登录两次就会发生错误......

我的答案很简单,我发现了错误...继续阅读,有一个但是...

//Facebook 身份验证和登录路由

 router.get('/auth/facebook',
passport.authenticate('facebook', {session: false, scope : ['email'] })
);

// handle the callback after facebook has authenticated the user
router.get('/auth/facebook/callback',
passport.authenticate('facebook', { session: false, failureRedirect : '/'}),

// on succes
function(req,res) {
// return the token or you would wish otherwise give eg. a succes message
res.render('json', {data: JSON.stringify(req.user.access_token)});
},

// on error; likely to be something FacebookTokenError token invalid or already used token,
// these errors occur when the user logs in twice with the same token
function(err,req,res,next) {
// You could put your own behavior in here, fx: you could force auth again...
// res.redirect('/auth/facebook/');
if(err) {
res.status(400);
res.render('error', {message: err.message});
}
}
);

但是,如果你们希望它再次登录/授权,您可以在错误部分插入回调或重定向。

希望这能澄清你们遇到的一些问题。如果您有任何疑问或要求,请不要害羞。信息在我的个人资料中。

PS:stackoverflow.com 有一个重新验证的答案,如下所示:

app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err); // will generate a 500 error
}
// Generate a JSON response reflecting authentication status
if (! user) {
return res.send({ success : false, message : 'authentication failed' });
}
return res.send({ success : true, message : 'authentication succeeded' });
})(req, res, next);
});

关于node.js - NodeJS Passport - Facebook 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33241883/

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