17

I add accounts-password and accounts-base packages in Meteor

When I create user like this:

Accounts.createUser({username: username, password : password}, function(err){
          if (err) {
            // Inform the user that account creation failed
            console.log("Register Fail!") 
            console.log(err)
          } else {
               console.log("Register Success!")
            // Account has been created and the user has logged
          }    
  });

Account has been created and the user has logged.

for instance, I log in as an administrator and I want to create a account for somebody,but I don't want to log out after create account.

How to prevent auto login after create user ?

I find source code of accouts-password packages:

48 - 63 lines:

// Attempt to log in as a new user.
Accounts.createUser = function (options, callback) {
  options = _.clone(options); // we'll be modifying options

  if (!options.password)
    throw new Error("Must set options.password");
  var verifier = Meteor._srp.generateVerifier(options.password);
  // strip old password, replacing with the verifier object
  delete options.password;
  options.srp = verifier;

  Accounts.callLoginMethod({
    methodName: 'createUser',
    methodArguments: [options],
    userCallback: callback
  });
};

Should I modify the source code to solve this problem?

Any help is appreciated.

4

4 回答 4

24

You're trying to use client side accounts management to perform a task it hasn't been designed for.

Client side accounts package purpose is to specifically allow new users to create their account and expect to be logged in immediately.

You have to remember that certain functions can be ran on the client and/or on the server with different behaviors, Accounts.createUser docs specifies that : "On the client, this function logs in as the newly created user on successful completion."

On the contrary, "On the server, it returns the newly created user id." (it doesn't mess with the currently logged in user on the client).

In order to solve your problem, you should write a server side method creating a new user and be able to call it from your client side admin panel, after filling correctly a user creation form of your own design.

于 2013-06-28T08:42:10.370 回答
1

如果你真的想要这种行为,你需要修改password_server.js

并删除第 474-475 行,其中包含:

// client gets logged in as the new user afterwards.
this.setUserId(result.id);

所以用户创建后不会登录。

于 2013-06-28T10:33:07.740 回答
0

我有同样的问题。我想创建一个管理员界面,管理员可以在其中设置用户的密码,但不能以明文形式将其传递给服务器方法。Accounts.createUser 的客户端已经处理了这个问题,所以我只是accounts-password/password-server.js在存在标志的情况下更改正常的事件序列。它并不完美或漂亮,但似乎可以工作,您不必accounts-password直接修改包。

Meteor.startup(function ()
    {

        // store the default createUser method handler
        var default_create_user = Meteor.server.method_handlers.createUser;

        // remove it so we can register our own
        delete Meteor.server.method_handlers.createUser;

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

            var default_login_method = Accounts._loginMethod;

            // check if noAutoLogin flag is present
            if (options.noAutoLogin)
            {
                // temporarily disable the login method while creating our user

                // NB: it might be possible that simultaneous calls to createUser that do want the default behavior
                // would use the altered Accounts._loginMethod instead

                Accounts._loginMethod = function(s, m, a, p, fn)
                {
                    // this is the callback that is constructed with a closure of the options array and calls internal create functions
                    fn();

                    // restore default _loginMethod so other calls are not affected
                    Accounts._loginMethod = default_login_method;
                }
            }

            // invoke the default create user now that the login behavior has been short-circuited
            default_create_user(options);

        }});
    });
于 2014-04-29T20:13:30.817 回答
0

如果您想Accounts.createUser在不登录用户的情况下继续在客户端上使用。您可以调用Meteor.logout()fromcreateUser可选回调

Accounts.createUser(user, err => {
  if (err) {
    // handle error
    return;
  }

  // Prevent unwanted login
  Meteor.logout();
});
于 2017-11-02T11:08:46.290 回答