jwt:refresh token

2023-03-18 hit count image

let's see how to refresh the issued token in jwt(Json Web Token) which is one of the token based authentication systems.

Outline

we will talk about how to refresh the issued token when user was login in jwt authentication system. this blog is a series. if you want to know how to install jwt middleware, signup, signin and get the user information, see our previous blogs.

Repository

we’ve made the repository of jwt authentication system. click below link to see our repository.

Development Environment

in here, we’ll use Laravel development environment created by Laradock and Ansible. if you want to know our environment, see our previous blog.

Modify Controller

open /app/Http/Controllers/JWTAuthController.php file in Laravel project folder and add below code.

public function refresh() {
    return $this->respondWithToken(Auth::guard('api')->refresh());
}

Modify Route

open /routes/api.php file for the url which is to refresh jwt token, and add below code.

...
Route::group(['middleware' => 'auth:api'], function(){
    ...
    Route::get('refresh', 'JWTAuthController@refresh')->name('api.jwt.refresh');
    ...
});
...

Test

let’s test jwt token refresh feature we’ve developed via Postman.

# URL
localhost/api/refresh
# header
Authorization
Bearer jwt_token

if jwt token is valid, jwt token is refreshed and issues new token like below screen.

refresh token

if jwt token is expired or previous jwt token, you can get 401 error response.

fail to refresh token

Completed

we’ve seen how to refresh jwt token which user got after login in jwt authentication system. at next blog post, we will introduce how to add logout feature which is last feature in jwt authentication system.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts