I have a 2FA option users can enable. I am currently writing an API layer where I want to preserve this functionality. So far, I haven't been able to hook into authentication. \Drupal\user\UserAuth doesn't invoke any hook or event.

Is replacing the user.auth service with a custom implementation the only solution?

  • 1
    Swapping the core user auth I believe is the way to go. I recently implemented SSO for the same reason you mentioned in your question.
    – Kevin
    Nov 11, 2017 at 0:00

1 Answer 1


You need to:

  1. Create a service provider in your custom module that extends ServiceProviderBase. The name of this class must be your module name, in camel case, with ServiceProvider appended. The class must implements alter(), which changes the class for the user.auth service.

    public function alter(ContainerBuilder $container) {
      $definition = $container->getDefinition('user.auth');
  2. Drupal\my_module\AuthenticationClass needs to extend \Drupal\user\UserAuth and define an authenticate($username, $password) method, which returns TRUE when the authentication is successful.

  • Did this work for you @ivan-jaros ?
    – leopinzon
    Aug 8, 2018 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.