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

Meteor-添加新用户,更改登录用户,meteor,meteor-accounts,Meteor,Meteor Accounts,我有一个奇怪的问题, 我创建了一个表单,将新用户添加到数据库中,效果很好 现在的问题是,如果我使用“Alex”帐户登录,并且我将“Leonard”添加到数据库中,Leonard将成功添加,并且登录的用户将从Alex更改为Leonard 如何防止这种变化 更新: 这是我的密码: Template.addingUser.events({ 'submit #addUser': function (e, t) { e.preventDefault(); Ses

我有一个奇怪的问题, 我创建了一个表单,将新用户添加到数据库中,效果很好

现在的问题是,如果我使用“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
函数。用户已成功添加,但添加后当前登录的用户将注销

这是我的密码:

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...');
        });
    }
});
客户:

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);
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);
服务器:

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

        Accounts.createUser(options);

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

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

您正在设置名称、电子邮件、家庭地址和会话传递。 如果您使用Session.get('name')来控制您的登录用户,这将解释为什么会更新登录用户

请尝试不为会话设置这些值。我不明白那有什么意义


希望您能提供帮助。

您正在设置姓名、电子邮件、家庭地址和会话传递。 如果您使用Session.get('name')来控制您的登录用户,这将解释为什么会更新登录用户

请尝试不为会话设置这些值。我不明白那有什么意义


希望有帮助。

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) {
  // ...
})

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) {
  // ...
})

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

在客户端上,此函数作为新创建的用户登录 成功完成。在服务器上,它返回新创建的 用户id


您需要添加一个服务器方法,在其中调用
Accounts.createuser()

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

在客户端上,此函数作为新创建的用户登录 成功完成。在服务器上,它返回新创建的 用户id


您需要添加一个服务器方法,在其中使用
帐户调用
帐户。createuser()

。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);
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.createUser
Accounts.onCreateUser
的方法。正如他们在书中提到的那样

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

代码如下:

服务器:

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

        Accounts.createUser(options);

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

使用
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);
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.createUser
Accounts.onCreateUser
的方法。正如他们在书中提到的那样

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

代码如下:

服务器:

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

        Accounts.createUser(options);

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

请显示您用于添加新用户的代码。@MichelFloyd我更新了问题:)请显示您用于添加新用户的代码。@MichelFloyd我更新了问题:)谢谢,我对您投了赞成票,此解决方案有助于不登录新用户,但当前用户将被注销-谢谢,我对您投了赞成票,此解决方案有助于不登录新用户,但当前用户将被注销-是的,您的权利,我现在正在修改代码,希望它能工作,稍后会更新您。谢谢代码看起来更好,而且更少,谢谢。问题仍然存在,是的,你是对的,我现在正在修改代码,希望它能起作用,会马上更新你。谢谢代码看起来更好,而且更少,谢谢。问题仍然存在,但是--我正在实施解决方案,稍后会回复您,谢谢:)我正在实施解决方案,稍后会回复您,谢谢:)