Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit Meteor.js built-in Google authentication to a domain

I'd like to use the Meteor.loginWithGoogle() tool to authenticate users, but is there any way to limit it to a specific (Google Apps) domain?

I could check after the user is authenticated using the returned email, but is there a way to do this at the login stage with some parameter for Google login?

like image 944
kennysong Avatar asked Aug 27 '13 02:08

kennysong


2 Answers

I dont think its possible right now. There is a pull resquest to partly add that functionality: https://github.com/meteor/meteor/pull/1332 The issue with that pull request seems to be that it only fixes the client side of thinges (ie. it only shows accounts from the selected domain when the user logs in). But it does not add any server side checks.

Im using the following workaround: In a .js file in the sever folder I have the following code:

Accounts.validateNewUser(function (user) {
    if(user.services.google.email.match(/example\.org$/)) {
        return true;
    }
    throw new Meteor.Error(403, "You must sign in using a example.org account");
});

This prevents accounts from being made for domains different from example.org.

like image 88
ErikMejerHansen Avatar answered Oct 23 '22 09:10

ErikMejerHansen


If you want to only allow certain users from your domain, you could also add a whitelist collection that defines user ids from your Google Apps account. This way you can restrict access to only certain users, get single sign-on functionality, and can pre-set user roles and properties for your app before users even create their accounts.

Use the Accounts.onCreateUser(function(options, user){}) callback for that since it allows you to define additional user properties.

like image 45
Ed Myers Avatar answered Oct 23 '22 10:10

Ed Myers