gpt4 book ai didi

Meteor.js 谷歌帐户 : filter email and force account choser

转载 作者:行者123 更新时间:2023-12-01 15:24:07 24 4
gpt4 key购买 nike

在我的 Meteor.js 应用程序中,我使用 accounts-google 包来连接谷歌帐户。我有两个问题。

首先,有没有一种简单的方法可以过滤使用的帐户?我希望用户只能连接属于我公司的谷歌帐户。我们的谷歌帐户邮件以@mycompany.com 结尾。所以这将是一个简单的邮件过滤。我已经使用一些登录后 Hook 完成了此操作,但我想知道是否有更简单的方法来完成此操作。

我的第二个问题是如何强制打开 google 帐户选择器。现在,如果我尝试连接一个错误的谷歌帐户,并且如果我只添加了这个帐户(比如在 gmail、驱动器等中),谷歌选择器不会弹出并自动连接到这个错误的帐户。因此,在这种情况下,用户完全被阻止(如果他尝试使用错误的帐户登录,我的应用程序会断开他的连接,但 google 帐户模块不会建议他连接到另一个帐户)。

感谢您的帮助。

最佳答案

为了限制注册/登录到您的域,只需在服务器上执行:

var checkEmailAgainstAllowed = function(email) {
var allowedDomains = ['mycompanydomain.com'];
var allowedEmails = ['otheruser@fromotherdomain.com','anotheruser@fromanotherdomain.com'];
var domain = email.replace(/.*@/,'').toLowerCase();
email = email.toLowerCase();
return _.contains(allowedEmails, email) || _.contains(allowedDomains, domain);
};

Accounts.config({
restrictCreationByEmailDomain: function(email) {
if (!email) {
throw new Meteor.Error(403,'This email address is not allowed');
}
if (!checkEmailAgainstAllowed(email)) {
throw new Meteor.Error(403,'This email domain is not allowed');
}
return true;
}
});

要登录,您需要在客户端:

Meteor.loginWithGoogle({
forceApprovalPrompt: true, //this is what you want, to rerequest approval each time that prompts the google login prompt
loginStyle : "redirect", //or not, depending on your need
requestPermissions : ['profile', 'email'],
requestOfflineToken: true
}, function (err) {
if (err)
// set a session variable to display later if there is a login error
Session.set('loginError', 'reason: ' + err.reason + ' message: ' + err.message || 'Unknown error');
});

旁注:或者,您可以设置您的路线,以便每次调用新路线时,您登录,并且每次销毁路线或在 Windows 卸载时,您调用注销。这会导致每次路由更改时登录/注销往返,但您将确保新用户始终有一个新 session

编辑:当您退出您的 meteor 应用程序时,您不会退出谷歌。这就是 oauth 的工作原理。所以,基本上,如果你想要一个 meteor log out 也让用户退出他们的谷歌账户,这样他们下次回来时,他们需要再次提供凭据,你应该这样做:

Meteor.logout(function(e) {
if (e) {
console.log("Could not log the user out")
} else {
window.location.replace('https://accounts.google.com/Logout');
}
});

这使用了 Meteor.logout() 的回调,因此当注销成功时,用户会被重定向到 google 的中央帐户注销 url,用户也会在该 url 中注销所有 google 服务。

关于Meteor.js 谷歌帐户 : filter email and force account choser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29008008/

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