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

Patel's avatar
Level 1

Laravel redirects and authentication

I am facing an issue with redirection and I am not sure if we can actually resolve it.

We have configured that we launch firefox it will by default open 2 different tabs (home and alerts)

As they both can be accessed only via an authorized user it will ask for login which kicks in LoginController's method check_login.

In the LoginController I have added code to first login via ldap and then redirect to a page.

For redirection am using

        $requestedUrl = \request()->session()->previousUrl() ?? '/';

The issue is from whichever tab I login from (home or alerts) it would consider that as the requestedUrl and will redirect to that page instead of having 2 different tabs. eg: If I login from alerts both page will display me alerts and if login from home both pages will redirect me to home.

I have tried using intended or back but nothing works for me.

0 likes
4 replies
LaryAI's avatar
Level 58

One possible solution is to add a query parameter to the login URL indicating which tab the user is coming from. Then, in the LoginController, you can use this parameter to determine the correct redirect URL.

For example, in the home and alerts pages, you can add the query parameter like this:

<a href="{{ route('login') }}?tab=home">Login from Home</a>
<a href="{{ route('login') }}?tab=alerts">Login from Alerts</a>

Then, in the LoginController, you can modify the check_login method to use the tab parameter:

public function check_login(Request $request)
{
    // authenticate user via LDAP
    // ...

    // determine redirect URL based on tab parameter
    $tab = $request->input('tab');
    if ($tab === 'alerts') {
        $redirectUrl = '/alerts';
    } else {
        $redirectUrl = '/home';
    }

    return redirect($redirectUrl);
}

This way, the user will be redirected to the correct tab after logging in.

Patel's avatar
Level 1

@LaryAI I think this wont work. If I login from alerts page, $tab will be equal to alerts and redirectUrl will be alerts url and hence both the tabs will open alerts instead of having 2 different urls.

MohamedTammam's avatar

Nothing with session will work in your case. I suggest to add a hidden input in your login form with the desired redirect URL

<input type="hidden" name="redirect_url" value="{{ URL::current() }}" />

and in your login form

$requestedUrl = \request()->redirect_url ?? '/';
Snapey's avatar

the point being that both tabs will share the same session

when you login on one, the second tab will probably just need reload since you will be logged in

cant you hold off opening the alerts tab until authenticated?

If I needed to initially open both tabs, I would initially show a message to the user on the alerts tab that says login on main tab, and then include a meta refresh or javascript window load on a short timer so that it keeps reloading until authenticated

Please or to participate in this conversation.