Lookup invalidate usage.
Oct 12, 2023
5
Level 1
JWT logout method in laravel
Is it advisable to create a logout method in the backend when using JWT with eloquent? I say this because what I have been implementing is removing the token on the frontend to log out but we do not consume anything in Laravel to log out.
Should we implement something like this?
public function logout () {
$user = Auth::user()->token();
$user->revoke();
}
This is the login controller:
public function login(LoginRequest $request): JsonResponse
{
try {
$validated = $request->validated();
$captcha=$this->userService->ReCaptcha($validated['captcha']);
if ($captcha){
unset($validated['captcha']);
if (Auth::attempt($validated)) {
$user = Auth::user();
if ($user->email_verified_at == null) {
return response()->json([
'status' => false,
'message' => 'email validation still pending'
], 400);
} else {
$column_name = "USER_ID";
$column_value = $user->id;
$table = "USER_AUTHENTICATION";
$user_auth = $this->genericRepository->getRow($table, $column_name, $column_value);
if (!empty($user_auth)) {
$token = $user->createToken('user_token', [$user_auth->AUTH_LEVEL])->accessToken;
$timestamp = now()->addDays(1);
$expires_at = date('M d, Y H:i A', strtotime($timestamp));
return response()->json([
'status' => true,
'message' => 'Login successful',
'access_token' => $token,
'token_type' => 'bearer',
'expires_at' => $expires_at,
]);
} else {
return $this->errorService->databaseReadError();
}
}
} else {
return $this->errorService->invalidCredentials();
}
}else{
return $this->errorService->badCaptcha();
}
} catch (Exception $e) {
return response()->json([
'status' => false,
'message' => $e->getMessage()
], 500);
}
}
Please or to participate in this conversation.