I'm using the social account services to provide signup/login for my app with various social networks. Is it possible to assign users who use social login to roles and/or groups? I've looked at the accounts-role package and elsewhere but I can't figure out where to add the hooks, where to define roles, etc.
1 Answers
The Meteor.users collection is just like any other collection and the only thing that the social account services do is add a user to that collection and add a facebook/twitter/github object to your user with some social network specific information. You can edit it just like a normal accounts-password user account and add any extra data to it like roles. You could add a default role using the Accounts.onCreateUser method and then adjust roles afterwards like so:
Accounts.onCreateUser(function(options, user) {
user.role = "standard"
return user;
});
By default the user object is restricted to just the profile object, so if you wanted to use the new role data you've added to your user object, you'd need to do a normal publication/subscription of it. Or if you didn't want to do that you could just add the role to the profile object:
Accounts.onCreateUser(function(options, user) {
user.profile.role = "standard"
return user;
});
And then make sure you don't allow it to be edited by a client side call using allow/deny rules. I'm guessing you don't want people to want to change their roles!
- 358
- 1
- 9
-
thx for the details Jeff. Where I'm lost is where to add the onCreateUser method. it seems like every meteor example i can find is older and everyone uses a different dir structure. Does that method belong on Meteor.startup? Apologies for such noob questions but I'm just starting out. – RyGuy Mar 18 '15 at 16:43
-
Hey Ry, you can put it anywhere on the server, I myself have created an `accounts.js` file on the server and put it in there. It doesn't need to be on `Meteor.startup` because that runs on startup, this function is called when a user is created. Here is the details in the docs: http://docs.meteor.com/#/full/accounts_oncreateuser Just make sure you put it in the server folder in your project. – Jeff Lau Mar 19 '15 at 04:07
-
If you are using the [meteor-roles](https://github.com/alanning/meteor-roles) then have a look at http://stackoverflow.com/questions/22649600/unable-to-add-roles-to-user-with-meteor-using-roles-package, including the two not selected answers. – Dan Jun 19 '15 at 20:26
-
While this works if you want every user to have the same role, it doesn't work if you want users to have different roles. There does not appear to be a way to pass information through the `loginWithSocialService` call to dynamically determine user roles – Merlin -they-them- Jun 29 '17 at 16:09