Currently I have a login, register, update and delete functionality using my api made in Laravel using passport feature. Everything works fine the insertion of data and fetching the data from the database using this api. Now I wonder, how can I customize the response of the api when the token is expired. The expiration of token is working fine too. It automatically show this message
{ "message": "Unauthenticated" }
This is the code of routes where it is protected by my Oauth token where if the user did not login first then the user is not authenticated to browse the routes
Route::middleware('auth:api')->get('/user', function (Request $request){return $request->user();});
Route::post('/timekeeping','Auth\Api\AuthController@timekeeping');
Route::post('/login','Auth\Api\AuthController@login');
Route::middleware('auth:api')->group(function () {Route::post('/timekeeping_app','Auth\Api\AuthController@timekeeping_app');
Route::post('/logout','Auth\Api\AuthController@logout');
Route::post('/register','Auth\Api\AuthController@register');
Route::post('/show_dtr_list','Auth\Api\AuthController@show_dtr_list');
Route::post('/update','Auth\Api\AuthController@update');
Route::post('/delete','Auth\Api\AuthController@delete');
});
Then this is how I response whenever the user successfully logged in, registered, or even logged out their accounts.
return response(['status'=>'oK','message'=>'Successful!']);
What I want is when everytime the user is using the expired token. The api should response something like this
{ "message": "Token is expired" }
not just
{ "message": "Unathenticated" }
Some threads discussed that I need to overwrite some functionalities of laravel but I don't know where and how am I going to start.
My Answer:
To customize the response when the token is expired in Laravel Passport, you can create a custom middleware that checks for the token expiration and returns a custom response. Here's how you can do it:
1. Create a new middleware by running the following command in your terminal:
bash
php artisan make:middleware CheckTokenExpiration
2. Open the newly created middleware file located at `app/Http/Middleware/CheckTokenExpiration.php` and add the following code:
php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Auth\AuthenticationException;
class CheckTokenExpiration
{
public function handle($request, Closure $next)
{
if ($request->user() && $request->user()->token()->expires_at < now()) {
throw new AuthenticationException('Token is expired');
}
return $next($request);
}
}
3. Register the middleware in your `app/Http/Kernel.php` file under the `$routeMiddleware` array:
php
'checkTokenExpiration' => \App\Http\Middleware\CheckTokenExpiration::class,
4. Apply the middleware to the routes where you want to check for token expiration. You can do this by adding `'checkTokenExpiration'` to the middleware array in your routes file:
php
Route::middleware(['auth:api', 'checkTokenExpiration'])->get('/user', function (Request $request) {
return $request->user();
});
5. Now, when the token is expired, the custom response "Token is expired" will be returned instead of the default "Unauthenticated" message.
By following these steps, you can customize the response when the token is expired in Laravel Passport.
Rate this post
4 of 5 based on 6416 votesComments