From 0f151fa479aa4f449fd20b73a0fb4ee782fb5ed0 Mon Sep 17 00:00:00 2001 From: Hanif Dwy Putra S Date: Sat, 15 Apr 2023 14:03:45 +0000 Subject: [PATCH] feat: completed logout, and another fucking controller Signed-off-by: Hanif Dwy Putra S --- .../Controllers/Api/Auth/LogoutController.php | 15 +++++++++++++++ .../Controllers/Api/Auth/ProfileController.php | 13 +++++++++++++ routes/api.php | 8 +++++++- 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 app/Http/Controllers/Api/Auth/LogoutController.php diff --git a/app/Http/Controllers/Api/Auth/LogoutController.php b/app/Http/Controllers/Api/Auth/LogoutController.php new file mode 100644 index 0000000..d95dacf --- /dev/null +++ b/app/Http/Controllers/Api/Auth/LogoutController.php @@ -0,0 +1,15 @@ +logout(); + + return response()->json([ + 'data' => ['_' => 'OK'], + ]); + } +} +?> diff --git a/app/Http/Controllers/Api/Auth/ProfileController.php b/app/Http/Controllers/Api/Auth/ProfileController.php index d8930aa..1fe5370 100644 --- a/app/Http/Controllers/Api/Auth/ProfileController.php +++ b/app/Http/Controllers/Api/Auth/ProfileController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api\Auth; use App\Http\Controllers\ApiController; +use Carbon\Carbon; use Illuminate\Http\Request; class ProfileController extends ApiController @@ -14,4 +15,16 @@ public function self(Request $request) 'data' => $u, ], isset($u) ? 200 : 401); } + + public function refresh() + { + $new_token = auth()->refresh(true); + + return response()->json([ + 'data' => [ + 'token' => $new_token, + 'expires_at' => Carbon::now(config('APP_TIMEZONE'))->timestamp + auth()->factory()->getTTL() * 60, + ], + ]); + } } diff --git a/routes/api.php b/routes/api.php index acff0e0..8cb3dd1 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,12 +1,12 @@ middleware('guest'); + Route::get('logout', [LogoutController::class, 'logout'])->middleware([ + JwtLogged:: class, + ]); Route::get('profile', [ProfileController::class, 'self'])->middleware([ JwtLogged::class, ]); + Route::post('refresh', [ProfileController::class, 'refresh'])->middleware([ + JwtLogged::class, + ]); }); // /api/users