From ea4f44e4f47f7dc5744ff97a44c2287eb5d179aa Mon Sep 17 00:00:00 2001 From: mahdihty Date: Sun, 16 May 2021 14:07:44 +0430 Subject: [PATCH] add logout and revoke --- app/Http/Controllers/AuthController.php | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index a9de273..7436579 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -4,11 +4,13 @@ namespace App\Http\Controllers; use App\Http\Resources\UserResource; use App\Models\Business; +use App\Models\Fingerprint; use App\Models\User; use App\Notifications\DBNotification; use App\Notifications\MailNotification; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; +use Illuminate\Session\TokenMismatchException; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Hash; @@ -443,4 +445,50 @@ class AuthController extends Controller return $this->authWithInfo(); } + /** + * @param Request $request + * @return mixed + * @throws TokenMismatchException + */ + public function logout(Request $request) + { + $token = $request->bearerToken(); + + if (blank($token)) { + return new JsonResponse([ + 'message' => 'Not authorized request.', + 'status' => Response::HTTP_UNAUTHORIZED + ]); + } + + /** @var Fingerprint $token */ + $token = Auth::user()->fingerprints()->firstWhere([ + 'token' => $token, + ]); + + if ($token) { + return $token->delete(); + } + + throw new TokenMismatchException('Invalid token!'); + } + + /** + * @param string $token + * @throws TokenMismatchException + */ + public function revoke(string $token) + { + /** @var Fingerprint $token */ + $token = Fingerprint::firstWhere([ + 'token' => $token, + ]); + + if ($token) { + return $token->delete(); + } + + throw new TokenMismatchException(); + } + }