Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: How to assign different roles to users during sign up process

I am using the meteor package ian:accounts-ui-bootstrap-3 for accounts and alanning:roles for assigning roles.

On the sign up form I have two options one for Doctor and one for Advisor. I want to assign the selected option as a role to that user. Can someone let me know how to do this?

I have just started learning meteor and don't know much about its flow. I can assign roles to a user if I create the user manually like this:

var adminUser = Meteor.users.findOne({roles:{$in:["admin"]}});

if(!adminUser){

    adminUser = Accounts.createUser({
      email: "[email protected]",
      password: "admin",
      profile: { name: "admin" }
    });
    Roles.addUsersToRoles(adminUser, [ROLES.Admin]);
 }

But I want to assign a roll automatically as a user signs up and select one of the options and that option should be assigned as his role.

like image 251
Mohsin Rafi Avatar asked May 27 '15 12:05

Mohsin Rafi


People also ask

What is the meteor user () function for?

userId() import { Meteor } from 'meteor/meteor' (accounts-base/accounts_common.js, line 410) Get the current user id, or null if no user is logged in.

Which of the following user accounts packages are provided by the meteor developer group?

Here's a complete list of login providers for which Meteor actively maintains core packages: Facebook with accounts-facebook. Google with accounts-google. GitHub with accounts-github.


2 Answers

You shouldn't need a hack for this. Instead of using Accounts.onCreateUser you can do it with the following hook on the Meteor.users collection. Something along the lines of the following should work:

Meteor.users.after.insert(function (userId, doc) {
    if (doc.profile.type === "doctor") {
        Roles.addUsersToRoles(doc._id, [ROLES.Doctor])
    } else if (doc.profile.type === "advisor") {
        Roles.addUsersToRoles(doc._id, [ROLES.Advisor])
    }
});
like image 182
apeman Avatar answered Nov 05 '22 19:11

apeman


To get around having to check on login every time it's possible to directly set the roles on the user object instead of using the Roles API.

A hack? Yep, you probably need to make sure the roles have already been added to roles... not sure if there's anything else yet.

if(Meteor.isServer){
  Accounts.onCreateUser(function(options, user){
    if(options.roles){
      _.set(user, 'roles.__global_roles__', ['coach', options.roles]);
    }
    return user;
  });
}

Note: _.set is a lodash method not in underscorejs.

There's no pretty solution because:

  1. There's no server side meteor callback post complete account creation.
    • In onCreateUser the user hasn't been added to the collection.
    • Accounts.createUser's callback is currently only for use on the client. A method could then be used from that callback but it would be insecure to rely on it.
  2. The roles package seems to grab the user from the collection and in onCreateUser it's not there yet.
like image 25
Shwaydogg Avatar answered Nov 05 '22 20:11

Shwaydogg