Skip to content

Commit 9bc8aaf

Browse files
committed
fix smtng
1 parent 483c59c commit 9bc8aaf

File tree

4 files changed

+52
-43
lines changed

4 files changed

+52
-43
lines changed

app/Http/Middleware/Auth.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Firebase\JWT\JWT;
6+
7+
class Auth
8+
{
9+
10+
public static function create($body, string $strtime = "")
11+
{
12+
$iat = time();
13+
14+
if (($timestamp = strtotime($strtime)) === false) {
15+
$timestamp = ($iat + 2629746);
16+
}
17+
18+
$payload = [
19+
"iat" => $iat,
20+
"exp" => $timestamp,
21+
"dd" => $body
22+
];
23+
24+
return (object)["type" => "Bearer", "token" => JWT::encode($payload, $_ENV['APP_KEY'])];
25+
}
26+
27+
28+
public static function verify(?string $jwt = null)
29+
{
30+
if (preg_match('/Bearer\s(\S+)/', $jwt, $matches)) {
31+
try {
32+
$decoded = JWT::decode($matches[1], $_ENV['APP_KEY'], array('HS256'));
33+
return $decoded;
34+
} catch (\Throwable $th) {
35+
return null;
36+
}
37+
} else {
38+
return null;
39+
}
40+
}
41+
}

app/Http/Middleware/JWTAuth.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

app/Http/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function form()
4040

4141
$body = [];
4242
foreach ($_POST as $key => $value) {
43-
$body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
43+
$body[$key] = trim(filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS));
4444
}
4545
return (object)$body;
4646
}

public/index.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
use App\Application;
44
use App\Routing\Route;
55
use App\Controllers\UserController;
6+
use App\Http\Middleware\Auth;
7+
use App\Http\Response;
8+
use App\Http\Request;
69

710
require __DIR__ . '/../vendor/autoload.php';
811

@@ -26,4 +29,11 @@
2629
Route::put('/users/{id}', [UserController::class, 'update']);
2730
Route::delete('/users/{id}', [UserController::class, 'destroy']);
2831

32+
33+
Route::get('/hello/{name}', function (Request $request) {
34+
$name = $request->params->name;
35+
echo ("Hello, $name");
36+
});
37+
38+
2939
$app->run();

0 commit comments

Comments
 (0)