How to remove passport package from laravel application

Laravel Passport API authentication is one of the best API authentication packages but sometimes we are required to use other authentication systems like Sanctum or JWT after using Laravel. Then it required removing Laravel's passport completely.

We can directly change package but it will hold file memory and database table with it. Eventually, it will affect the application's performance. In some cases, it will randomly show errors.

In this tutorial, we will remove or uninstall Laravel Passport step by step. Here, we simply perform a reverse method of installation.

Remove Routes from Auth Service Provider

While setting up a passport, we have added a static method to add passport routes to our application into the AuthServiceProvider file. Let's remove this method and namespace for it.

Open app/Providers/AuthServiceProvider.php file and modify it as below :

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
//use Laravel\Passport\Passport; Remove this line

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
		// Comment below line
        // 'App\Models\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //Passport::routes(); Remove this line
    }
}

As you can see we have removed two lines first one is a namespace for Passport package and the second is routes method.

Remove package and components

In this step, we will remove passport package files using composer and manually delete resource or component file for passport.

Open your terminal and enter below command :

composer remove laravel/passport

It will take some to remove a package. After that, we need to remove published files for passport. Again enter below command or delete manually from file manager.

rm -r ./resources/js/components/passport
rm -r ./resources/views/vendor/passport

Those files are optional if you can't see them then ignore this step.

Update App.js file

In resources/js/app.js file, remove passport components registration. You may also find and remove these registered components if you used them somewhere:

grep -rn 'passport-authorized-clients'     resources/js/*
grep -rn 'passport-personal-access-tokens' resources/js/*
grep -rn 'passport-clients'                resources/js/*

Update Authentication Model

Here, we will remove namespace and HasApiTokens trait from our models. Open app/Models/User.php and remove below lines:

use Laravel\Passport\HasApiTokens;

use Notifiable; //remove HasApiTokens from this line

If you are using multiple guard authentication or another authentication model then perform same step for those models.

Remove OAuth keys

In this step, we will remove OAuth keys from our application storage. Remove it from file manager or enter below command in terminal :

rm storage/oauth-*.key

Change Api driver In Config File

In this step, we will change API guard's driver to a token. Open config/auth.php and change driver like below :

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
],

Remove migrations for Passport

As you know passport stores related data in database into OAuth tables. So we have to remove those tables from our database and from migration table too.

For this step, either you can refresh your entire database using artisan command or delete specific migrations.

To refresh entire database:

php artisan migrate:refresh

To remove specific migrations open terminal and enter into tinker terminal using below command :

php artisan tinker

Enter below commands one by one to remove migrations.

Schema::drop('oauth_access_tokens');
Schema::drop('oauth_auth_codes');
Schema::drop('oauth_clients');
Schema::drop('oauth_personal_access_clients');
Schema::drop('oauth_refresh_tokens');

DB::table('migrations')->where('migration', 'like', '%_oauth_access_tokens_table')->delete();
DB::table('migrations')->where('migration', 'like', '%_oauth_auth_codes_table')->delete();
DB::table('migrations')->where('migration', 'like', '%_oauth_clients_table')->delete();
DB::table('migrations')->where('migration', 'like', '%_oauth_personal_access_clients_table')->delete();
DB::table('migrations')->where('migration', 'like', '%_oauth_refresh_tokens_table')->delete();

Refresh Application

Let's refresh the application file using the below command. First of all, we will generate auto-load files for our packages using the dump-autoload command and then we will optimize resources. Lastly, we will generate a node package for our application.

composer dump-autoload
php artisan optimize:clear
npm run dev

Conclusion

Here, We just removed Laravel Passport package from the application. This process will completely remove Passport and its configuration or components permanently. It will ensure that our application runs properly.