Laravel 8: Route login not defined after redirection back from auth middleware

  laravel, laravel-8, php

I just added this route group to web.php:

Route::middleware('auth')->group(function() {
    Route::get('profile' , [ProfileController::class, 'index'])->name('profile');
});

As you can see I have used the middleware auth so if I’m not logged in, it should be showing me the login page.

And I had also defined authentication routes manually:

Route::prefix('auth')->namespace('Auth')->middleware('guest')->group(function() {
    Route::get('login' , [LoginController::class, 'showLogin'])->name('auth.login');
    Route::post('login' , [LoginController::class, 'login']);
    Route::get('register' , [RegisterController::class, 'showRegister'])->name('auth.register');
    Route::post('register' , [RegisterController::class, 'register']);
});

So the route name of the login page is auth.login and I just added it to Authenticate middleware which is set to auth at kernel.php:

'auth' => AppHttpMiddlewareAuthenticate::class,

Authenticate.php:

protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route('auth.login');
        }
    }

Now, the problem is whenever it should showing me the login page when not signed in, I get this error message:

SymfonyComponentRoutingExceptionRouteNotFoundException Route
[login] not defined.

So what’s going wrong here, how can I fix this error?

Source: Laravel

Leave a Reply