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

kyort's avatar
Level 2

use LoginResponse in history

I have a custom LoginResponse under App\Http\Responses. ``` class LoginResponse extends LoginResponseContract {

public function toResponse( $request ): RedirectResponse|Redirector {
    if ( auth()->user()->role === "Provider" ) {
        return redirect()->intended( "ccs/provider" );
    }

    return redirect()->intended( "/ccs" );
}

}

This works well when a user logs in. But if a "Provider" hits the back button they go to the admin dashboard. How do I accomplish the same conditional redirect when they are using their history?

0 likes
1 reply
LaryAI's avatar
Level 58

When dealing with browser history navigation, such as when a user hits the back button, the server-side logic (like your LoginResponse) doesn't get executed again. The browser simply loads the previous page from its cache or history. To handle this scenario, you can use client-side JavaScript to check the user's role and redirect them accordingly.

Here's a possible solution using JavaScript:

  1. Add a JavaScript snippet to your layout or specific page that checks the user's role and redirects them if necessary. This script should be included on pages where you want to enforce the redirect based on the user's role.

  2. Store the user's role in a JavaScript-accessible way, such as a data attribute or a JavaScript variable. You can pass this information from your server-side code to the client-side.

Here's an example of how you might implement this:

// In your Blade template or HTML file
<script>
    document.addEventListener('DOMContentLoaded', function() {
        // Assuming you have a way to get the user's role, e.g., from a meta tag or a global JS variable
        var userRole = "{{ auth()->user()->role }}"; // Make sure to escape this properly

        if (userRole === "Provider") {
            // Check if the current URL is not the intended one
            if (!window.location.pathname.startsWith('/ccs/provider')) {
                window.location.href = '/ccs/provider';
            }
        } else {
            // Check if the current URL is not the intended one for non-providers
            if (!window.location.pathname.startsWith('/ccs')) {
                window.location.href = '/ccs';
            }
        }
    });
</script>

Explanation:

  • User Role Detection: The script checks the user's role using a server-side variable passed to the client-side. Ensure this is done securely to prevent exposure of sensitive information.

  • Conditional Redirect: If the user is a "Provider" and the current path does not match the intended path, the script redirects them to the correct path. The same logic applies to non-provider users.

  • DOMContentLoaded Event: The script runs after the DOM is fully loaded to ensure all elements are accessible.

This approach ensures that even if a user navigates back using the browser's history, they will be redirected to the appropriate page based on their role.

Please or to participate in this conversation.