Composer package to externally authenticate Drupal users
If the user exists in Drupal, authenticates the user.
If the user does not exist in Drupal, it creates the user and authenticates.
composer require lyseontech/drupal-external-auth
$response = new Response();
$pdo = new PDO(...);
(new \DrupalExternalAuth\Auth($response, $pdo))->auth([
'name' => 'username',
'pass' => 'PrefixHash$' . 'hashOfPassord',
'timezone' => 'America/Sao_Paulo',
'langcode' => 'pt-br',
'roles' => ['administrator']
]);
foreach ($response->headers->getCookies() as $cookie) {
header('Set-Cookie: '.$cookie->getName().strstr($cookie, '='));
}
In PrefixHash, put the prefix hash to identify the hash password from your system.
In hashPassord put the hash for user, if don't is necessary the user authenticate in Drupal by default login page of Drupal, put anything in this field.
If you dont implement custom validation hash in Drupal, the user can only access Drupal through your system.
$response = new Response();
$pdo = new PDO(...);
(new \DrupalExternalAuth\Auth($response, $pdo))->logout();
foreach ($response->headers->getCookies() as $cookie) {
header('Set-Cookie: '.$cookie->getName().strstr($cookie, '='));
}