How to create a default user on meteor?

前端 未结 4 764
感动是毒
感动是毒 2021-02-19 03:39

I want to create an admin user if no users exist. I tried it on a js file inside the server folder

Meteor.startup(function () {
  if(!Meteor.users.find().count()         


        
相关标签:
4条回答
  • 2021-02-19 04:09

    this way works for me:

    var users=[
       {email: "dgra@gmail.com", username: "gra", name: "gra", roles:['admin']}
    ];
    _.each(users, function(user){
        Accounts.createUser({
            email: user.email,
            password: "admin",
            profile: {username: user.username},
            profile: {name: user.name},
            roles: user.roles
        });
    });
    
    0 讨论(0)
  • 2021-02-19 04:13

    After some time, I tested again the above code with meteor 0.6.4 and it worked without problem. Probably was an issue with meteor 0.6.1 or I accidentally solved the problem somewhere in the code.

    0 讨论(0)
  • 2021-02-19 04:15

    I've never found any problem on creating a user on Meteor. This is my coffeescript code:

    Meteor.startup ->
      if Meteor.users.find.count() is 0
        options =
          email: 'email@example.com'
          password: 'pass'
        Accounts.createUser(options)
    
    0 讨论(0)
  • 2021-02-19 04:19

    I would suggest a /server/fixtures.js file. In this file, you can add your default user creation as such:

    if ( Meteor.users.find().count() === 0 ) {
        Accounts.createUser({
            username: 'username',
            email: 'email',
            password: 'asdfasdf',
            profile: {
                first_name: 'fname',
                last_name: 'lname',
                company: 'company',
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题