Laravel 8 Passport API Authentication Tutorial
This tutorial will guide you through setting up API authentication in a Laravel 8 application. We'll create a simple API with endpoints for user registration, login, and retrieving the authenticated user's information.
Prerequisites
-
Basic knowledge of Laravel
-
Familiarity with PHP and the command line
Steps Overview
-
Setting up the API
-
Implementing the Authentication
-
Using the API
Step 1: Install Laravel 8
composer create-project --prefer-dist laravel/laravel blog
Step 2: Install Passport
composer require laravel/passport
php artisan migrate
php artisan passport:install
Step 3: Configure Passport
app/Models/User.php
use Laravel\Passport\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
...
}
config/auth.php
'guards' => [
'api' => [
'driver' => 'passport',
'provider' => 'users',
],
],
Step 4: Create Product Table and Model
php artisan make:migration create_products_table
php artisan migrate
Migration Example
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('detail');
$table->timestamps();
});
Product Model (app/Models/Product.php)
class Product extends Model
{
use HasFactory;
protected $fillable = ['name', 'detail'];
}
Step 5: Create API Routes
routes/api.php
Route::post('register', [RegisterController::class, 'register']);
Route::post('login', [RegisterController::class, 'login']);
Route::middleware('auth:api')->group(function () {
Route::resource('products', ProductController::class);
});
Step 6: Create Controller Files
app/Http/Controllers/API/BaseController.php
public function sendResponse($result, $message) { ... }
public function sendError($error, $errorMessages = [], $code = 404) { ... }
app/Http/Controllers/API/RegisterController.php
public function register(Request $request) { ... }
public function login(Request $request) { ... }
app/Http/Controllers/API/ProductController.php
public function index() { ... }
public function store(Request $request) { ... }
public function show($id) { ... }
public function update(Request $request, Product $product) { ... }
public function destroy(Product $product) { ... }
Step 7: Create API Resource
php artisan make:resource Product
app/Http/Resources/Product.php
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'detail' => $this->detail,
'created_at' => $this->created_at->format('d/m/Y'),
'updated_at' => $this->updated_at->format('d/m/Y'),
];
}
Step 8: Run the Server
php artisan serve
API Headers
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$accessToken,
]
API Endpoints Summary
Method | Endpoint | Description |
---|---|---|
POST | /api/register | Register a new user |
POST | /api/login | Login a user |
GET | /api/products | List all products |
POST | /api/products | Create a product |
GET | /api/products/{id} | Get product details |
PUT | /api/products/{id} | Update a product |
DELETE | /api/products/{id} | Delete a product |
Conclusion
This tutorial has shown you how to implement a basic API authentication system in a Laravel 8 application using Passport. From here, you can expand your API by adding features like password resets, email verification, and role-based permissions.