API Authentication How to UseLaravel Passport for API Authentication

How to Use Laravel Passport for API Authentication

Aug 01, 2025 |

11 minutes read

API Authentication How to UseLaravel Passport for API Authentication

API Authentication with Laravel Passport: Secure, Scalable & Modern

Laravel Passport provides a comprehensive OAuth 2.0 server implementation for securing your API. It enables you to authenticate users using access tokens, supporting personal access tokens, password grants, and authorization codes. In this blog, we will explore how to set up Passport and secure your API routes with code-wise examples and explanations, making it an essential tool in laravel api development.

Integration with Laravel’s Eloquent ORM further streamlines complex authentication flows, while Passport’s flexibility ensures seamless compatibility with third-party clients, single-page applications (SPAs), and mobile app backends. For modern laravel api development, adopting Passport empowers engineering teams to build robust, production-ready APIs that meet today’s security and performance standards.

Why Laravel Needs Passport for API Authentication:

  1. API vs Web Authentication: APIs are stateless and use token-based authentication, unlike Laravel’s session-based web auth. Passport handles this with secure OAuth2 tokens.
  2. OAuth2 Support: Passport provides full OAuth2 server functionality like token issuance, refresh, and revocation — no need to build it manually.
  3. Mobile & SPA Friendly: For mobile and SPA apps, Passport offers secure token-based login via personal or password grant tokens.
  4. Easy Token Management: Passport simplifies token handling — generation, expiration, and revocation — for scalable API security.
  5. Third-Party API Access: Passport supports external apps with authorization code and client credentials grant types, ideal for public APIs.
  6. Secure Route Middleware: Use auth:api middleware from Passport to protect routes, ensuring only valid token requests are allowed.
  7. Seamless Laravel Integration: Passport works directly with Laravel’s core (Eloquent, Auth, Middleware), making token auth setup fast and developer-friendly.

Analyzing the Client’s Requirements

When building an API-driven application, understanding the client’s authentication needs is essential. If the client expects secure, token-based access for mobile apps, SPAs, or third-party integrations, Laravel Passport is the right choice. It supports stateless authentication using OAuth2, a widely accepted industry standard.

Laravel Passport offers complete OAuth2 functionality out of the box, including access token issuance, refresh tokens, and revocation. This removes the complexity of building token logic manually and ensures robust security. For frontend apps (like React or Vue) or mobile platforms, Passport’s personal access and password grant tokens allow users to authenticate securely without needing to manage sessions or cookies.

Furthermore, if the client wants third-party applications to access the API, Passport’s authorization code and client credentials grant types enable external services to authenticate just like they do with major platforms (e.g., Google APIs).

With built-in middleware (auth:api) and seamless integration into Laravel’s existing auth system, Passport aligns perfectly with most API-first requirements — making it an ideal solution for developers and clients alike.

Install Laravel Passport Package

First, ensure you have a Laravel project set up. Then install Laravel Passport via Composer:

API Authentication with Laravel

After installation, run the passport:install command to create the encryption keys and default clients used to generate access tokens.

API Authentication with Laravel 1

This will create the necessary tables like oauth_clients, oauth_access_tokens, oauth_refresh_tokens, etc.

Configure AuthServiceProvider

Open the AuthServiceProvider.php file and include the Passport service in the boot() method:

API Authentication with Laravel 2

You can also customize token expiration and scopes here if needed.

You may optionally set token expiration in AuthServiceProvider like this:

API Authentication with Laravel 3

Also, users can revoke tokens using $user->tokens()->delete() if you implement logout or session invalidation functionality.

Set Passport as API Auth Guard

Tell Laravel to use Passport for API authentication. In config/auth.php, under the guards array, update the api guard to use Passport:

API Authentication with Laravel 4

This ensures that API routes will authenticate using Passport tokens.

Add the HasApiTokens Trait to the User Model

In your User model (App\Models\User), add:

API Authentication with Laravel 5

Create API Routes

Define the public and protected API routes in routes/api.php:

API Authentication with Laravel 5

Here, /login is public, and /user is protected and requires a valid access token.

Create Authentication Controller

Now, create a controller to handle login and authenticated user data. Then implement the login and user methods:

API Authentication with Laravel 6

Log in to get an Access Token to proceed with further APIs

If your API uses email/password login to generate a personal access token (like shown in Laravel Passport setup), you can get the access token by requesting the login API.

Laravel API 1

Access a Protected Route with Bearer Token (cURL Example)

Use the token received in the login response to access any protected route:

Laravel API 2

Replace YOUR_ACCESS_TOKEN with the actual token from the login response.

Password Grant Token Request (OAuth2 Flow)

If you are using the password grant client (created via php artisan passport: install), then you can use:

Laravel API 3

Token Revocation: You can also manage tokens using Passport’s built-in tokens table.

API Authentication with Laravel

API Authentication: Build secure Laravel OAuth2 APIs

The Way Forward

Using Laravel Passport in Laravel 12 makes API authentication secure and efficient with full OAuth2 support. With just a few configuration changes and controller logic, you can implement token-based authentication for mobile apps, SPAs, and third-party services. Thanks to its seamless integration with Laravel authentication mechanisms, whether you’re building a simple API or a large-scale system, Passport provides the tools needed to manage secure access to your Laravel-powered backend.

Free Consultation

    Lopa Das

    With over 13 years of experience, Lopa Das is a seasoned professional at iFlair Web Technologies Pvt Ltd, specializing in web and mobile app development. Her technical expertise spans across Laravel, PHP, CodeIgniter, CakePHP, React, Vue.js, Nuxt.js, iOS, Android, Flutter, and React Native. Known for her exceptional skills in team handling, client communication, presales, and risk analysis, Lopa ensures seamless project execution from start to finish. Her proficiency in Laravel CRM, Next.js, and mobile app development makes her a valuable asset in delivering robust, scalable solutions.



    MAP_New

    Global Footprints

    Served clients across the globe from38+ countries

    iFlair Web Technologies
    Privacy Overview

    This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.