Laravel Token Based Authentication

Laravel token based Authentication

To work with laravel Token based authentication you should installed Dingo and JWT.

How to install Dingo :

How to install JWT :

Here we are going to see how to install oauth2 package and Token based authentication.

Step 1 : Add oauth2 pacjage in composer.json and run composer update

Step 2 : Add Provider in config/app.php

 Step 3 : Add $middleware and $routeMiddleware in your app/Http/Kernal.php

 Step 4 : Publish vendor by running the below command.

Result Will be :

Copied File [/vendor/lucadegasperi/oauth2-server-laravel/config/oauth2.php] To [/config/oauth2.php]
Copied Directory [/vendor/lucadegasperi/oauth2-server-laravel/database/migrations] To [/database/migrations]
Copied File [/vendor/anahkiasen/former/src/config/former.php] To [/config/former.php]
Publishing complete for tag []!

It will create outh2.php file under config folder.
Will create migrations file under database folder

Step 5 : Run migration to install oauth table

Result :
Migrated: 2014_04_24_110151_create_oauth_scopes_table
Migrated: 2014_04_24_110304_create_oauth_grants_table
Migrated: 2014_04_24_110403_create_oauth_grant_scopes_table
Migrated: 2014_04_24_110459_create_oauth_clients_table
Migrated: 2014_04_24_110557_create_oauth_client_endpoints_table
Migrated: 2014_04_24_110705_create_oauth_client_scopes_table
Migrated: 2014_04_24_110817_create_oauth_client_grants_table
Migrated: 2014_04_24_111002_create_oauth_sessions_table
Migrated: 2014_04_24_111109_create_oauth_session_scopes_table
Migrated: 2014_04_24_111254_create_oauth_auth_codes_table
Migrated: 2014_04_24_111403_create_oauth_auth_code_scopes_table
Migrated: 2014_04_24_111518_create_oauth_access_tokens_table
Migrated: 2014_04_24_111657_create_oauth_access_token_scopes_table
Migrated: 2014_04_24_111810_create_oauth_refresh_tokens_table

Step 6 : Create OAuth Service provider

Run below Command to create Service Provider

It will create OAuthServiceProvider.php file in app/Providers/OAuthServiceProvider.php 

Change your app/Providers/OAuthServiceProvider.php file like below

 Step 7 : Add your service Provider in config/app.php provider array.

and Run composer dump-autoload in your terminal to autoload service provider.

Step 8 :  Add grant_type with in the array with in the oauth2.php

And change ‘access_token_ttl’ value to ‘access_token_ttl’ => 604800, //As it is in the grant_type array.

 Step 9 : Create client_id and client_secret to your database.

You can see oauth_clients table in your database there you want to add your client instances.

create model :

It will create the model inside your App folder


 Step 10 : Create seeder to insert instances

It will create a OAuthClientTableSeeder.php file under seeder folder and modify like below.


Step 11 : Feed seeder.

 Step 12 : Create UserVerifier.php

we have callback in our oauth2.php grant_type array
‘callback’ => ‘App\Verifiers\UserVerifier@verify’,

Create a folder called Verifiers inside App folder.
Create UserVerifier.php file like below

Step 13 : Create a route to issue Authorize token

Goto Postman to check how api works

By clicking send button you will receive new token,

“access_token”: “eBmSfnxuiJ3iYH7kTs2wKBu4rAExcp2dEPJTMNXT”,
“token_type”: “Bearer”,
“expires_in”: 604800,
“refresh_token”: “ld9cVZUBdtssUQaaLWbFggwEDrdYFSiaGfiViFYf”

You can use this token for your further request.




Step 14 : Create Token Based authentication

Create Route like below to check token based authentication.

Inside my HomeController I have function called users. It wil looks like below :

 Step 15 : Goto Postman to check how api works

1. Select method : GET
2. Enter URL :
3. Select Body

Provide below key and value

Your Authorization header should be Bearer <token> // which is get by using

By clicking send button you will receive all the users,

“users”: [
“id”: 1,
“name”: “Marimuthu”,
“email”: “”,
“created_at”: “2016-06-28 07:48:46”,
“updated_at”: “2016-06-28 09:36:08”
“id”: 2,
“name”: “Marimuthu”,
“email”: “”,
“created_at”: “2016-06-28 07:49:41”,
“updated_at”: “2016-06-28 07:53:40”





Thats it now you have successfully installed Token Based authentication.
Thanks for reading.
If you like this article don’t forget to share and comment.