Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cameronscott137's avatar

Conditionally set a URL to redirect to after authentication.

In my app, a post author can set an otherwise public post to private. If an unauthenticated user tries to visit that post, they will be prompted to login.

After they authenticate, I want to redirect them back to the original post URL, so they can read that private post.

This behavior is normally handled by Laravel's default auth middleware. However, because the posts are often public, I can't use that in this case.

Here's my current, non-functioning middleware:

    public function handle($request, Closure $next)
    {
        $post = $request->route('post');
        if ($post->isPrivate()) {
            $request->session()->setPreviousUrl($request->url());
            return redirect()->guest('login');
        }
        return $next($request);
    }

My hope is that I can set a custom URL to redirect to (/posts/{id}). However, when I try to login, I'm redirected to my default $redirectTo property (/dashboard).

Is this something that's feasible? Am I even thinking about this in the correct way?

0 likes
7 replies
bobbybouwmann's avatar
Level 88

Well you should be handeling this in your LoginController. The trait that's included redirects the user, but you can override that behaviour

protected function authenticated(Request $request, $user)
{
    if ($request->session()->has('previous_url')) {
        return redirect($request->session()->get('previous_url'));
    }
}

You already catch the correct url in your middleware, so you should be good to go after that ;)

cameronscott137's avatar

@bobbybouwmann, thank you! That put me on the right path.

For anyone else looking, here's my final code:

Middleware:

    public function handle($request, Closure $next)
    {

        if (auth()->check()) {
            return $next($request);
        }

        $post = $request->route('post');
        if ($post->isPrivate()) {
            $request->session()->setPreviousUrl($request->path());
            return redirect()->guest('login');
        }

        return $next($request);
    }

LoginController:

    protected function authenticated(Request $request, $user)
    {
        if ($request->session()->has('url.intended')) {
            return redirect($request->session()->get('url.intended'));
        }
    }
jlrdw's avatar

And for someone looking, it is in the docs:

Authenticating

Now that you have routes and views setup for the included authentication controllers, you are ready to register and authenticate new users for your application! You may access your application in a browser since the authentication controllers already contain the logic (via their traits) to authenticate existing users and store new users in the database. Path Customization

When a user is successfully authenticated, they will be redirected to the /home URI. You can customize the post-authentication redirect location by defining a redirectTo property on the LoginController, RegisterController, ResetPasswordController, and VerificationController:

protected $redirectTo = '/';

Next, you should modify the RedirectIfAuthenticated middleware's handle method to use your new URI when redirecting the user.

I followed docs, and all worked as expected, out of the box.

bobbybouwmann's avatar

@jlrdw That was not the question! The OP wants to redirect to a certain route which is different every time. The redirectTo variable is fixed and can't change dynamically. So quoting the docs is not a good approach here!

@cameronscott137 Can you mark yours or mine answer as best reply. This way it might help others as well ;)

Snapey's avatar

The redirectTo variable is fixed and can't change dynamically. So quoting the docs is not a good approach here!

Sorry @bobbybouwmann thats not correct.

redirectTo can also be a function and is expected to return the string of where the user should be sent after login.

for instance

    protected function redirectTo()
    {
        if (request()->session()->has('url.intended')) {
            return request()->session()->get('url.intended');
        }
    return '/';
    }

Infact I'm thinking this is not required at all, as the default behaviour is to redirect to the intended URL anyway.

1 like
cameronscott137's avatar

@jlrdw Thanks for the insight! You're correct, we could achieve the same result by turning the $redirectTo property into a protected function, as @snapey suggested.

@snapey, you're correct, as well—in most cases, this would be the default behavior when using Laravel's auth middleware.

That wasn't an option for me here, though. Some posts are public, and some are private, and I don't know which is which until I hit the controller.

Utilizing the auth middleware would keep those public posts from being accessible, so I had to watch for private posts, and manually re-create that same functionality using @bobbybouwmann's suggestion:

if ($post->isPrivate()) {
    $request->session()->setPreviousUrl($request->path());
    return redirect()->guest('login');
}   

Thanks, all!

Snapey's avatar

That said, I would use authorization rather than middleware

1 like

Please or to participate in this conversation.