passport install in laravel

In this tutorial passport install in laravel php and few easy step follow and your passport middelware running succesfully passport install in laravel

Step 1: Install Laravel project

First step, we require to get New(FRESH) Laravel application using bellow command, So open your terminal or command prompt and run bellow command passport install in laravel:

composer create-project --prefer-dist laravel/laravel passport_ware

Step 2.Install Laravel Passport packege

Laravel Passport provides a full (OAuth2) server implementation for your Laravel application in a matter of few minutes wait.

composer require laravel/passport

Step 3. Run Migration

The Passport migrations will create the tables your application needs to store to your database clients and access tokens.

php artisan migrate

Step 4. Generate keys

This command use and will create the encryption keys needed to generate secure access tokens. In addition, the command will create “personal access” and “password grant permission” clients which will be used to generate access tokens your env. @DATABASE:

php artisan passport:install

After running this command, add the Laravel\Passport\HasApiTokens trait to your App\Usermodel. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user’s token and scopes:

Step 5. Model/User.php

<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable, HasApiTokens;
}

Step 6: 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();
    }
}

Step 7: Finally, in your config/auth.php configuration file Chnage Only passport

Step 6. Create API Routes in api.php

We will create api routes. Laravel file routes/api.php file for write web services route. So, Finally add new route on this file @This is right way

Just Add on your Controller array this @2 line

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

$data = array(
              'device_token' => $device_token,
              'device_type' => $device_type,
              'device_id' => $device_id,
              'user_id' => $user_id,
              );

$user1 = User::where('id', $user_id)->first();

$array_merge = array_merge($user1->toArray(), $data);

$success = $array_merge;

$success['user_token'] = $user->createToken('signup')->accessToken;

return $this->sendResponse(1, 'Registration successfully', $success);

Now last Quick Run

php artisan serve

1 thought on “passport install in laravel”

Leave a Comment