Categories: Blog

Create API Rest with Laravel Passport Authentication

Laravel Passport is an OAuth2 server and API authentication package that is simple and enjoyable to use.

If your application absolutely needs to support OAuth2, then you should use Laravel Passport.

Laravel Passport provides a full 0Auth2 server implementation. With it, you can easily generate a personal access token to uniquely identify a currently authenticated user.

1. Installation

For installing this package Run this command from the Terminal:

composer require laravel/passport

Once the installation is complete, passport migrations will create the tables your application needs to store clients and access tokens.Run the following command to migrate your database:

php artisan migrate

2. Generate keys

This command will create the encryption keys needed to generate secure access tokens.

php artisan passport:install

After the installation process, add the Laravel\Passport\HasApiTokens trait to your App\User model as shown here:

Now, to register the routes necessary to issue and revoke access tokens, call the Passport::routes method within the boot method of your AuthServiceProvider.

To do this, open the app/Providers/AuthServiceProvider file and update its content as shown below:

Finally, open config\auth.php and set the driver of API to passport in the guards array shown below.

3. Create API Routes

Laravel provides routes/api.php file for writing web services routes. So, add a new route on that file.

4. Create API Controller

Create function in your controller file

  • Login function
  • Logout function

5. Test API in Postman

Add Request data in postman for Login

The API will return a token, which we will use in further requests as our means to authentication.

For logout using this token In Postman, the “Authorization” tab has a drop-down where the type can be set to “Bearer Token,” after which the token can go into the token field.

And you will successfully log out.

Official Documentation

For more information and details you can read the official document from Laravel website.

Lets Nurture

Share
Published by
Lets Nurture

Recent Posts

Best AI Web Design Tools In 2025 To Build Your Dream Website Faster

Web design was once an element of online brand-building that you needed to know how…

2 months ago

AI In Finance: How Machine Learning Is Transforming Banking

Financial services and banking are being reshaped and revolutionized by what artificial intelligence has made…

2 months ago

Can I Add Google Maps To My Android App: A Step-By-Step Integration Guide

Yes, you can add Google Maps to your Android app, providing seamless, interactive mapping for…

2 months ago

Future-Proof Your SEO: Top Digital Marketing Trends You Need To Leverage In 2025

SEO and digital marketing are constantly evolving. It happens slowly but quickly at the same…

2 months ago

The Ethics of AI: Balancing Innovation with Responsibility

As an AI development team, we regularly encounter scenarios and questions from clients regarding AI…

2 months ago

How AI-Powered Predictive Analytics Is Shaping Business Decisions

Predictive analytics is your ultimate guide to data-backed business strategy. Data-driven insights, proactive decision-making, and…

2 months ago