Meteor - 添加新用户,更改登录用户

时间:2016-12-05 23:54:34

标签: meteor meteor-accounts

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

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

如何阻止此更改?

更新:

这是我的代码:

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();
    },
});

如何在添加新用户后阻止当前用户注销?

4 个答案:

答案 0 :(得分:2)

这是因为您在客户端而不是服务器上调用Accounts.createUser()

来自docs

  

在客户端上,此功能以新创建的用户登录   顺利完成。在服务器上,它返回新创建的   用户ID。

您需要添加一个调用Accounts.createuser()

的服务器方法

答案 1 :(得分:1)

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

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

希望有所帮助。

答案 2 :(得分: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) {
  // ...
})

答案 3 :(得分: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;
        });
    },
});
相关问题