How to Use Laravel Passport for API Authentication Aug 01, 2025 | 11 minutes read 8 Likes API Authentication with Laravel Passport: Secure, Scalable & ModernLaravel 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: 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. OAuth2 Support: Passport provides full OAuth2 server functionality like token issuance, refresh, and revocation — no need to build it manually. Mobile & SPA Friendly: For mobile and SPA apps, Passport offers secure token-based login via personal or password grant tokens. Easy Token Management: Passport simplifies token handling — generation, expiration, and revocation — for scalable API security. Third-Party API Access: Passport supports external apps with authorization code and client credentials grant types, ideal for public APIs. Secure Route Middleware: Use auth:api middleware from Passport to protect routes, ensuring only valid token requests are allowed. 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 RequirementsWhen 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 PackageFirst, ensure you have a Laravel project set up. Then install Laravel Passport via Composer: After installation, run the passport:install command to create the encryption keys and default clients used to generate access tokens. This will create the necessary tables like oauth_clients, oauth_access_tokens, oauth_refresh_tokens, etc. Configure AuthServiceProviderOpen the AuthServiceProvider.php file and include the Passport service in the boot() method: You can also customize token expiration and scopes here if needed.You may optionally set token expiration in AuthServiceProvider like this: Also, users can revoke tokens using $user->tokens()->delete() if you implement logout or session invalidation functionality. Set Passport as API Auth GuardTell Laravel to use Passport for API authentication. In config/auth.php, under the guards array, update the api guard to use Passport: This ensures that API routes will authenticate using Passport tokens.Add the HasApiTokens Trait to the User ModelIn your User model (App\Models\User), add: Create API RoutesDefine the public and protected API routes in routes/api.php: Here, /login is public, and /user is protected and requires a valid access token. Create Authentication ControllerNow, create a controller to handle login and authenticated user data. Then implement the login and user methods: Log in to get an Access Token to proceed with further APIsIf 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. Access a Protected Route with Bearer Token (cURL Example)Use the token received in the login response to access any protected route: 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: Token Revocation: You can also manage tokens using Passport’s built-in tokens table. API Authentication: Build secure Laravel OAuth2 APIs Learn MoreThe Way ForwardUsing 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 DasAug 01 2025With 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.You may also like Implementing Real-Time Notifications in Laravel Using Laravel Echo & Pusher Read More Aug 01 2025 Excel and CSV Data Import/Export in Laravel Read More Jul 31 2025 Integrating Laravel Horizon: Real-Time Queue Monitoring for Laravel Apps Read More Jul 24 2025 Docupipe(Docupanda) API Integration in Laravel for Automated Document Upload and Verification Read More Jul 24 2025 An Easy Introduction to Working with LLMs in Laravel Read More Jul 21 2025 Laravel PEST: The Ultimate Testing Tool That’s Revolutionizing Web Development Read More Jul 21 2025