Error redirecting a custom page

Good Day, I am facing the following problem: I am making a redirect to a filament custom page with the following line:

return redirect(route('filament.pages.restart-password'));

return redirect(route('filament.pages.restart-password'));
But the redirect does not go through, and takes me back to the login page. What can I be doing wrong? The configuration of the filament custom page is:
protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.pages.restart-password';

protected static bool $shouldRegisterNavigation = false;

protected static ?string $title = '';

protected static ?string $navigationLabel = 'restart-password';

protected static ?string $slug = 'restart-password';
protected static ?string $navigationIcon = 'heroicon-o-document-text';

protected static string $view = 'filament.pages.restart-password';

protected static bool $shouldRegisterNavigation = false;

protected static ?string $title = '';

protected static ?string $navigationLabel = 'restart-password';

protected static ?string $slug = 'restart-password';
Thank you very much.
12 Replies
Patrick Boivin
Patrick Boivin16mo ago
I think this should work:
return redirect(MyCustomPage::getUrl());
return redirect(MyCustomPage::getUrl());
matiasarea
matiasareaOP16mo ago
Hello Patrick, thank you very much for your reply. I have tried it too and I get the same url but the redirect is not done, I get only HTTPS 302 and the content is not shown and it redirects me to the login.
Patrick Boivin
Patrick Boivin16mo ago
Where does the redirect happen? Can you share more code for context?
matiasarea
matiasareaOP16mo ago
I understand that the problem in the question is that I am doing the redirect inside a middleware. The case is the following, I have to check if the user has to change the password. So we have a middleware that checks it after the login of the Filament-Brezzy, but I understand that it should not be a middleware but it should be a function that runs after validating the user, but filament brezzy returns:
return app(LoginResponse::class);
return app(LoginResponse::class);
And I'm not being able to change to a return redirect(MyCustumPage::getUrl()) middleware does the following:
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
$user = Auth::User();
$lastPasswords = PasswordHistory::where('user_id', $user->id)->orderBy('created_at', 'desc')->first();
if ($lastPasswords->created_at->diffInDays(now()) > env('PASSWORD_LIFETIME')) {
// Get previous session
$previous_session = SessionModel::where(
'user_id',
$user->id
)->first();
//

// Delete previous session
Session::getHandler()->destroy($previous_session->id);
//

// Redirect to password change page
// return redirect()->route('password.request');

return redirect(RestartPassword::getUrl());
//
}
}
return $next($request);
}
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
$user = Auth::User();
$lastPasswords = PasswordHistory::where('user_id', $user->id)->orderBy('created_at', 'desc')->first();
if ($lastPasswords->created_at->diffInDays(now()) > env('PASSWORD_LIFETIME')) {
// Get previous session
$previous_session = SessionModel::where(
'user_id',
$user->id
)->first();
//

// Delete previous session
Session::getHandler()->destroy($previous_session->id);
//

// Redirect to password change page
// return redirect()->route('password.request');

return redirect(RestartPassword::getUrl());
//
}
}
return $next($request);
}
Patrick Boivin
Patrick Boivin16mo ago
I think redirecting inside a middleware is fine. Where did you register this middleware, can you share your config? Another option could be to listen to the LoginSuccess event from Breezy: Oops no, nevermind
matiasarea
matiasareaOP16mo ago
Yes, of course. In the filament.php file inside the config, I declare the following:
'middleware' => [
'auth' => [Authenticate::class],
'base' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DispatchServingFilamentEvent::class,
MirrorConfigToSubpackages::class,
CheckSingleSession::class,
ChangePasswordAfterDays::class,
ChangePasswordFirstLogin::class,
],
],
'middleware' => [
'auth' => [Authenticate::class],
'base' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DispatchServingFilamentEvent::class,
MirrorConfigToSubpackages::class,
CheckSingleSession::class,
ChangePasswordAfterDays::class,
ChangePasswordFirstLogin::class,
],
],
and in Kernel.php:
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\CheckSingleSession::class,
\App\Http\Middleware\ChangePasswordAfterDays::class,
\App\Http\Middleware\ChangePasswordFirstLogin::class,
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:1000,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\CheckSingleSession::class,
\App\Http\Middleware\ChangePasswordAfterDays::class,
\App\Http\Middleware\ChangePasswordFirstLogin::class,
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
'throttle:1000,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
and use in web.php
Route::get('/', function () {
return redirect('admin/login');
})->middleware([
'single.session',
'password.changeAfterDays',
'password.changeFirstLogin',
]);
Route::get('/', function () {
return redirect('admin/login');
})->middleware([
'single.session',
'password.changeAfterDays',
'password.changeFirstLogin',
]);
Patrick Boivin
Patrick Boivin16mo ago
Ok, I'm not sure... Just an idea: Are you sure you need the middleware in both Filament + web ? Is there any chance the middleware could be triggered for regular app users and would redirect them to a Filament page (which they don't have access to)?
matiasarea
matiasareaOP16mo ago
Based on what you say, I'm thinking that when I have the middleware Authenticate from filament, the custom page rejects the access because it doesn't have a valid session yet. The LoginSuccess event would have been a good alternative, because it executes that validation after an OK event occurred.
Patrick Boivin
Patrick Boivin16mo ago
Yeah, my mistake... I don't think you can redirect from an event handler
matiasarea
matiasareaOP16mo ago
I understand. Thank you very much for your time and your answers. One last question, is it possible to add a custom page to non-logged in users?
awcodes
awcodes16mo ago
Non logged in users would just be a normal Laravel route and view. You can reuse several of the filament components to make it look similar though. But basically you can’t access a panel or tenant without being logged in.
Andrew Wallo
Andrew Wallo16mo ago
I think he’s using Filament v2 based on his config file for Filament
Want results from more Discord servers?
Add your server