-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthFilter.php
More file actions
80 lines (68 loc) · 2.5 KB
/
AuthFilter.php
File metadata and controls
80 lines (68 loc) · 2.5 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) 2021 CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace App\Filters;
use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
class AuthFilter implements FilterInterface
{
/**
* Do whatever processing this filter needs to do.
* By default it should not return anything during
* normal execution. However, when an abnormal state
* is found, it should return an instance of
* CodeIgniter\HTTP\Response. If it does, script
* execution will end and that Response will be
* sent back to the client, allowing for error pages,
* redirects, etc.
*
* @param array|null $arguments
*
* @return RequestInterface|ResponseInterface|string|void
*/
public function before(RequestInterface $request, $arguments = null)
{
// Start a session
$session = session();
// $session->set('isLoggedIn', true);
// Check session
if (! $session->get('isLoggedIn')) {
// If not logged in, run logic
echo 'Please login before continue';
exit;
}
}
/**
* Allows After filters to inspect and modify the response
* object as needed. This method does not allow any way
* to stop execution of other after filters, short of
* throwing an Exception or Error.
*
* @param array|null $arguments
*
* @return ResponseInterface|void
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// This method will be executed after the controller action.
// Log the request and response data
$logger = \Config\Services::logger();
// Log request information
$logger->info('Request URL: ' . $request->getUri());
$logger->info('Request Method: ' . $request->getMethod());
$logger->info('Request Headers: ' . json_encode($request->getHeaders()));
$logger->info('Request Body: ' . $request->getBody());
// Log response information
$logger->info('Response Status Code: ' . $response->getStatusCode());
$logger->info('Response Headers: ' . json_encode($response->getHeaders()));
$logger->info('Response Body: ' . $response->getBody());
}
}