File tree Expand file tree Collapse file tree 4 files changed +52
-43
lines changed Expand file tree Collapse file tree 4 files changed +52
-43
lines changed Original file line number Diff line number Diff line change 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+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 33use App \Application ;
44use App \Routing \Route ;
55use App \Controllers \UserController ;
6+ use App \Http \Middleware \Auth ;
7+ use App \Http \Response ;
8+ use App \Http \Request ;
69
710require __DIR__ . '/../vendor/autoload.php ' ;
811
2629Route::put ('/users/{id} ' , [UserController::class, 'update ' ]);
2730Route::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 ();
You can’t perform that action at this time.
0 commit comments