Change record status: 
Project: 
Introduced in branch: 
8.x
Introduced in version: 
8.x
Description: 

The procedural user_authenticate() has now been deprecated in favour of using a 'user.auth' service (UserAuth object).

Before:

$form_state['uid'] = user_authenticate($form_state['values']['name'], $password);

Now:

// Inject user.auth service..
$form_state['uid'] = $this->userAuth->authenticate($form_state['values']['name'], $password);

// or
$form_state['uid'] = \Drupal::service('user.auth')->authenticate($form_state['values']['name'], $password);
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

quartarian’s picture

$form_state is now also an object.

$form_state->setValue('uid', 
    \Drupal::service('user.auth')->authenticate(
        $form_state->getValue("name"),
        $form_state->getValue("pass")
    )
)
leopinzon’s picture

Hi guys, I'm migrating some old code from D6 to D8 but unfortunately, facing some trouble with this guide. Previously I had this:
user_authenticate($userRequest)

Now I changed it per @damiankloip 's suggestion to:
\Drupal::service('user.auth')->authenticate($userRequest['name'],$userRequest['pass']);

It runs perfect and returns the user id OK, the problem is that after finishing the execution, the user is still logged out. ..

Can you help me figuring out what am I missing?

Thanks in advance

leopinzon’s picture

After digging in, I found the solution:

The first line returns the user_id, then it is needed to create the entity and call the user_login_finalize function like this:

$uid = \Drupal::service('user.auth')->authenticate($userRequest['name'],$userRequest['pass']);
$user = \Drupal\user\Entity\User::load($uid);
user_login_finalize($user);

Hope this helps someone else, cheers!