implement laravel fortify for authentication
Introduction
Laravel Fortify is a powerful, flexible, and customizable authentication package for Laravel applications. It provides secure user authentication and registration, as well as password reset and email verification functionalities. This tutorial will guide you through the process of integrating Laravel Fortify into your Laravel application.
Step 1: Install Laravel Fortify
To begin, install Laravel Fortify using composer by running the following command:
composer require laravel/fortify
Step 2: Publish the Fortify Service Provider
Next, publish the Fortify Service Provider using the following command:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
This will copy necessary files to your application's configuration and actions directories.
Step 3: Register the Fortify Service Provider
Open the config/app.php file and register the FortifyServiceProvider by adding it to the providers array:
App\Providers\FortifyServiceProvider::class,
Step 4: Check Routes
After registering the provider, you can run php artisan route:list to see the available routes for authentication, registration, and password reset functionali
php artisan route:list
Step 5: Create Views
Create the necessary views for login, registration, and password reset. ie registration.blade.php
<form method="POST" action="{{ route('register') }}">
@csrf
<input type="text" class="form-control" id="name" placeholder="Your Name">
<input type="Submit" value="submit">
</form>
Step 6: Customize Fortify Routes in the FortifyServiceProvider
Open the app\Providers\FortifyServiceProvider.php file and customize the routes by defining the login and register views inside the boot method:
public function boot(): void
{
Fortify::loginView("login");
Fortify::registerView("register");
// ... other code
}
Step 8: Save Data to Database
Laravel Fortify handles saving user data to the database. The controller responsible for creating new users is located in
app\Actions\Fortify\CreateNewUser.php.
public function create(array $input): User
{
dd($input);
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
])->validate();
return User::create([
'name' => $input['name'],
]);
}
Conclusion:
Laravel Fortify provides a robust and customizable authentication solution for your Laravel applications. By following these steps, you can easily integrate Laravel Fortify into your project and take advantage of its powerful features for user authentication and management.