我正在我的 Node js 机器人中进行 Azure 广告身份验证。 在最后一步中,它显示了带有魔术代码的弹出窗口。 我想关闭 5 秒后自动弹出的魔法窗口。
这是代码
res.send('Welcome ' + req.user.displayName + '! Please copy this number and paste it back to your chat so your authentication can complete: ' + magicCode);
这一行发送一个带有魔术代码的窗口弹出。我想在 5 秒后自动关闭此窗口。 我的代码
server.get('/api/OAuthCallback/',
passport.authenticate('azuread-openidconnect', { failureRedirect: '/login' }),
(req, res) => {
console.log('OAuthCallback');
console.log(req);
const address = JSON.parse(req.query.state);
const magicCode = crypto.randomBytes(4).toString('hex');
const messageData = { magicCode: magicCode, accessToken: req.user.accessToken, refreshToken: req.user.refreshToken, userId: address.user.id, name: req.user.displayName, email: req.user.preferred_username };
magicNum = magicCode;
var continueMsg = new builder.Message().address(address).text(JSON.stringify(messageData));
console.log(continueMsg.toMessage());
test_name = JSON.parse(req.user._raw).preferred_username.split("@")[0]
bot.receive(continueMsg.toMessage());
res.send('Sign-in successful');
}
);
提前致谢
最佳答案
看起来您正在使用 botauth
lib,如果是这样,应该有一个 html 页面需要用来显示 magic code
。
此 html 应包含类似的 js 脚本代码,例如:
document.getElementById("magic_code").innerText = (window.location.hash || '').replace('#', '');
您可以添加以下代码以在 5 秒内关闭窗口
setTimeout(() => {
window.close();
}, 5000);
类似的代码示例位于 https://github.com/CatalystCode/node-authbot 。所有这些都使用 OAuth 2.0 流程,一旦用户成功登录身份验证提供程序,它将重定向到您在服务器中配置的返回 URL。
在您的代码中,您的身份验证返回路由只是在浏览器中发送一个字符串。此外,您可以向用户呈现 HTML 页面,但是您将面临问题
Scripts may close only the windows that were opened by it.
与您在评论中提供的问题相同。
不幸的是,SO 上有几个问题: window.close() doesn't work - Scripts may close only the windows that were opened by it window.close and self.close do not close the window in Chrome ,目前还没有好的解决方案。
关于node.js - 关闭node js登录后弹出的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50370932/