· 5 years ago · Aug 24, 2020, 01:40 PM
1<?php namespace App\Filters;
2
3use CodeIgniter\HTTP\RequestInterface;
4use CodeIgniter\HTTP\ResponseInterface;
5use CodeIgniter\Filters\FilterInterface;
6
7
8// Panggil JWT
9use \Firebase\JWT\JWT;
10// panggil class Auht
11use App\Controllers\Auth;
12// panggil restful api codeigniter 4
13use CodeIgniter\RESTful\ResourceController;
14
15class LoginFilter implements FilterInterface
16{
17 public function __construct()
18 {
19 $this->protect = new Auth();
20 }
21
22 public function before(RequestInterface $request)
23 {
24 $secret_key = $this->protect->privateKey();
25 $token = null;
26 $authHeader = $this->request->getServer('HTTP_AUTHORIZATION');
27 $arr = explode(" ", $authHeader);
28 $token = $arr[1];
29 if($token){
30 $decoded = JWT::decode($token, $secret_key, array('HS256'));
31 if(!$decoded){
32 $output = [
33 'message' => 'Access denieds',
34 "error" => $e->getMessage()
35 ];
36
37 return $this->respond($output, 401);
38 }
39 }
40 }
41
42 //--------------------------------------------------------------------
43
44 public function after(RequestInterface $request, ResponseInterface $response)
45 {
46 // Do something here
47 }
48}