1

I tried to create user in Drupal8 using entity_create. The user is created just fine, but I can't login with the password unless I change it from the admin page.

$authData['name'] = 'testuser';
$authData['pass'] = 'testpass';
$authData['mail'] = 'sss@test.com';
$authData['status'] = 1;
$account = entity_create('user', $authData);

I also tried, the following code (obtained from drush)

$account->pass = 'testpass';
$account->save();

and also $account->setPassword

none of above works.

0
1

I try with you code, I not see any problem and work for me (drupal 8 beta7).

$authData['name'] = 'testuser';
$authData['pass'] = 'testpass';
$authData['mail'] = 'sss@test.com';
$authData['status'] = 1;
$account = entity_create('user', $authData);
$account->save();

Note that user and pass are different when you try to log in

user: testuser

pass: testpass

That drupal 8 version are you using?

2
  • I also use the latest version.. maybe what I am doing is not the right place.
    – Vorapoap
    Mar 17 '15 at 16:21
  • I know what was wrong, I tried to create a user in #validate user_login_form and forgot to call ::validateAuthentication right after the user was created... and the password never worked because of the intercept of this hook. ! Thanks to remind me that there is nothing wrong with my code so I took a look at somewhere else.
    – Vorapoap
    Mar 17 '15 at 16:35
1

I am using the following code:

$values = array(
'name' => 'test',
'mail' => 'test@example.com',
'roles' => array(),
'pass' => 'password',
'status' => 1,
);
$account = entity_create('user', $values);
$account->save();
2
  • I know what was wrong, I tried to create a user in #validate user_login_form and forgot to call ::validateAuthentication right after the user was created... and the password never worked because of the intercept of this hook. ! Thanks to remind me that there is nothing wrong with my code so I took a look at somewhere else.
    – Vorapoap
    Mar 17 '15 at 16:36
  • Yeah I had just tried removing the empty roles array as that was the only difference and it still worked but good to hear you worked it out.
    – pete80
    Mar 17 '15 at 17:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.