Laravel 10 REST API with Passport Authentication
In this guide, we’ll walk you through building a REST API in Laravel 10 with Passport authentication. You’ll learn how to set up Laravel Passport, create and authenticate users, and build secure API endpoints using standard CRUD operations.
Laravel Passport makes it easy to issue OAuth2 access tokens, perfect for mobile or SPA authentication.
Features Covered
-
Laravel 10 REST API setup
-
Authentication using Laravel Passport
-
User registration and login
-
Token generation and secure endpoints
-
Get the user list with pagination
Step 1: Install Laravel 10
Install Laravel using Composer:
Step 2: Install Laravel Passport
Install the Passport package:
Run the migrations and install Passport:
Step 3: Configure Auth Guards
Open config/auth.php
and configure the API guard to use Passport:
Step 4: Create the User Table
Create the migration:
Inside the migration file, define the schema:
Run the migration:
Step 5: Update User Model
In app/Models/User.php
, add the HasApiTokens
trait:
Step 6: Create API Routes
Edit routes/api.php
:
Step 7: Create Controllers
RegisterController
LoginController
UserManagementController
Step 8: API Testing Endpoints
Use tools like Postman or Insomnia to test the following endpoints:
Method | URL | Description |
---|---|---|
POST | /api/auth/register/save | Register user |
POST | /api/auth/login/push | Login user |
GET | /api/users/list/page?page=1 (with Bearer token) | Get the users' list |
Headers (for protected routes):
Conclusion
You’ve now successfully created a Laravel 10 REST API secured with Passport OAuth2 authentication. You can use this setup for mobile apps, SPA frontends, or third-party API consumers.