...
|
|
@ -0,0 +1,61 @@
|
|||
https://www.youtube.com/watch?v=USqOc-GCA4s&list=PLT9miexWCpPW6Z_OXNv2d78cqCrZaPrVG&index=32
|
||||
|
||||
Create AuthFilter.php
|
||||
```bash
|
||||
php spark make:filter Auth --suffix
|
||||
```
|
||||
|
||||
Define before method in AuthFilter.php
|
||||
```php
|
||||
public function before(RequestInterface $request, $arguments = null)
|
||||
{
|
||||
helper("auth");
|
||||
|
||||
if (!auth("tokens")->loggedIn()) {
|
||||
return redirect()->to(base_url("api/ invalid-access")); // need to create invalid-access route
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Routes.php
|
||||
```php
|
||||
// API ROUTES
|
||||
$routes->group("api",["namespace]=> "App\Controllers\Api"], function($routes) {
|
||||
...
|
||||
$routes->get("invalid-access", "AuthController::accessDenied");
|
||||
§routes->get('demo-method", "AuthController::demoMethod",["filter" => "apiauth"]); // Filter Name is defined in aliases array in Filters.php below.
|
||||
...
|
||||
```
|
||||
|
||||
Define accessDenied method in app/Controllers/Api/AuthController.php
|
||||
```php
|
||||
...
|
||||
public function accessDenied()
|
||||
{
|
||||
return $this->respondCreated([
|
||||
"status" => false,
|
||||
"message" => "Invalid access",
|
||||
"data" => []
|
||||
]);
|
||||
}
|
||||
...
|
||||
```
|
||||
|
||||
Add filter to app/Config/Filters.php
|
||||
```php
|
||||
use App\Filters\AuthFilter;
|
||||
|
||||
|
||||
public array $aliases = [
|
||||
'csrf' => CSRF::class,
|
||||
'toolbar' => DebugToolbar::class,
|
||||
'honeypot' => Honeypot::class,
|
||||
'invalidchars' => InvalidChars::class,
|
||||
'secureheaders' => SecureHeaders::class,
|
||||
'apiauth' => AuthFilter::class
|
||||
]
|
||||
```
|
||||
|
||||
Authorize request by adding field "Authorization" with value "Bearer xxxxxx" in Request Header:
|
||||
![[Pasted image 20241127140801.png]]
|
||||
|
Before Width: | Height: | Size: 198 KiB After Width: | Height: | Size: 198 KiB |
|
Before Width: | Height: | Size: 138 KiB After Width: | Height: | Size: 138 KiB |
|
Before Width: | Height: | Size: 86 KiB After Width: | Height: | Size: 86 KiB |
|
Before Width: | Height: | Size: 132 KiB After Width: | Height: | Size: 132 KiB |
|
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 27 KiB |
|
Before Width: | Height: | Size: 73 KiB After Width: | Height: | Size: 73 KiB |
|
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 68 KiB |
|
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 50 KiB |
|
Before Width: | Height: | Size: 45 KiB After Width: | Height: | Size: 45 KiB |
|
Before Width: | Height: | Size: 92 KiB After Width: | Height: | Size: 92 KiB |
|
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 51 KiB |
|
Before Width: | Height: | Size: 61 KiB After Width: | Height: | Size: 61 KiB |
|
Before Width: | Height: | Size: 345 KiB After Width: | Height: | Size: 345 KiB |
|
Before Width: | Height: | Size: 217 KiB After Width: | Height: | Size: 217 KiB |
|
Before Width: | Height: | Size: 111 KiB After Width: | Height: | Size: 111 KiB |
BIN
MyObsidianVault/_images/Pasted image 20241127140801.png
Normal file
|
After Width: | Height: | Size: 163 KiB |