I want to log in Drupal 8 users from an external PHP file. I am trying the following code.

use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
use Drupal\user\UserInterface;
//$autoloader = include('/vendor/autoload.php');
$autoloader = require_once __DIR__ . '/vendor/autoload.php'; 
$kernel = new DrupalKernel('prod', $autoloader);

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

// ID of the user.
$uid = 100; 
$user = Drupal\user\Entity\User::load($uid);

// This is required to call user_login_finalize here.


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

When I run it, I get the following error.

TypeError: Argument 1 passed to user_login_finalize() must implement interface Drupal\user\UserInterface, null given, called in /var/www/html/stocksee/public_html/component/login.php on line 22 in user_login_finalize() (line 554 of core/modules/user/user.module).

Please help me.

1 Answer 1


I'd say in this specific case the error message looks like saying that $user is NULL. You need to validate that User::load returns a valid UserInterface because if the $uid passed to the method is not found, it will return NULL, which is the value you are getting. Try with a valid uid like 1, see what happens and move ahead from there.

  • I already tried but I am getting error "Error: Call to undefined function user_login_finalize() in main() (line 45 of login.php)." @jbarrio
    – sourabh
    Commented Jan 15, 2021 at 9:12
  • 3
    Well, but that's a completely different error message from the one you exposed in your question. This second error you are mentioning is happening due to the fact that you are invoking the function user_login_finalize from the external PHP file, which doesn't have bootstrapped the Drupal's User module found in its core. In your case I'd consider to use the API to perform the login. Something similar to this drupal.stackexchange.com/questions/185494/…. Please remember to vote answers if you find them useful.
    – jbarrio
    Commented Jan 15, 2021 at 9:27
  • Yes, that is a different error, and it's not caused from changing the user ID to load the user entity. If you get an Error: Call to undefined function user_login_finalize() error, you get it even with the code shown in the question.
    – avpaderno
    Commented Jan 15, 2021 at 10:07
  • use Drupal\Core\DrupalKernel; use Drupal\user\Entity\User; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; $autoloader = require_once '/component/autoload.php'; $kernel = new DrupalKernel('prod', $autoloader); $request = Request::createFromGlobals(); $response = $kernel->handle($request); $uid = 1; if(isset($uid)) { $user = User::load($uid); user_login_finalize($user); $user_destination = \Drupal::destination()->get(); $response = new RedirectResponse($user_destination); $response->send(); }
    – sourabh
    Commented Jan 15, 2021 at 10:12
  • 3
    @sourabh Please don't post code in comments - instead, edit your question, and make it reflect what you're asking. If it's a different question from your original one, or would invalidate this answer, please post it as a new question
    – Clive
    Commented Jan 15, 2021 at 10:49

Your Answer

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

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