Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel 11 fortify example


Laravel 11 fortify example


here's an example of how to use Laravel Fortify in a Laravel 11 application:

1. Install Laravel Fortify:
- Require the Fortify package using Composer: `composer require laravel/fortify`
- Publish the Fortify configuration and views: `php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"`

2. Configure Fortify:
- Open the `config/fortify.php` file and customize the settings as per your requirements.
- For example, you can enable the registration feature by setting `'features' => [Fortify\Features::registration()]`.

3. Implement the Registration Feature:
- In your `FortifyServiceProvider`, define the registration view:

php
   use Laravel\Fortify\Fortify;

   public function boot()
   {
       Fortify::registerView(function () {
           return view('auth.register');
       });
   }
   

- Create the `auth.register` view and include a form that makes a POST request to the `/register` endpoint.

4. Customize the Registration Process:
- Create a `CreateNewUser` action in your `app/Actions/Fortify` directory.
- Implement the `create` method to handle the user validation and creation logic.

php
   namespace App\Actions\Fortify;

   use App\Models\User;
   use Illuminate\Support\Facades\Hash;
   use Illuminate\Support\Facades\Validator;
   use Laravel\Fortify\Contracts\CreatesNewUsers;
   use Laravel\Jetstream\Jetstream;

   class CreateNewUser implements CreatesNewUsers
   {
       use PasswordValidationRules;

       public function create(array $input)
       {
           Validator::make($input, [
               'name' => ['required', 'string', 'max:255'],
               'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
               'password' => $this->passwordRules(),
               'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
           ])->validate();

           return User::create([
               'name' => $input['name'],
               'email' => $input['email'],
               'password' => Hash::make($input['password']),
           ]);
       }
   }
   

5. Implement the Login Feature:
- Define the login view in your `FortifyServiceProvider`:

php
   Fortify::loginView(function () {
       return view('auth.login');
   });
   

- Create the `auth.login` view and include a form that makes a POST request to the `/login` endpoint.

6. Customize the Login Process:
- Create a `LoginResponse` class in your `app/Http/Responses/Fortify` directory.
- Implement the `toResponse` method to customize the login response.

php
   namespace App\Http\Responses\Fortify;

   use Illuminate\Http\JsonResponse;
   use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

   class LoginResponse implements LoginResponseContract
   {
       public function toResponse($request)
       {
           // Customize the login response here
           return new JsonResponse([
               'message' => 'Login successful',
               'user' => $request->user()->only('id', 'name', 'email'),
               'token' => $request->user()->createToken(Fortify::$guard)->plainTextToken,
           ]);
       }
   }
   

- Register the custom `LoginResponse` class in your `FortifyServiceProvider`:

php
   $this->app->bind(LoginResponseContract::class, LoginResponse::class);
   

This is a basic example of how to use Laravel Fortify in a Laravel 11 application. You can further customize the registration, login, and other Fortify features to fit your application's requirements.

Remember to refer to the official Laravel Fortify documentation for more detailed information and advanced usage examples.

Citations:
[1] https://github.com/qirolab/laravel-fortify-example
[2] https://laravel.com/docs/11.x/fortify
[3] https://github.com/laravel/fortify/actions/runs/8832562441/job/24250119062
[4] https://github.com/laravel/fortify/blob/1.x/src/Http/Controllers/RegisteredUserController.php
[5] https://www.youtube.com/watch?v=CLsyHP7x0N0