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 the 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 the 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 the Passport package and the second is the routes method.
Remove package and components
In this step, we will remove passport package files using composer and manually delete resource or component files for a passport.
Open your terminal and enter the below command :
composer remove laravel/passport
It will take some to remove a package. After that, we need to remove published files for a passport. Again enter the below command or delete manually from file manager.
rm -r ./resources/js/components/passport
rm -r ./resources/views/vendor/passport
Update App.js file
In the 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 the namespace and HasApiTokens trait from our models. Open app/Models/User.php and remove the 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 the 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 the below command in the terminal :
rm storage/oauth-*.key
Change Api driver In Config File
In this step, we will change the API guard’s driver to a token. Open config/auth.php and change the 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 a database into OAuth tables. So we have to remove those tables from our database and from the migration table too.
For this step, you can refresh your entire database using the artisan command or delete specific migrations.
To refresh the entire database:
php artisan migrate:refresh
To remove specific migrations open a terminal and enter into the tinker terminal using the below command :
php artisan tinker
Enter the 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 the Laravel Passport package from the application. This process will completely remove the Passport and its configuration or components permanently. It will ensure that our application runs properly.