Skip to main content
Drupal 8

Main navigation

  • Home
    • Question And Answer
    • NEWS
    • Projects
  • Tutorials - Drupal
    • Android
    • Other
User account menu
  • Log in

Breadcrumb

  1. Home

Login programmatically as a user

By editor , 9 December, 2015
Question

How to login programmatically as a user  in Drupal 8 ?
How to autologin in D8?

Example:
$user = User::load($uid);
user_login_finalize($user);

Tags

  • Drupal 8
  • Code
  • User
  • Add new comment

Comments8

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Himanshu Bhawsar (not verified)

10 years 7 months ago

Thanks for this code

Thanks for this code, you save my day. It's working for me.

  • Reply

Shah (not verified)

8 years 12 months ago

In reply to Thanks for this code by Himanshu Bhawsar (not verified)

Login

How do you trigger this way of login? Does the code need to be in the controller?

  • Reply

Vindesh mohariya (not verified)

10 years 4 months ago

How to login programmatically into Drupal

Sometimes we need to login into our Drupal site not through the regular login form. For example if we have trouble with login form or maybe we just forgot our password... Here are few ways to login into your Drupal site not through login form.

For Drupal 8 you just need to create file your_auto_login.php in the Drupal root directory, put there the next code and open this file in your browser:

<?php

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;

$autoloader = require_once 'autoload.php';

$kernel = new DrupalKernel('prod', $autoloader);

$request = Request::createFromGlobals();
$response = $kernel->handle($request);

// ID of the user.
$uid = THE_AUTOMATIC_USER_ID; //Do not use 1 because it is the super admin
$user = Drupal\user\Entity\User::load($uid);
user_login_finalize($user);

$response->send();

$kernel->terminate($request, $response);

  • Reply

editor

10 years 4 months ago

In reply to How to login programmatically into Drupal by Vindesh mohariya (not verified)

If Forget password

If you forget the password, use drush to recover

drush uli

  • Reply

Himanshi Bansal (not verified)

9 years 1 month ago

In reply to How to login programmatically into Drupal by Vindesh mohariya (not verified)

Use this code in custom module

Hi,

When I am using this code in a custom module, I am getting below error

"InvalidArgumentException: Cannot redirect to an empty URL. in Symfony\Component\HttpFoundation\RedirectResponse->setTargetUrl() (line 76 of /websites/www/html/drl/vendor/symfony/http-foundation/RedirectResponse.php) #0 /websites/www/html/drl/vendor/symfony/http-foundation/RedirectResponse.php(39): Symfony\Component\HttpFoundation\RedirectResponse->setTargetUrl(NULL) #1 /websites/www/html/drl/sites/grants/themes/grants/grants.theme(1536): Symfony\Component\HttpFoundation\RedirectResponse->__construct(NULL) #2 [internal function]: custom_login_user_login(Object(Drupal\user\Entity\User)) #3 /websites/www/html/drl/core/lib/Drupal/Core/Extension/ModuleHandler.php(402): call_user_func_array('custom_login_us...', Array) #4 /websites/www/html/drl/core/modules/user/user.module(559): Drupal\Core\Extension\ModuleHandler->invokeAll('user_login', Array) #5 /websites/www/html/drl/your_auto_login.php(12): user_login_finalize(Object(Drupal\user\Entity\User)) #6 {main}."

 

Can you please help me where the thing is going wrong.

  • Reply

Raghvendra Gupta (not verified)

10 years 4 months ago

IF you forgate password of drupal 8

You need to put index.php at root $uid ='Your User ID Here'; $account = \Drupal\user\Entity\User::load($uid); \Drupal::service('session')->migrate(); \Drupal::service('session')->set('uid', $account->id()); \Drupal::moduleHandler()->invokeAll('user_login', array($account)); or $uid ='Your User ID Here'; $user = Drupal\user\Entity\User::load($uid); user_login_finalize($user); $response->send();
  • Reply

Sandeep (not verified)

9 years 5 months ago

Undefined user_login_finalize function

( ! ) Fatal error: Call to undefined function user_login_finalize() in F:\wamp64\www\oxshot-rep-sandeep\your_auto_login.php on line 17
  • Reply

editor

9 years 5 months ago

In reply to Undefined user_login_finalize function by Sandeep (not verified)

Are you using a drupal 8 module ?

Hello,
is this (F:\wamp64\www\oxshot-rep-sandeep\your_auto_login.php) a drupal 8 module ? If you use it out side of drupal, the error is normal.

 

  • Reply

Links

  • Drupal.org
  • Drupal core release cycle
  • Drupal events with ease
  • Unit test with Drupal using PHPUnit (Drupal 10)
  • Tokens for file upload url
  • Tips for Docker
  • CKEditor inline image files usage for custom forms.
  • Install Docker on Linux Mint
  • Install PHP 8.1
  • Drupal Security Best Practices
  • Order and sort items with tabledrag
  • Add a custom service YAML container
  • Apply a patch with composer