Drupal 9 rest custom login resource, возвращает данные сессии для создания cookie во фронтенде.
Отсутствует csrf (можно получить по адресу /session/token
).
Создайте плагин rest с помощью команды Drush.
Или используя псевдоним
Это ресурс POST
, поэтому выполните команду $ drush cr
.
С помощью Rest UI включите ресурс и добавьте разрешение для анонимной роли.
<?php
namespace Drupalcustom_rest_apiPluginrestresource;
use DrupalCoreSessionAccountProxyInterface;
use DrupalrestModifiedResourceResponse;
use DrupalrestPluginResourceBase;
use PsrLogLoggerInterface;
use SymfonyComponentDependencyInjectionContainerInterface;
use DrupalCoreSessionSessionManagerInterface;
use DrupalCoreExtensionModuleHandlerInterface;
use DrupalCorePasswordPasswordInterface;
use SymfonyComponentHttpKernelExceptionBadRequestHttpException;
/**
* Represents Custom login resource records as resources.
*
* @RestResource (
* id = "custom_rest_api_custom_login_resource",
* label = @Translation("Custom login resource"),
* uri_paths = {
* "create" = "/api/custom/login"
* }
* )
*
* @DCG
* This plugin exposes database records as REST resources. In order to enable it
* import the resource configuration into active configuration storage. You may
* find an example of such configuration in the following file:
* core/modules/rest/config/optional/rest.resource.entity.node.yml.
* Alternatively you can make use of REST UI module.
* @see https://www.drupal.org/project/restui
* For accessing Drupal entities through REST interface use
* DrupalrestPluginrestresourceEntityResource plugin.
*/
class CustomLoginResource extends ResourceBase {
/**
* A current user instance.
*
* @var DrupalCoreSessionAccountProxyInterface
*/
protected $currentUser;
protected $sessionManager;
protected $moduleHandler;
protected $password;
/**
* Constructs a new CustomLoginResource object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param array $serializer_formats
* The available serialization formats.
* @param PsrLogLoggerInterface $logger
* A logger instance.
* @param DrupalCoreSessionAccountProxyInterface $current_user
* A current user instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
AccountProxyInterface $current_user,
SessionManagerInterface $session_manager,
ModuleHandlerInterface $module_handler,
PasswordInterface $password) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->currentUser = $current_user;
$this->sessionManager = $session_manager;
$this->moduleHandler = $module_handler;
$this->password = $password;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->getParameter('serializer.formats'),
$container->get('logger.factory')->get('exp_fs'),
$container->get('current_user'),
$container->get('session_manager'),
$container->get('module_handler'),
$container->get('password')
);
}
/**
* Responds to POST requests.
*
* @return DrupalrestModifiedResourceResponse
* The HTTP response object.
*
* @throws SymfonyComponentHttpKernelExceptionHttpException
* Throws exception expected.
*/
public function post($data) {
$this->validate($data);
$pass_check = FALSE;
$name = $data['name'];
$pass = $data['pass'];
$account = user_load_by_name(trim($name));
if ($account) {
$pass_check = $this->password->check(trim($pass), $account->getPassword());
}
else {
$body = [
'error' => 'Wrong username and/or password.',
];
}
if ($pass_check == FALSE) {
$body = [
'error' => 'Wrong username and/or password..',
];
}
else {
$session = Drupal::service('session');
$session->migrate();
$session->set('uid', $account->id());
$this->moduleHandler->invokeAll('user_login', [$account]);
user_login_finalize($account);
$sess_name = $this->sessionManager->getName();
$sess_id = $this->sessionManager->getId();
$body = [
'sess_name' => $sess_name,
'sess_id' => $sess_id,
'current_user' => [
'name' => $account->getAccountName(),
'uid' => $account->id(),
'roles' => $account->getRoles(),
],
];
}
return new ModifiedResourceResponse($body, 200);
}
/**
* Validates incoming record.
*
* @param mixed $record
* Data to validate.
*
* @throws SymfonyComponentHttpKernelExceptionBadRequestHttpException
*/
protected function validate($record) {
if (!is_array($record) || count($record) == 0) {
throw new BadRequestHttpException(t('No record content received'));
}
if (empty($record['name'])) {
throw new BadRequestHttpException(t('name id is required'));
}
if (empty($record['pass'])) {
throw new BadRequestHttpException(t('Password date is required'));
}
}
}