Summer Sale! All accounts are 50% off this week.

overlocked's avatar

Dynamic check or helper to get all enabled Fortify routes

Currently, I am manually hardcoding paths to check if a URL belongs to active Fortify routes. For example, to avoid redirect loops with url.intended

if (url()->previous() != url('/register') && url()->previous() != url('/forgot-password')) {
    session(['url.intended' => url()->previous()]);
}

This approach is brittle. If I enable more Fortify features (like two-factor auth) in the future, I have to remember to manually update this list.I need a dynamic way to either check if a given URL/route belongs to Fortify, or programmatically retrieve a list of all currently enabled Fortify paths.

0 likes
3 replies
Shivamyadav's avatar

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()]);
}
overlocked's avatar

That's a good idea, thanks! But the problem is that Fortify route names don't contain the fortify. prefix.

Glukinho's avatar

https://laravel.com/docs/13.x/fortify#introduction

After installing Fortify, you may run the route:list Artisan command to see the routes that Fortify has registered.

Can you run route:list and point which routes you want to select?

update 1: Also you can see here which routes Fortify registers and select them by part of name or URL: https://github.com/laravel/fortify/blob/1.x/routes/routes.php

$fortifyRouteNames = ['login', 'logout', 'password', 'register', 'verification', 'user-profile-information', 'user-password', 'two-factor', 'passkey'];

// If route name starts with $fortifyRouteNames...

update 2: And more, here you can see Fortify registers it's routes with a prefix from config: https://github.com/laravel/fortify/blob/1.x/src/FortifyServiceProvider.php

protected function configureRoutes()
    {
        if (Fortify::$registersRoutes) {
            Route::group([
                // ...
                'prefix' => config('fortify.prefix'),

So I think you can publish Fortify config and set in config/fortify.php:

'prefix' => 'fortify.',

And all your Fortify routes names will start from fortify. and will be easy to catch.

Please or to participate in this conversation.