Saya menggunakan login RESTFul khusus pada drupal 8 tetapi tidak dengan cookie. Ini untuk aplikasi seluler dan setiap kali saya membutuhkan informasi, saya menggunakan Otentikasi sederhana:
Karena Drupal 8.2x kita membutuhkan 2 file dalam modul:
rest.ressource.user.rest_ressource.yml di folder config / instal
langcode: en
status: true
dependencies:
module:
- basic_auth
id: user.rest_ressource
plugin_id: 'user_rest_ressource'
granularity: resource
configuration:
methods:
- GET
- PATCH
formats:
- json
authentication:
- basic_auth
Anda dapat menambahkan lebih banyak metode seperti DELETE / POST
Maka kita perlu file tersebut
userRestRessource.php di src / Plugin / rest / resource
<?php
namespace Drupal\yourmodule\Plugin\rest\resource;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Log\LoggerInterface;
/**
* Provides a resource to get view modes by entity and bundle.
*
* @RestResource(
* id = "user_rest_ressource",
* label = @Translation("User Rest"),
* uri_paths = {
* "canonical" = "/api/user/getInfo"
* }
* )
*/
class UserRestRessource extends ResourceBase {
/**
* A current user instance.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* Constructs a Drupal\rest\Plugin\ResourceBase 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 \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* A current user instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
array $serializer_formats,
LoggerInterface $logger,
AccountProxyInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->currentUser = $current_user;
}
/**
* {@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('yourmodulename'),
$container->get('current_user')
);
}
/**
* Responds to GET requests.
*
* Returns a list of bundles for specified entity.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function get() {
$uid=$this->currentUser->getAccount()->id();
$role=$this->currentUser->getAccount()->getRoles(1);
//here you can add your custom code
$responseResource=new ResourceResponse(
array()
);
return $responseResource;
}
/**
* Responds to PATCH requests.
*
* Returns a list of bundles for specified entity.
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* Throws exception expected.
*/
public function patch(){
}
}
Jangan lupa untuk langsung ke pengguna untuk menerima metode GET / POST atau apa pun yang Anda tambahkan dalam konfigurasi Anda.
Dengan itu Anda dapat membuat setiap file REST kustom untuk setiap entitas kustom.
Dan di js saya: Jangan lupa menelepon
yoursiteUrl / rest / session / token
untuk mendapatkan token
$http({
method: 'GET',
url: 'siteUrl/api/user/getInfo?_format=json',
withCredentials:true,
headers: {
'Content-Type': "application/hal+json",
'X-CSRF-Token': token,
'Authorization': 'Basic ' + btoa(user+':'+password),
},
}).then(function successCallback(response) {
return response;
}, function errorCallback(response) {
return false;
});