Search code examples
meteormeteor-accounts

Create OAUTH accounts prior to signin


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.


Solution

  • 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