Mah*_*ran 5 oauth node.js passport.js passport-google-oauth
我一直在使用passport.js 来执行OAuth。因此,成功身份验证后会命中重定向 URI,但是对于失败情况。我无论如何都无法触发它,
在以下情况下会发生什么?
1. 当 google/twitter 或任何 OAuth 提供商打开他们的登录页面但用户根本没有响应/关闭浏览器窗口/时,我如何在超时后点击失败 URL/重定向 URL。
2. 没有任何取消按钮可以中止 Google OAuth 登录,我预计至少会调用失败。
基本上,我想知道的是,所有可能的用例/场景都会调用失败 URL 吗?
Google OAuth 重定向的示例代码,我相信所有 OAuth 提供商的失败情况都是一样的。
app.get('/oauth/failure', function (req, res, next){
console.log('Query params:', req.params.provider);
console.log('failure login called');
res.send("<script> window.close()</script>");
});
app.get('/oauth/redirect/google', passport.authenticate('google', { failureRedirect: '/oauth/failure?provider=google' }), (req, res) => {
if(req.user.id){
// gets executed for successful authentication
let redirectUrl = `http://localhost:8082/auth.html?pid=${req.user.id}`
res.redirect(redirectUrl)
}
});
Run Code Online (Sandbox Code Playgroud)
以上只是来自 的一个例子passport-google-oauth,我相信任何护照策略都是如此。
小智 0
当用户授予 OAuth 访问权限时,他自己会点击重定向 uri,据我所知,无法知道用户是否拒绝访问。如果您想实现失败重定向 URI,您可以添加 window.onbeforeunload 事件侦听器,并在用户即将退出页面时触发失败端点。