0

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.
// REPLACE WITH WHATEVER ID YOU WANT TO LOGIN AS;
$uid = 100; 
$user = Drupal\user\Entity\User::load($uid);

// This is required to call user_login_finalize here.
$kernel->prepareLegacyRequest($request);
user_login_finalize($user);

$response->send();

$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.

2

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.

6
  • 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
    Jan 15 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
    Jan 15 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.
    – apaderno
    Jan 15 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
    Jan 15 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
    Jan 15 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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