Skip to content

Commit 7f0d0e3

Browse files
committed
Added custom functions, user JWT, and response allowed headers
1 parent a7cd262 commit 7f0d0e3

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

app/config/routes.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,50 @@
66
* ? Allowed Headers:
77
* ? - Allowed origins: *
88
* ? - Allowed methods: GET, POST, PUT, DELETE, OPTIONS
9-
* ? - Allowed headers: headerRef, Content-Type, Authorization, X-Requested-With
9+
* ? - Allowed headers: Content-Type, Authorization, X-Requested-With
1010
* ? - Allowed credentials: true
1111
* ? - Max age: 86400
1212
* -----------------------------------------------------------------------------
1313
* ? Availlable Middlewares:
1414
* ? - Auth: check if user is logged in with "JWT" or "API KEY in Header" and has the right permissions to access the route
1515
* ? - Validation: Sanitize input data and check if it is valid (e.g. email, password, etc.)
1616
* -----------------------------------------------------------------------------
17-
* ? Auth MIDDLEWARE PARAMETERS:
17+
* ? Auth MIDDLEWARE FLAGS:
1818
* ? - - guest: check if user is not logged in
1919
* ? - - user: check if user is logged in
2020
* ? - - admin: check if user is logged in and has the right permissions to access the route
21-
* ? - Validation MIDDLEWARE PARAMETERS:
21+
*
22+
* ? Validation MIDDLEWARE PARAMETERS:
2223
* ? - - field1|field2|field3: List of fields to check if they are valid (e.g. email, password, etc.)
2324
* ? - - scope: Scope of the validation rules (e.g. all, Example1, Example2, etc.)
2425
* -----------------------------------------------------------------------------
2526
* ? Example:
27+
*
2628
* ? - GET ROUTE:
2729
* * $router->get('/', 'HomeController@index');
30+
*
2831
* ? - POST ROUTE:
2932
* * $router->post('/', 'HomeController@index');
33+
*
3034
* ? - PUT ROUTE:
3135
* * $router->put('/', 'HomeController@index');
36+
*
3237
* ? - DELETE ROUTE:
3338
* * $router->delete('/', 'HomeController@index');
39+
*
3440
* ? - ROUTE WITH MIDDLEWARES:
3541
* * $router->get('/', 'HomeController@index', ['Auth@guest', Validation@email|password|etc.@Example1']);
3642
*
43+
* ? - ROUTE WITH CUSTOM FUNCTION:
44+
* * $router->get('/', fn($data) => {
45+
* * // Do something
46+
* * });
47+
*
48+
* ? - ROUTE WITH CUSTOM FUNCTION AND MIDDLEWARES:
49+
* * $router->get('/', fn($data) => {
50+
* * // Do something
51+
* * }, ['Auth@guest', Validation@email|password|etc.@Example1']);
52+
*
3753
* @package App\Config
3854
* @author Mohammed-Aymen Benadra
3955
*/

app/controllers/Auth.php

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ public function login($data = [])
6565
{
6666
$example = $this->model('Example')->getBy('headerRef', $data['headerRef']);
6767

68-
if (!$example) {
69-
Router::abort(404, json_encode([
70-
'status' => 'error',
71-
'message' => 'example not found'
72-
]));
73-
}
74-
7568
Response::send([
7669
'status' => 'success',
7770
'data' => $example
@@ -86,15 +79,6 @@ public function login($data = [])
8679
*/
8780
public function registerJWT($data = [])
8881
{
89-
$example = $this->model('Example')->getBy('username', $data['username']);
90-
91-
if ($example) {
92-
Router::abort(400, json_encode([
93-
'status' => 'error',
94-
'message' => 'example already exists'
95-
]));
96-
}
97-
9882
// Hash password
9983
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
10084

@@ -123,13 +107,6 @@ public function loginJWT($data = [])
123107
{
124108
$example = $this->model('Example')->getBy('username', $data['username']);
125109

126-
if (!$example) {
127-
Router::abort(404, json_encode([
128-
'status' => 'error',
129-
'message' => 'example not found'
130-
]));
131-
}
132-
133110
if (!password_verify($data['password'], $example->password)) {
134111
Router::abort(401, json_encode([
135112
'status' => 'error',
@@ -155,9 +132,8 @@ public function loginJWT($data = [])
155132
$jwt = JWT::encode($payload, $secret_key, "HS256");
156133

157134
// Set expirable cookie for JWT
158-
setcookie('jwt', $jwt, $expire_claim, "/", $_ENV['SERVER_ADDRESS'], false, true);
135+
setcookie(name: 'jwt', value: $jwt, expires_or_options: $expire_claim, httponly: true);
159136

160-
Response::code();
161137
Response::send(
162138
array(
163139
"message" => "Successful login.",
@@ -166,15 +142,33 @@ public function loginJWT($data = [])
166142
);
167143
}
168144

145+
/**
146+
* Logout an User
147+
*
148+
* @return void
149+
*/
150+
public function logoutJWT()
151+
{
152+
setcookie(name: 'jwt', value: '', expires_or_options: time() - 3600, httponly: true);
153+
154+
Response::send([
155+
'status' => 'Logged out successfully!'
156+
]);
157+
}
158+
169159
/**
170160
* Get current authenticated User
171161
*
172162
* @return object
173163
*/
174-
public static function user()
164+
public static function userJWT()
175165
{
176166
$jwt = Request::authorization();
177167

168+
if (!$jwt) {
169+
return null;
170+
}
171+
178172
$token = JWT::decode($jwt, new Key($_ENV['JWT_SECRET_KEY'], "HS256"));
179173

180174
return (new Example)->getBy('username', $token->sub);

core/helpers/Response.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ class Response
1010
public static function headers(
1111
$contentType = 'application/json',
1212
$allowOrigin = '*',
13-
$allowMethods = 'GET, POST, PUT, DELETE, OPTIONS'
13+
$allowMethods = 'GET, POST, PUT, DELETE, OPTIONS',
14+
$allowHeaders = 'X-Requested-With, Content-Type, Authorization'
1415
) {
1516
header('Content-Type: ' . $contentType . '; charset=UTF-8');
1617
header('Access-Control-Allow-Origin: ' . $allowOrigin);
1718
header('Access-Control-Allow-Methods: ' . $allowMethods);
18-
header('Access-Control-Allow-Headers: clientRef, Content-Type, Authorization, X-Requested-With');
19+
header('Access-Control-Allow-Headers: ' . $allowHeaders);
1920
header('Access-Control-Allow-Credentials: true');
2021
header('Access-Control-Max-Age: 86400');
2122
}

0 commit comments

Comments
 (0)