Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

agiledigital's avatar

Fortify - perform action after successful login

I'm using Fortify with Laravel 11, and I'm looking to extend Fortify to allow me to perform an action when a user successfully logs in.

In a nutshell, when the user has logged in, I want to store a couple of additional session variables before the user is then redirected to the dashboard. Is there a method I can call in the FortifyServiceProvider's register or boot methods to achieve this or is there another/ better way of doing this?

0 likes
3 replies
aleahy's avatar
aleahy
Best Answer
Level 25

You could just replace the LoginResponse with your own that stores the session variables.

Something like:

// FortifyServiceProvider

public function register(): void
{
    $this->app->instance(LoginResponse::class, new class implements LoginResponse {
        public function toResponse($request)
        {
            // Store session variables here

            return $request->wantsJson()
                    ? response()->json(['two_factor' => false])
                    : redirect()->intended(Fortify::redirects('login'));
        }
    });
}

https://laravel.com/docs/11.x/fortify#customizing-authentication-redirects

agiledigital's avatar

@aleahy Thanks, I worked out the answer whilst waiting for a reply but it matches what you've suggested so will give you a best answer mark.

1 like
martinbean's avatar

@agiledigital You could register your own login response in your FortifyServiceProvider that sets any session variables:

$this->app->instance(LoginResponse::class, new class implements LoginResponse {
    /**
     * @param \Illuminate\Http\Request $request
     */
    public function toResponse($request)
    {
        // Set session variables here...
        $request->session()->set('foo', true);
        $request->session()->set('bar', false);

        return redirect()->intended('/');
    }
});

Docs: https://laravel.com/docs/fortify#customizing-authentication-redirects

EDIT: Someone else wrote pretty much the same answer whilst I was composing my reply πŸ˜…

Please or to participate in this conversation.