Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I integrate session controllers with other Fortify features


How can I integrate session controllers with other Fortify features


here's how you can integrate session controllers with other Fortify features:

1. Use Fortify's Login Controller:
- Fortify provides a built-in `AuthenticatedSessionController` that handles the login and logout functionality.
- You can customize this controller to integrate it with other Fortify features.

2. Customize the `AuthenticatedSessionController`:
- Create a new `AuthenticatedSessionController` in your `app/Http/Controllers/Auth` directory.
- Implement the `AuthenticatedSessionControllerContract` interface and override the necessary methods.

php
   namespace App\Http\Controllers\Auth;

   use App\Http\Controllers\Controller;
   use Illuminate\Http\Request;
   use Laravel\Fortify\Contracts\AuthenticatedSessionController as AuthenticatedSessionControllerContract;
   use Laravel\Fortify\Fortify;

   class AuthenticatedSessionController extends Controller implements AuthenticatedSessionControllerContract
   {
       public function store(Request $request)
       {
           // Customize the login logic here
           return Fortify::authenticateUsing(function (Request $request) {
               // Your custom authentication logic
           });
       }

       public function destroy(Request $request)
       {
           // Customize the logout logic here
           $request->user()->currentAccessToken()->delete();
           return response()->json(null, 204);
       }
   }
   

3. Register the Custom `AuthenticatedSessionController`:
- In your `FortifyServiceProvider`, bind the custom `AuthenticatedSessionController` to the `AuthenticatedSessionControllerContract`.

php
   public function boot()
   {
       $this->app->bind(
           AuthenticatedSessionControllerContract::class,
           AuthenticatedSessionController::class
       );
   }
   

4. Integrate with Other Fortify Features:
- Now that you have a custom `AuthenticatedSessionController`, you can integrate it with other Fortify features, such as registration, password reset, and two-factor authentication.
- For example, you can customize the registration process to automatically log in the user after successful registration.

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\Fortify\Fortify;

   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(),
           ])->validate();

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

           // Automatically log in the user after registration
           Fortify::login($user);

           return $user;
       }
   }
   

By customizing the `AuthenticatedSessionController` and integrating it with other Fortify features, you can create a seamless authentication experience in your Laravel application, leveraging the power of Fortify's backend implementation.

Remember to refer to the official Laravel Fortify documentation for more detailed information and examples on customizing the various Fortify controllers and features.

Citations:
[1] https://laracasts.com/discuss/channels/laravel/laravel-fortify-laravel-sanctum
[2] https://github.com/laravel/fortify/blob/1.x/src/Http/Controllers/RegisteredUserController.php
[3] https://github.com/laravel/fortify/actions/runs/8832562441/job/24250119062
[4] https://laravel.com/docs/11.x/fortify
[5] https://laracasts.com/discuss/channels/laravel/fortify-returning-view-after-login-although-views-false-set