Laravel Fortify
What is Laravel Fortify?
Laravel Fortify is a backend authentication framework for Laravel applications that handles common user authentication tasks such as login, registration, password reset, and email verification. It does not include front-end scaffolding, making it suitable for SPA or custom UI integrations.
Origin
Fortify was introduced to provide a backend-only authentication solution for developers who want to build custom front-end interfaces for their applications.
Why is Laravel Fortify Used?
- Customizable Authentication: Enables developers to implement their own UI while managing backend authentication.
- Comprehensive Features: Includes authentication features like two-factor authentication and password resets.
- Decouples Backend and Frontend: Works seamlessly with SPAs and API-driven applications.
Best Practices
- Secure API Endpoints: Use middleware to protect Fortify routes.
- Customize as Needed: Modify Fortify's actions and configuration to suit your application.
- Use Front-End Frameworks: Combine Fortify with Vue or React for robust authentication workflows.
Example in Action
Install Fortify:
composer require laravel/fortify
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
Enable Fortify in config/app.php:
'providers' => [
Laravel\Fortify\FortifyServiceProvider::class,
];
Customize authentication logic in FortifyServiceProvider:
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->email)->first();
if ($user && Hash::check($request->password, $user->password)) {
return $user;
}
});