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

antoniolima's avatar

Fortify - Target [Illuminate\Contracts\Auth\StatefulGuard] is not instantiable

I've installed Fortify locally and implemented the login routes as well as 2FA without any issues (i.e. users can login, enable it, scan the QR code, input a code to confirm and after that it will always ask for a 2FA code when logging in). However, after deploying it on Heroku, I was getting a 404 when hitting /login. If I run php artisan route:list I could still see all of Fortify's routes. Weird. So I decided to move them to routes\web.php and add Fortify::ignoreRoutes(); to the register method in FortifyServiceProvider.php. It worked. But after trying to log in I got this error:

Target [Illuminate\Contracts\Auth\StatefulGuard] is not instantiable while building [Laravel\Fortify\Http\Controllers\AuthenticatedSessionController].

I then also added this to the register method in FortifyServiceProvider.php :

$this->app->bind(StatefulGuard::class, function () {
    return Auth::guard(config('fortify.guard', null));
});

This fixed the previous problem, the user was logged in, but I still got this error:

Target [Laravel\Fortify\Contracts\LoginResponse] is not instantiable.

And when I tried to enable 2FA I got this error:

Target [Laravel\Fortify\Contracts\TwoFactorAuthenticationProvider] is not instantiable.

This is as far as I got. I'm sure there must be a "hack" to prevent this error but this can't be the proper way to fix it. I followed the official instructions and got it to work locally so obviously the different environment is the key factor. I just can't think of anything that could cause this. Some extra configuration in Heroku I'm missing, perhaps?

Thank you to anyone who can help me with this!

0 likes
5 replies
LaryAI's avatar
Level 58

It sounds like you may need to register the Fortify service provider in your config/app.php file. This should be done after all of your other service providers are registered.

'providers' => [
    // Other Service Providers

    Laravel\Fortify\FortifyServiceProvider::class,
],

You may also need to make sure that the config/fortify.php file is configured correctly. Make sure that the guard option is set to the guard that you are using for authentication.

'guard' => 'web',

If you are still having issues, you may need to clear your application cache with the php artisan config:cache command.

I hope this helps!

antoniolima's avatar

@LaryAI Thanks Lary, I had done all 3 things already but unfortunately it didn't help. Though I believe if I hadn't, it wouldn't be working locally either.

Silas_MK's avatar

Likewise having the same issue in production but works in local environment

antoniolima's avatar

@Silas_MK Unfortunately, I never got to fix this issue BUT I recently came across a similar issue which took me countless hours to find a solution. In the end, this is what worked for me - just adding that just adding that extra configuration. Given the similarities in the error messages, I have a feeling this could be the root problem. Let me know if it works!

Have a look: https://stackoverflow.com/questions/68995275/laravel-problem-with-cache-file-when-deploy-to-heroku/69006892#69006892

DerekBuntin's avatar

@antoniolima I ran into this same issue and here is what I added to my custom fortify service provider to get both issues resolved:

class FortifyServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        // Bind the guard Fortify controllers expect
        $this->app->bind(StatefulGuard::class, function () {
            $guard = config('fortify.guard', 'web');

            return Auth::guard($guard);
        });

        // Bind the two-factor redirector contract to Fortify's default action
        $this->app->bind(
            RedirectsIfTwoFactorAuthenticatable::class,
            RedirectIfTwoFactorAuthenticatable::class
        );
    }

    public function boot(): void
    {
        // ... existing code ...
    }
}

This has worked for me so far, but I am still testing. If you'd like to see the entire Fortify and Jetstream service providers, let me know.

I hope this helps.

Please or to participate in this conversation.