我试图限制我的Google +登录按钮只允许@ something.edu帐户登录。我将如何进行此操作。到目前为止,这是我的代码:
Template.googleLogin.events({
'click #gLogin': function(event) {
Meteor.loginWithGoogle({}, function(err){
if (err) {
throw new Meteor.Error("Google login didn't work!");
}
else {
Router.go('/home')
}
});
}
})
Template.primaryLayout.events({
'click #gLogout': function(event) {
Meteor.logout(function(err){
if (err) {
throw new Meteor.Error("Hmm looks like your logout failed. ");
}
else {
Router.go('/')
}
})
}
})
答案 0 :(得分:4)
您可以使用Accounts.config
完成此操作(在根目录中,因此它可以在客户端和服务器上运行)
Accounts.config({ restrictCreationByEmailDomain: 'something.edu' })
如果您需要更多自定义内容,则可以使用某种方法替换something.edu
,如果您需要细化您的要求,即任何.edu
域:
Accounts.config({ restrictCreationByEmailDomain: function(address) {
return new RegExp('\\.edu$', 'i')).test(address)
}
});
答案 1 :(得分:1)
帐户包允许通过以下方式配置帐户创建域:
Accounts.config({
restrictCreationByEmailDomain: 'something.edu'
})
但是在谷歌的情况下这有一些限制:
restrictCreationByEmailDomain
是否为字符串,如果它是一个函数,它只会丢弃它。因此,为了能够正确安全地使用此类功能,您需要使用官方Accounts.validateNewUser
回调:
Accounts.validateNewUser(function(newUser) {
var newUserEmail = newUser.services.google.email;
if (!newUserEmail) throw new Meteor.Error(403,'You need a valid email address to sign up.');
if (!checkEmailAgainstAllowed(newUserEmail)) throw new Meteor.Error(403,'You need an accepted organization email address to sign up.');
return true;
});
var checkEmailAgainstAllowed = function(email) {
var allowedDomains = ['something.edu'];
var allowedEmails = ['someone@example.com'];
var domain = email.replace(/.*@/,'').toLowerCase();
return _.contains(allowedEmails, email) || _.contains(allowedDomains, domain);
};
如果您需要格外谨慎,也可以对Accounts.validateLoginAttempt
和Accounts.onCreateUser
回调实施相同的操作。