Behrouz Riahi
Behrouz Riahi

Reputation: 1791

Meteor - adding new user, changes the logged in user

I am having a weird issue, I created a form that adds new user to the database and it works fine.

The problem now is, if I am logged in with "Alex" account, and I added "Leonard" to the database, Leonard will be added successfully, and the logged in user will change from Alex to Leonard.

How do I prevent this change?

UPDATE :

Here is my code:

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

UPDATE 1.1

I tried to use Accounts.onCreateUser function. The user is successfully added, but the current logged in user will be signed out, after the addition.

here is my code:

Client:

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);

Server:

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

How do I prevent the current user from logging out after new user is being added?

Upvotes: 0

Views: 198

Answers (4)

kkkkkkk
kkkkkkk

Reputation: 7738

Accounts.createUser will log user in automatically after sign up successful, and it seems to be there is no options provided by Meteor to turn it off. So you would need to call createdUser by yourself to prevent that:

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

Upvotes: 1

Behrouz Riahi
Behrouz Riahi

Reputation: 1791

Using Accounts.onCreateUser solved it for me

Client:

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);

Then I passed options to the method in which I invoked Accounts.createUser and Accounts.onCreateUser. As it is mentions in their docs.

By default the profile option is added directly to the new user document. To override this behavior, use Accounts.onCreateUser.

Here is the code:

Server:

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

        Accounts.createUser(options);

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

Upvotes: 0

Michel Floyd
Michel Floyd

Reputation: 20227

It's because you are invoking Accounts.createUser() on the client instead of the server.

From the docs

On the client, this function logs in as the newly created user on successful completion. On the server, it returns the newly created user id.

You need to add a server method in which you invoke Accounts.createuser()

Upvotes: 2

Danilo Miranda
Danilo Miranda

Reputation: 111

You are setting the name, email, homeAddress and pass to the Session. If you are using the Session.get('name') to control your logged user, that explain why the logged user are been updated.

Try do not set there values to the Session. I don't see the point of that.

Hope that help.

Upvotes: 1

Related Questions