You can create a helper function like this or service class.
if (!function_exists('isFortifyRoute')) {
function isFortifyRoute(string $url): bool
{
try {
$route = app('router')->getRoutes()->match(
request()->create($url)
);
return $route && $route->getName() && str_starts_with($route->getName(), 'fortify.');
} catch (\Throwable $e) {
return false;
}
}
}
And use it like this
if (!isFortifyRoute(url()->previous())) {
session(['url.intended' => url()->previous()]);
}