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

yaddly's avatar

Multi-Authentication Error

I'm following a tutorial on how to add multiple authentication systems to a Laravel 10 app using Jetstream package. A login system for users and a separate one for admins. The tutorial is using Laravel 9 and despite following everything step by step, I'm getting the error below when I navigate to admin/login route: Kindly follow this link to download a sample app that I'm working on. Please remember to create an auth_app database first, then run your migration and finally, you run the app.

App\Http\Controllers\AdminController::__construct(): Argument #1 ($guard) must be of type Illuminate\Contracts\Auth\StatefulGuard, null given

This is the class that is throwing the exception:

use Illuminate\Http\Request;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Features;
use Illuminate\Routing\Pipeline;
use Illuminate\Routing\Controller;
use App\Http\Responses\LoginResponse;
use Illuminate\Contracts\Auth\StatefulGuard;
//use Laravel\Fortify\Contracts\LoginResponse;
use Laravel\Fortify\Contracts\LogoutResponse;
use Laravel\Fortify\Http\Requests\LoginRequest;
use Laravel\Fortify\Contracts\LoginViewResponse;
use App\Actions\Fortify\AttemptToAuthenticate;
use Laravel\Fortify\Actions\EnsureLoginIsNotThrottled;
use Laravel\Fortify\Actions\PrepareAuthenticatedSession;
use App\Actions\Fortify\RedirectIfTwoFactorAuthenticatable;

class AdminController extends Controller
{
    /**
     * The guard implementation.
     *
     * @var \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected $guard;

    /**
     * Create a new controller instance.
     *
     * @param  \Illuminate\Contracts\Auth\StatefulGuard  $guard
     * @return void
     */
    public function __construct(StatefulGuard $guard)
    {
        $this->guard = $guard;
    }

    public function login()
    {
        //return view('auth.login', ['guard' => 'admin']);
        return view('auth.login', ['guard' => $this->guard?->name]);
    }

    /**
     * Show the login view.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Laravel\Fortify\Contracts\LoginViewResponse
     */
    public function create(Request $request): LoginViewResponse
    {
        return app(LoginViewResponse::class);
    }

    /**
     * Attempt to authenticate a new session.
     *
     * @param  \Laravel\Fortify\Http\Requests\LoginRequest  $request
     * @return mixed
     */
    public function store(LoginRequest $request)
    {
        return $this->loginPipeline($request)->then(function ($request) {
            return app(LoginResponse::class);
        });
    }

    /**
     * Get the authentication pipeline instance.
     *
     * @param  \Laravel\Fortify\Http\Requests\LoginRequest  $request
     * @return \Illuminate\Pipeline\Pipeline
     */
    protected function loginPipeline(LoginRequest $request)
    {
        if (Fortify::$authenticateThroughCallback) {
            return (new Pipeline(app()))->send($request)->through(array_filter(
                call_user_func(Fortify::$authenticateThroughCallback, $request)
            ));
        }

        if (is_array(config('fortify.pipelines.login'))) {
            return (new Pipeline(app()))->send($request)->through(array_filter(
                config('fortify.pipelines.login')
            ));
        }

        return (new Pipeline(app()))->send($request)->through(array_filter([
            config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
            Features::enabled(Features::twoFactorAuthentication()) ? RedirectIfTwoFactorAuthenticatable::class : null,
            AttemptToAuthenticate::class,
            PrepareAuthenticatedSession::class,
        ]));
    }

    /**
     * Destroy an authenticated session.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Laravel\Fortify\Contracts\LogoutResponse
     */
    public function destroy(Request $request): LogoutResponse
    {
        $this->guard->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();

        return app(LogoutResponse::class);
    }
}
0 likes
1 reply
LaryAI's avatar
Level 58

The error message suggests that the $guard argument passed to the RedirectIfTwoFactorAuthenticatable constructor is null, but it should be an instance of Illuminate\Contracts\Auth\StatefulGuard. This could be caused by a misconfiguration of the authentication guards in the config/auth.php file or in the fortify.php file if you are using Laravel Fortify.

To fix this issue, make sure that you have defined the guards correctly in the configuration files and that you are passing the correct guard name to the RedirectIfAuthenticated middleware. Also, make sure that you have imported the Illuminate\Contracts\Auth\StatefulGuard class in the file where the RedirectIfTwoFactorAuthenticatable class is defined.

Here's an example of how to define multiple guards in the config/auth.php file:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
],

And here's an example of how to use the RedirectIfAuthenticated middleware with multiple guards:

Route::middleware(['auth:web,admin'])->group(function () {
    // Routes that require authentication for both web and admin guards
});

Route::middleware(['auth:web'])->group(function () {
    // Routes that require authentication for the web guard only
});

Route::middleware(['auth:admin'])->group(function () {
    // Routes that require authentication for the admin guard only
});

Make sure that you are passing the correct guard name to the Auth::guard() method in the RedirectIfAuthenticated middleware. For example, if you want to check if the user is authenticated with the admin guard, you should call Auth::guard('admin')->check().

If you are still having issues, try clearing the cache and restarting the server:

php artisan cache:clear
php artisan config:clear
php artisan route:clear
php artisan view:clear
php artisan serve

Please or to participate in this conversation.