Laravel Fortify is a powerful, frontend-agnostic authentication backend package for Laravel applications. It provides a robust set of features to handle user authentication, registration, password resets, and more, without dictating your frontend design. In this post, we'll explore how to set up Fortify and leverage its key features.
Getting Started with Laravel Fortify
First, install Fortify via Composer:
composer require laravel/fortify
Next, publish the Fortify configuration and migration files:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
Run the migrations to create the necessary database tables:
php artisan migrate
Configuring Fortify
In your config/app.php
file, add the Fortify service provider:
'providers' => [
// ...
App\Providers\FortifyServiceProvider::class,
],
Now, let's configure some basic Fortify features in the app/Providers/FortifyServiceProvider.php
file:
use Laravel\Fortify\Fortify;
public function boot()
{
Fortify::registerView(function () {
return view('auth.register');
});
Fortify::loginView(function () {
return view('auth.login');
});
Fortify::requestPasswordResetLinkView(function () {
return view('auth.forgot-password');
});
Fortify::resetPasswordView(function ($request) {
return view('auth.reset-password', ['request' => $request]);
});
}
Implementing Two-Factor Authentication
Fortify makes it easy to add two-factor authentication to your application. First, ensure the feature is enabled in your config/fortify.php
file:
'features' => [
Features::twoFactorAuthentication([
'confirmPassword' => true,
]),
// ...
],
Then, add the TwoFactorAuthenticatable
trait to your User model:
use Laravel\Fortify\TwoFactorAuthenticatable;
class User extends Authenticatable
{
use TwoFactorAuthenticatable;
// ...
}
Customizing Authentication Logic
You can customize the authentication logic by using the Fortify::authenticateUsing
method in your FortifyServiceProvider
:
use App\Models\User;
use Illuminate\Support\Facades\Hash;
Fortify::authenticateUsing(function ($request) {
$user = User::where('email', $request->email)->first();
if ($user &&
Hash::check($request->password, $user->password)) {
return $user;
}
});
Suggested Usages
SPA Authentication: Use Fortify as a backend for your Single Page Application, handling authentication via API requests.
Custom Login Flows: Implement multi-step login processes or social authentication by customizing the authentication logic.
Enhanced Security: Leverage Fortify's two-factor authentication and email verification features to boost your application's security.
Password Policies: Implement custom password validation rules to enforce strong passwords.
User Profile Management: Use Fortify's profile information update features to allow users to manage their account details.
Best Practices
Always customize the views to match your application's design.
Implement rate limiting on authentication attempts to prevent brute-force attacks.
Use Fortify's events to hook into the authentication process for logging or additional actions.
Regularly update Fortify to ensure you have the latest security patches.
Laravel Fortify provides a solid foundation for handling authentication in your Laravel applications. By leveraging its features and customizing them to fit your needs, you can quickly implement secure, robust authentication systems without reinventing the wheel.
Remember, Fortify is designed to be flexible, so don't hesitate to dive into its source code and customize it further if needed. Happy coding!