Laravel 5.7 Passport API Authentication With Example

Brijpal Sharma
laravel   Publish on:-February 06th, 2019 , Category:- Web Development

Today topic is Laravel passport API authentication with an example.

We all this stuff do in fresh Laravel Application you can add this in your running application. so let's start.

Step:1 Install Laravel Application 

(If you are using the old application you can skip this step.)

For creating a new Laravel application run this following command in your CMD.

laravel new passport

After creating application run this command for using default auth of Laravel application.

php artisan make:auth


Step: 2 Install Package

Install Passport via the Composer package manager using this following command.

composer require laravel/passport

The Passport service provider some migration table with the framework, so we should migrate your database after installing the package using this following command.

php artisan migrate

Now, needed to generate secure access tokens so we should run the command. passport:install. this command will create "personal access" and "password grant" clients which will be used to generate access tokens:

php artisan passport:install


Step: 3 Add HasApiTokens in User.php

This will provide some helper methods to your model which allow inspecting the authenticated user's token and scopes.



namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
    use HasApiTokens, Notifiable;


Step: 4 Call Passport route

Next, we need to call the passport route into App/Providers/AuthServiceProvider.php



namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
     * The policy mappings for the application.
     * @var array
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',

     * Register any authentication / authorization services.
     * @return void
    public function boot()


Now, config auth to tell Laravel to use the passport as a driver.


'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',

    'api' => [
        'driver' => 'passport',
        'provider' => 'users',

Now open Postman or other, and let's try a test in Postman enter this URL // and sent a get request.

Laravel 5.7 Passport API Authentication With Example

It's will give an error, So in order to access we need to a token first, we should register after successful

login it's will return a token. So, the first login.

we use this key and value to log in.

$response = $http->post('//', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => '[email protected]',
        'password' => 'my-password',
        'scope' => '',

You can get client_id and client_secret from oauth_clients  table which is generated by the command and username and password from the user's table.


Laravel 5.7 Passport API Authentication With Example

When you use the right detail in Postman we will get access_token.  Now we can access the protected resource.

Now pass one other parameter Authorization into // to get access user details. Copy above access_token to pass Authorization value.

Before access_token you should must be added Bearer as below.

Laravel 5.7 Passport API Authentication With Example

Now, we can see we are able to access protected data using access_token and it's work properly.

So, we finally completed tutorial Laravel Passport API Authentication With Example.

Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...


Generic placeholder image

Publish on:-June 15th, 2019
Its about 1:43 AM here after 6 Hours searching i found you. Many thanks for this article

You must be logged in to post a comment.