Skip to content

Commit

Permalink
jwt exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorOlikov committed Apr 3, 2024
1 parent 87571d4 commit 64b2f33
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
33 changes: 33 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Request;
use Throwable;
use Tymon\JWTAuth\Exceptions\InvalidClaimException;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\PayloadException;
use Tymon\JWTAuth\Exceptions\TokenBlacklistedException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;

class Handler extends ExceptionHandler
{
Expand All @@ -26,5 +33,31 @@ public function register(): void
$this->reportable(function (Throwable $e) {
//
});

$this->renderable(function (TokenExpiredException $e, Request $request) {
return response()->json(['message' => 'Token has expired'], 401);
});

$this->renderable(function (TokenBlacklistedException $e, Request $request) {
return response()->json(['message' => 'Token in blacklist'], 400);
});

$this->renderable(function (TokenInvalidException $e, Request $request) {
return response()->json(['message' => 'Invalid token, could not decode token from JSON'], 400);
});

$this->renderable(function (InvalidClaimException $e, Request $request) {
return response()->json(['message' => 'Invalid token, invalid token claim'], 400);
});

$this->renderable(function (PayloadException $e, Request $request) {
return response()->json(['message' => 'Invalid token, invalid token payload'], 400);
});

$this->renderable(function (JWTException $e, Request $request) {
return response()->json(['message' => 'Token could not be parsed from the request'], 400);
});


}
}
4 changes: 3 additions & 1 deletion resources/js/src/Axios/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ axiosJwtApi.interceptors.response.use((response) => {
const authStore = useAuthStore();
console.log(error)
const originalRequest = error.config
if (error.response.status === 500 && !originalRequest._retry) {
if (error.response.status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
try {
const response = await axios.post(`http://localhost/api/v1/auth/refresh-tokens`,null, {
Expand All @@ -37,6 +37,8 @@ axiosJwtApi.interceptors.response.use((response) => {

}
return axiosJwtApi(originalRequest);
} else if (error.response.status === 401){
//logout
}
})

Expand Down

0 comments on commit 64b2f33

Please sign in to comment.