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 :
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 :
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.
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:
Update Authentication Model
Here, we will remove namespace and HasApiTokens trait from our models. Open app/Models/User.php and remove below lines:
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 :
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 :
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:
To remove specific migrations open terminal and enter into tinker terminal using below command :
Enter below commands one by one to remove migrations.
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.
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.