Search code examples
authenticationmeteoriron-routergoogle-signin

Limit Google Sign-In to .edu accounts in Meteor


I'm trying to limit my Google + Sign-In Button to only allow @something.edu accounts to sign in. How would I go about doing this. This is my code so far:

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('/')
            }
        })
    }
})

Solution

  • You can accomplish this using Accounts.config (in the root directory, so it runs on both the client and server)

    Accounts.config({ restrictCreationByEmailDomain: 'something.edu' })
    

    If you need something more custom, you can replace something.edu with a method if you need to fine grain your requirement, i.e for any .edu domain:

    Accounts.config({ restrictCreationByEmailDomain: function(address) {
            return new RegExp('\\.edu$', 'i')).test(address)
        }
    });