1

I have an app I am working on that has one or two "main" accounts linked to other accounts. I am using accounts-password, accounts-google, and alanning:roles currently, but I may add other OAuth providers later.

The idea is that there will be one or two users that are "admins" for a larger group, and as such I'd like to be able to allow them to add users. I don't mind if I have to go through the OAuth authorization page on first login, but I would like to be able to add the users to the Meteor.users collection and allow the admins to set things up prior to their logging in, but I am not sure how to do it at all. Can I simply add a user to the Meteor.users collection with only the email attribute of the google sub-document populated? I would guess that the email is NOT what Meteor uses to connect a stored Meteor.users document to the corresponding Google OAuth account as there is also an id attribute that seems like it might be more useful for that purpose.

3
  • You can do a Meteor.method, or even on a Meteor.call use something like this. var currentUser = Meteor.user() isUserSuperAdmin = Roles.userIsInRole(currentUser,'Super-Admin'); // bassed on this you can create the if isSuperAdmin == true let them insert the new user With this you will have the control to create the users, you can create a user only with the email field, but for example if you are using 'accounts-ui package` you should change the accounts-ui config Accounts.ui.config({ passwordSignupFields: 'USERNAME_ONLY' }); i don't know if you are looking for something like tha
    – Ethaan
    Apr 8, 2015 at 15:53
  • I'm not sure I understand what you are saying. So, in my application I need to allow people to create a group of users. Users that may not have logged in yet. So, what I'd like to do is allow them to add the user prior to initial login. This process will add some stuff to the profile of the user so the app can associate all the users together. But in order to do that, I'd have to be able to great enough of a document in the db.users collection to allow Meteor to fill in the rest when they first login without erasing the stuff I setup for them prior to them signing in.
    – CodeChimp
    Apr 9, 2015 at 17:28
  • i think i got you idea, if not please tell me
    – Ethaan
    Apr 10, 2015 at 6:38

1 Answer 1

0

So based on what you say on the comment i elaborate this DEMO, since you want to create users and add that users to some kind of "role" or "group" im using here the meteor-roles package.

allow people to create a group of users

This could be done on some differentes ways, for the demo purpose im creating 1x1 user and leater assign them a Role.

to avoid the autologin behavior you should use a Meteor.method.

//server
Meteor.methods({
   createSimpleUser:function(email){
      return  Accounts.createUser({
               email:email,
               password:"test123" //you can force the user in the first login to change the password.
            })
     }
  })
//Client
Meteor.call('createUser',"[email protected]")

Now that you have the user created, you should assign them a role.

So on some event handler you want,do something like this.

   //this should be incide an {{#each}} or {{#with}} in order to this._id works, if not use Sessions
   Meteor.users.update({_id:this._id},{$set:{group:newGroup}})

This is just the global idea, you should protect in the allow rules, who can edit the users group, you can also use template helpers like

{{if userIsInRole 'nameGroup'}}
  <!-- Show content only available to users in this group -->
{{else}}
  <!-- Some warning access denied template -->
{{/if}}

This process will add some stuff to the profile of the user so the app can associate all the users together

Filter by groups.

Template.example.helpers({
 groupX:function(){
   return Meteor.users.find({group:"X"})
  }
})

when they first login without erasing the stuff I setup for them prior to them signing in.

Here again there could be many reasons, for example you can add some field inside the current user created.

Meteor.methods({
       createSimpleUser:function(email){
          return  Accounts.createUser({
                   email:email,
                   password:"test123",
                   firstLogin:false //by default the users login is false.
                })
         }
      })

and in the iron route, create a function(why on the router? see Overworked helpers on the David Weldon webpage).

   requireEdit = function(){
      var query = Meteor.users.findOne({_id:this.params._id})
      if(query.firstLogin == false){
        this.render('profileEditin') //do some change password here and other stuff
      }else{
        this.next()'
     }
  }

And call it on the onBeforeAction() method

This is just the idea and demo is a ugly-fast example of how it should work, good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.