gpt4 book ai didi

node.js - 如何处理 PassportJS 端点错误

转载 作者:搜寻专家 更新时间:2023-10-31 23:25:24 25 4
gpt4 key购买 nike

如果我访问位于 http://localhost/login?code=*** 的 Facebook 回调链接

使用伪造代码:FacebookTokenError:验证码格式无效。
使用相同的代码(重放攻击):FacebookTokenError: This authorization code has been used.
代码过期:FacebookTokenError: This authorization code has expired.

所有这些错误都与客户端无关。我想简单地重试登录过程(再次重定向到 Facebook 以进行身份​​验证)。

Express().get('/login', Passport().authenticate('facebook', {
failureRedirect: '/',
}), function(req, res) {

});

但是,如果出现上述三种错误,服务器只会抛出一个错误并发送给客户端。

是否有端点错误的错误回调可用?

最佳答案

这是我正在使用的:

// Google OAUTH sendoff
app.get('/auth/google',
passport.authenticate('google')
);

// Google OAUTH return
app.get('/auth/google/return', function(req, res, next) {
passport.authenticate('google', function(err, user, email) {
if (err) { return next(err); }

// OAUTH success, but user isn't authorized
if (!user && email) {
return res.redirect('/myNotAuthorizedUrl');
// OAUTH error
} else if (!user) {
return res.redirect('/login/');
// user disabled or some other check
} else if (user.get('is_blacklisted') !== 1) {
return res.redirect('/youAreEvil');
}
// success and authorized
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.redirect('/home');
});

})(req, res, next);
});

关于node.js - 如何处理 PassportJS 端点错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20647511/

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