Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Laravel fortify http controllers authenticated sessioncontroller


Laravel fortify http controllers authenticated sessioncontroller


here's an overview of the `AuthenticatedSessionController` in Laravel Fortify:

1. Location: The `AuthenticatedSessionController` is located in the `Laravel\Fortify\Http\Controllers` namespace.

2. Purpose: The `AuthenticatedSessionController` is responsible for handling the login and logout functionality in a Laravel Fortify-powered application.

3. Key Methods:
- `store()`: This method handles the login process. It validates the user's credentials and logs the user in if the credentials are valid.
- `destroy()`: This method handles the logout process. It logs the user out and invalidates the user's session.

4. Customization:
- To customize the login and logout functionality, you can create a custom `AuthenticatedSessionController` in your application and bind it to the `AuthenticatedSessionController` contract in your `FortifyServiceProvider`.

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);
       }
   }
   

- In your `FortifyServiceProvider`, bind the custom `AuthenticatedSessionController` to the `AuthenticatedSessionControllerContract`:

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

By customizing the `AuthenticatedSessionController`, you can modify the login and logout functionality to fit your application's specific requirements, such as adding additional validation, logging, or response handling.

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://github.com/laravel/fortify/blob/1.x/src/Http/Controllers/RegisteredUserController.php
[2] https://github.com/laravel/fortify/actions/runs/8832562441/job/24250119062
[3] https://laravel.com/docs/11.x/fortify
[4] https://github.com/laravel/fortify
[5] https://www.youtube.com/watch?v=CLsyHP7x0N0