Laravel Passport

What is laravel passport?

In typical laravel application in order to authenticate you use laravel auth feature that allows you to login via forms however if you are working on rest api and you want to authenticate what will you do?

Rest api does not handle sessions and it authenticate via tokens. Laravel passport makes rest api authentication very easy if you are plan to use laravel as rest api.

How to install laravel passport?

First of all you need to have laravel application in order to install passport. Let us say that you are working on laravel project and you want to plan your user authentication for your api using laravel passport.

First of all using composer you need to install laravel passport library. Open your terminal window and run following command to bring laravel passport into your laravel project.

composer require laravel/passport​

Next, laravel passport needs to create some tables in order to import these new tables in your laravel application you need to run following command in your terminal.

php artisan migrate​

In addition, to create "personal access" and "password grant" clients which will be used to generate access tokens we need to run following command:

php artisan passport:install​

Note:  make sure to run above command otherwise you wont be able to generate tokens.

Next, we need to modify our user model so that we can have methods available from passport that help us deal with tokens.

Next, we enable passport by adding service provider. Open config/app.php file and add following line to providers array:

...
...
'providers' => [
        ...
        Laravel\Passport\PassportServiceProvider::class,
],
...
...

Open your user model and add make following changes:

<?php

namespace App;

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

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;
}

How to add laravel passport routes?

Next, we have to make our laravel app aware of new passport routes. To make this change open app/Providers/AuthServiceProvider file and make following changes:

<?php

namespace App\Providers;

use Laravel\Passport\Passport;
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()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}

Now, your laravel knows about new routes defined by passport library.

How to setup auth driver in laravel?

Once we have configured routes and user model we need to alter auth configuration file config/auth.php.

In this file we will replace basic api authentication with our passport authentication. Make following changes:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],
],

Alright, we are all set with installing passport in our laravel application.