Create API Authentication With Laravel Passport
Learn how to setup and configure Laravel Passport for API Authentication and RESTful APIs in a Laravel application.
Join the DZone community and get the full member experience.Join For Free
In this tutorial, we will see how to use Laravel passport authentication in your Laravel application.
Let’s see how to setup and configure Laravel Passport for API authentication and RESTful APIs in a Laravel application.
Creating a New Project
You need to add Service Provider in the
config/app.php file. So, open the file and add the Service Provider in the
Migration and Installation
Set up database credentials in the
.env file. Run the migration command to migrate schemas to your database.
Next, it is required to install
passport using the command below. It will generate encryption keys required to generate secret access tokens.
Laravel\Passport\HasApiTokens trait to your
User model. It will provide few helper methods.
Passport::routes method in the boot method of your
AuthServiceProvider. It will generate necessary routes. This is how the
app/Providers/AuthServiceProvider.php will look like after changes.
config/auth.php file, set driver to the passport.
Add routes in the
Create Controller for Authentication
Create Passport Controller by running the following command.
Copy the contents below to
Now thar our logic is complete, let’s start testing. We will be testing it on PHP development server, but you can use virtual host if you want. Run the following command to serve the application on the PHP development server.
Now, let’s test our API endpoints using an API testing tool like Postman.
Published at DZone with permission of Razet Jain. See the original article here.
Opinions expressed by DZone contributors are their own.