jwt installation&settings

2020-12-16 hit count image

introduce how to install and configure jwt(Json Web Token) Middleware to use token based authentication system on Laravel.

Outline

we try to use jwt(Json Web token) to make token based authentication system on Laravel. if we implement everything of jwt authentication system, it’s hard and takes long time. so we decided to use tymon/jwt-auth middleware to develop jwt authentication system.

in this blog, we’ll see how to install and configure tymon/jwt-auth. if you want to more detail about tymon/jwt-auth, see below links.

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.

Install Middleware

execute below composer command to install tymon/jwt-auth middleware for jwt authentication system.

# vagrant ssh
# sudo docker exec -it laradock_workspace_1 bash
composer require tymon/jwt-auth

Configure Provider

open config/app.php file in Laravel project folder and add Provider like below.

...
'providers' => [
    ...
    Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
    ...
]
...

after configuration, execute below artisan command to apply the middleware.

# vagrant ssh
# sudo docker exec -it laradock_workspace_1 bash
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

if you get the error like below,

In ProviderRepository.php line 208:
Class 'Tymon\JWTAuth\Providers\LaravelServiceProvider' not found

you should update tymon/jwt-auth version. modify composer.json file like below.

...
"require": {
    ...
    "tymon/jwt-auth": "^1.0.0-rc.3"
    ...
},
...

after modifying the version, execute below composer command to update it.

composer update

and execute artisan command again to apply the middleware.

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"

Create Secret Key

we need to create secret key which tymon/jwt-auth middleware uses.

php artisan jwt:secret

check below exists at the bottom of .env file

JWT_SECRET=*******

Completed

we saw how to install and configure tymon/jwt-auth middleware for jwt(Json Web Token) based authentication system. next blog, we’ll introduce how to use jwt to develop authentication system.

Reference

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