user login by php code i have an app that sends username and password to a php script. This script checks that a user exists on Drupal 8 with username and password entered but does not log the user. I need the user to log in. Can someone help me? thanks
Follows script code
use Drupal\user\UserAuthInterface;
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
// Boot Drupal.
$autoloader = require __DIR__ . '/autoload.php';

$request = Request::createFromGlobals();
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod', FALSE);
$kernel-&gt;boot();
// Recupero Nome utente e password
$nome_ut = $_POST['nome'];
$pass_ut = $_POST['pwd'];
// effettuo il controllo
$user_auth = \Drupal::service('user.auth');
$user_storage = \Drupal::entityTypeManager()-&gt;getStorage('user');

$accounts = $user_storage-&gt;loadByProperties(['name' =&gt; $nome_ut, 'status' =&gt; 1]);
$account = reset($accounts);

if ($account) {
    if ($user_auth-&gt;authenticate($nome_ut, $pass_ut))
       {
    // The credentials are correct.
       $id = $user_auth-&gt;authenticate($nome_ut, $pass_ut);
       print (&quot;Accesso riuscito!&quot;);
           // Force session start if we don't already have a session.
               $session = \Drupal::service('session');
               if (!$session-&gt;isStarted()) {
               $session-&gt;migrate();
           }
           if (\Drupal::currentUser()-&gt;isAuthenticated()) {
               // This user is logged in.
               print (&quot;Utente Loggato&quot;);
           } else {
               // This user is not logged in.
               print (&quot;Utente non Loggato&quot;);
           }
           
       }
}
Could you use a rest endpoint? Because this is already handled by drupal core. See for example this or this
Otherwise I think you're just missing user_login_finalize after your check if the user is logged in.
user_login_finalize($user);
//Eventually redirect the user somewhere
$user_destination = \Drupal::destination()-&gt;get();
$response = new RedirectResponse($user_destination);
$response-&gt;send();