F
Filamentβ€’11mo ago
williamdk.

Route [login] not defined.

Hello, I get this error whenever session expire and to login again. Any help. Thanks in advance
3 Replies
Dennis Koch
Dennis Kochβ€’11mo ago
I think this is solved since you asked in other threads, but for completeness I'll copy here. Define a route with the name "login" that redirects to Filament login
Route::get('/laravel/login',
fn() => redirect(route('filament.auth.login'))
)->name('login');
Route::get('/laravel/login',
fn() => redirect(route('filament.auth.login'))
)->name('login');
acroninja
acroninjaβ€’11mo ago
I have 2 panels with separate logins and this route fix will not work
Route::get('/laravel/login',
fn() => redirect(request()->is('admin*')
? route('filament.admin.auth.login')
: route('filament.frontend.auth.login'))
)->name('login');
Route::get('/laravel/login',
fn() => redirect(request()->is('admin*')
? route('filament.admin.auth.login')
: route('filament.frontend.auth.login'))
)->name('login');
I think because at the point of checking the request it is already /laravel/login and not the original route. To get to the cause of the issue there are 2 places I've found where laravel references the login route. 1. In authenticate middleware. So that's a simple change in app/Http/Middleware/Authenticate.php
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*/
protected function redirectTo(Request $request): ?string
{
if ($request->expectsJson()) {
return null;
}

// Check if the request is for the admin panel
if ($request->is('admin*')) {
return route('filament.admin.auth.login');
}

// Default to frontend
return route('filament.frontend.auth.login');
}
}
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*/
protected function redirectTo(Request $request): ?string
{
if ($request->expectsJson()) {
return null;
}

// Check if the request is for the admin panel
if ($request->is('admin*')) {
return route('filament.admin.auth.login');
}

// Default to frontend
return route('filament.frontend.auth.login');
}
}
2. In the unauthenticated exception. So add an override in app/Exceptions/Handler.php
protected function unauthenticated($request, AuthenticationException $exception)
{
return $this->shouldReturnJson($request, $exception)

? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest($exception->redirectTo() ??
($request->is('admin*')
? route('filament.admin.auth.login')
: route('filament.frontend.auth.login')));
}
protected function unauthenticated($request, AuthenticationException $exception)
{
return $this->shouldReturnJson($request, $exception)

? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->guest($exception->redirectTo() ??
($request->is('admin*')
? route('filament.admin.auth.login')
: route('filament.frontend.auth.login')));
}
This means the login route is not called any more πŸ™‚
hdaklue
hdaklueβ€’11mo ago
I had the same problem and solved it by two methods: 1- is to extend the use Filament\Http\Middleware\Authenticate and override the redirectTo() method and update 1 ->authMiddleware([ Authenticate::class, ]); in the panel provider 2- to edit the protected function redirectTo(Request $request): ?string { return $request->expectsJson() ? null : route('filament.app.auth.login'); } on App\Http\Middleware` this way you handeled both Middlewares