How to Use Laravel Fortify for Authentication
To use
Laravel Fortify, first install it via Composer, then publish its configuration and views. Next, enable desired features like login and registration in the fortify.php config file, and finally register Fortify's routes in your RouteServiceProvider or routes/web.php.Syntax
Laravel Fortify is a backend authentication package that provides routes and controllers for login, registration, password reset, and more. You install it via Composer, publish its config, and enable features in config/fortify.php. You then register Fortify's routes in your app.
composer require laravel/fortify: installs Fortifyphp artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider": publishes config and viewsFortify::loginView(): defines custom login viewFortify::registerView(): defines custom register viewconfig/fortify.php: enable features like login, registration, password reset
bash and php
composer require laravel/fortify php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider" // In App\Providers\FortifyServiceProvider.php use Laravel\Fortify\Fortify; public function boot() { Fortify::loginView(fn() => view('auth.login')); Fortify::registerView(fn() => view('auth.register')); } // In config/fortify.php 'features' => [ Features::registration(), Features::resetPasswords(), Features::emailVerification(), Features::updateProfileInformation(), Features::updatePasswords(), Features::twoFactorAuthentication(), ], // In routes/web.php or RouteServiceProvider Fortify::routes();
Example
This example shows how to install Laravel Fortify, publish its resources, enable registration and login features, and define custom views for login and registration.
php
<?php // Run in terminal // 1. Install Fortify // composer require laravel/fortify // 2. Publish config and views // php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider" // 3. Enable features in config/fortify.php return [ 'features' => [ \Laravel\Fortify\Features::registration(), \Laravel\Fortify\Features::resetPasswords(), ], ]; // 4. Define views in App\Providers\FortifyServiceProvider.php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Laravel\Fortify\Fortify; class FortifyServiceProvider extends ServiceProvider { public function boot() { Fortify::loginView(fn() => view('auth.login')); Fortify::registerView(fn() => view('auth.register')); } } // 5. Register Fortify routes in routes/web.php use Laravel\Fortify\Fortify; Fortify::routes();
Output
When visiting /login or /register, you see the respective forms handled by Fortify with backend authentication logic.
Common Pitfalls
- Not publishing Fortify's config and views causes missing files errors.
- Forgetting to register Fortify routes means login and registration URLs won't work.
- Not enabling features in
config/fortify.phpdisables those features silently. - Not defining custom views or using default views without publishing causes view not found errors.
- Not running migrations for users table or password resets causes database errors.
php
<?php // Wrong: Forgetting to register routes // routes/web.php // No Fortify::routes(); means no auth routes // Right: Register routes use Laravel\Fortify\Fortify; Fortify::routes();
Quick Reference
Summary tips for using Laravel Fortify:
- Install via Composer and publish resources.
- Enable needed features in
config/fortify.php. - Define custom views in
FortifyServiceProvider. - Register Fortify routes in your app.
- Run migrations for user authentication tables.
Key Takeaways
Install Laravel Fortify via Composer and publish its config and views before use.
Enable authentication features like registration and login in the fortify config file.
Define custom views for login and registration in FortifyServiceProvider.
Always register Fortify routes to activate authentication endpoints.
Run database migrations to support user authentication tables.