2016-12-05 58 views
0

我有一个奇怪的问题, 我创建了一个表单,它将新用户添加到数据库,它工作正常。流星 - 添加新用户,更改登录用户

现在的问题是,如果我使用“Alex”帐户登录,并且向数据库添加了“Leonard”,Leonard将成功添加,登录用户将从Alex更改为Leonard。

如何防止此更改?

UPDATE:

这里是我的代码:

Template.addingUser.events({ 
    'submit #addUser': function (e, t) { 

     e.preventDefault(); 

     Session.set('name', t.find('#name').value); 
     Session.set('email', t.find('#email').value); 
     Session.set('homeAddress', t.find('#homeAddress').value); 
     Session.set('pass', t.find('#pass').value); 


     Session.set('taxNo', t.find('#taxNo').value); 


     let userId = Accounts.createUser({ 
      username: Session.get('name'), 
      password: Session.get('pass'), 
      email: Session.get('email'), 
      profile: { 
      homeAddress: Session.get('homeAddress'), 
     } 

     }, function (err) { 
      if (err) 
       console.log(err); 
      else 
       console.log('It worked...'); 
     }); 
    } 
}); 

更新1.1

我试图用Accounts.onCreateUser功能。用户已成功添加,但当前登录的用户将在添加后注销。

这里是我的代码:

客户:

var options = { 
      username: t.find('#name').value, 
      password: t.find('#pass').value, 
      email: t.find('#email').value, 
      profile: { 
       homeAddress: t.find('#homeAddress').value, 
      } 
     }; 
     Meteor.call('createUser', options); 

服务器:

Meteor.methods({ 
    employeeAddition: function(options){ 
     Accounts.onCreateUser(function(options, user) { 
      if (options.profile) 
       user.profile = options.profile; 
     }); 
     var userId = Accounts.createUser(); 
    }, 
}); 

如何防止以后新用户注销当前用户被添加?

+0

请出示您使用添加新用户的代码。 –

+0

@MichelFloyd我更新了这个问题:) –

回答

1

您正在设置名称,电子邮件,homeAddress并传递给会话。 如果您使用Session.get('name')来控制登录的用户,这就解释了登录用户更新的原因。

试着不要在会话中设置值。我没有看到这一点。

希望有所帮助。

+0

是啊你的权利,我现在修改代码,希望它会起作用,稍后会更新你。感谢 –

+0

代码看起来更好,方式更少,谢谢。问题仍然存在tho -_- –

1

Accounts.createUser注册成功后会自动登录用户,看起来没有Meteor提供的选项关闭它。所以,你会需要自己打电话createdUser防止:

Meteor.call('createUser', { 
    username: Session.get('name'), 
    password: Accounts._hashPassword(Session.get('pass')), 
    email: Session.get('email'), // you have to hash the password 
    profile: { 
    homeAddress: Session.get('homeAddress'), 
    }, 
}, function(err, re) { 
    // ... 
}) 
+0

谢谢,我upvoted你,这个解决方案帮助不登录新用户,但当前用户将被注销-_- –

2

这是因为你的客户端调用Accounts.createUser()代替服务器的。

docs

在客户端,这个功能登录作为 成功完成新创建的用户。在服务器上,它将返回新创建的 用户标识。

您需要添加在你调用Accounts.createuser()

+0

我现在正在实施的解决方案,将回到你的一会儿,谢谢:) –

0

使用Accounts.onCreateUser解决了这个问题对我来说

客户端服务器方法:

var options = { 
      username: t.find('#name').value, 
      password: t.find('#pass').value, 
      email: t.find('#email').value, 
      profile: { 
       homeAddress: t.find('#homeAddress').value, 

      } 
     }; 

     Meteor.call('employeeAddition', options); 

然后我经过选择的方法,其中我调用了Accounts.createUserAccounts.onCreateUser。因为它们在docs中提及。

默认情况下,配置文件选项直接添加到新的用户文档。要覆盖此行为,请使用Accounts.onCreateUser。

下面是代码:

服务器:

Meteor.methods({ 
    employeeAddition: function(options){ 

     Accounts.createUser(options); 

     Accounts.onCreateUser(function(options, user) { 
      if (options.profile) 
       user.profile = options.profile; 
     }); 
    }, 
});