Hello everyone. REST API is very common in Magento 2. To send the request to Rest, it needs the resource to give the permission to access the data or process the logic code.
In this blog, I will show you how to rewrite the REST authentication. So you can create your custom authentication. (Magento <= 2.1.2)
- First, please create di.xml file:
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Webapi\Controller\Rest" type="Magestore\Training\Controller\Rest"/> </config> |
- After that, please create the file to handle the rewrite:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php namespace Magestore\Training\Controller; use Magento\Framework\Exception\AuthorizationException; use Magento\Framework\Webapi\Rest\Request as RestRequest; use Magento\Framework\Webapi\Rest\Response as RestResponse; use Magento\Framework\Webapi\Rest\Response\FieldsFilter; use Magento\Webapi\Controller\Rest\Router; class Rest extends \Magento\Webapi\Controller\Rest { protected function checkPermissions() { //Todo } } |
Please write the code to check permissions in the //todo.
After that, please refresh the cache to make it work.
The steps I mention above is the shortest process for you to Override REST Authentication in Magento 2.
Thank you for reading this post and see you in other posts from Magestore!
Your Magento website needs a powerful Magento ERP for stock control.
DON'T MISS THE CHANCE!