Laravel 5.7 Passport API Authentication With Example

Generic placeholder image
Brijpal Sharma
laravel  

Date:-February 06th, 2019 , Posted On:- 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.

User.php

<?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;
}

 

Step: 4 Call Passport route

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

AuthServiceProvider.php

<?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()
    {
        $this->registerPolicies();

        Passport::routes();
    }
}

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

config/auth.php

'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 http://127.0.0.1:8000/api/user 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('http://your-app.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client-id',
        'client_secret' => 'client-secret',
        'username' => 'taylor@laravel.com',
        '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 http://127.0.0.1:8000/api/user 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.


About the author

user profile
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...

1 comments
Generic placeholder image
Samuel
June 15th, 2019 - 11:44PM

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.