F
Filament17mo ago
Matthew

Change default route after login, and/or after being authenticated

I will try and explain this as best as I can. I basically want to add a landing page to my website. If the user isnt already logged in, after logging in, he shall be redirected to the landing page instead of /app panel. Otherwise, if he is logged in, and he goes at any page on the panel, so /app/*, he should be redirected to the landing page. I only want this to be shown once, thats why I added an appropriate column to the users table. I tried doing this with middleware routes, however I had trouble finding out if the user is authinticated with Auth::check(). To me this seems more like a laravel question, than filament. Thats why im marking it as non-filamant. Any help is appreciated.
3 Replies
DrByte
DrByte17mo ago
Sounds like you're doing some sort of "confirm that you've read the terms and conditions" or "welcome" splash page, that only displays the first time a person joins. The last time I did that I created a new middleware that fired after the Authenticate middleware, and made sure to include that middleware in the controller/s that the user was most likely to hit (in my case there were two possible dashboards, and even if somehow they went to a sub-page at some point, they'd be back to the dashboard at least once per "session", so they'd see the alert then even if it was a bit after they'd done something else .... I think that's slightly different than yours, but you could register the middleware on every controller (or Panel in Filament's case) ) The middleware would just check the flag field on the User record, and redirect to the page or just return the view. If this is all related to a Filament Panel, then you'd register the middleware in the Panel.
Matthew
MatthewOP17mo ago
Can you perhaps share what you did please?
DrByte
DrByte17mo ago
Can't find that project right now. But, here's one I hacked together using Laravel's RedirectIfAuthenticated ... it's almost entirely that middleware, with just a couple lines changed:
<?php

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class HijackToWelcome
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, string ...$guards): Response
{
$guards = empty($guards) ? [null] : $guards;

foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {

// DEMO LOGIC HERE.
// This is a terrible example, but saves me adding db fields for this demo
// pick the current time's "seconds", and check whether the last digit is "less than 5"
// so the redirect happens only on seconds 0,1,2,3,4 of every 10-second increment
$seconds = date('s');
if (substr($seconds, -1) < 5) return redirect('/admin/tech');
// probably should check the current route and abort if we're already on the redirected page, so that it doesn't do an endless loop

}
}

return $next($request);
}
}
<?php

namespace App\Http\Middleware;

use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class HijackToWelcome
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, string ...$guards): Response
{
$guards = empty($guards) ? [null] : $guards;

foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {

// DEMO LOGIC HERE.
// This is a terrible example, but saves me adding db fields for this demo
// pick the current time's "seconds", and check whether the last digit is "less than 5"
// so the redirect happens only on seconds 0,1,2,3,4 of every 10-second increment
$seconds = date('s');
if (substr($seconds, -1) < 5) return redirect('/admin/tech');
// probably should check the current route and abort if we're already on the redirected page, so that it doesn't do an endless loop

}
}

return $next($request);
}
}
then in my Panel, I added it to the existing list of middleware:
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,

// added here:
HijackToWelcome::class,

ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,

// added here:
HijackToWelcome::class,

ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authMiddleware([
Authenticate::class,
])

Did you find this page helpful?