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

safrinbaharudin's avatar

Return redirect intended not really working on Laravel 10 + Vue JS

Im starting to implementing javascript front-end on my Laravel app, and I chose Vue as my front-end framework, after publishing the app to production, I just realised that I the app does not redirect me to the homepage after login, I have to refresh the page in order to be redirected to homepage.

After doing some experiment, I found that return redirect()->intended(RouteServiceProvider::HOME); does not work on the production, but it worked on development when it running on npm run dev. I kinda solve it by change the way Laravel return after login successfull:

app > Http > Controllers > Auth > AuthenticatedSessionController.php

public function store(LoginRequest $request): RedirectResponse
    {
        $request->authenticate();

        $request->session()->regenerate();

        $url = parse_url(Session::get('url.intended'));

        return redirect($url['path']);

        // return redirect()->intended(RouteServiceProvider::HOME);

        // return redirect()->route('url.intended');
    }

any idea on what could possibly go wrong or is there any proper solution for this?

0 likes
8 replies
LaryAI's avatar
Level 58

It seems like you're facing an issue where the redirect()->intended() method is not working as expected in your production environment with a Vue.js frontend. This method is supposed to redirect the user to the URL they were originally trying to access before being intercepted by the authentication process.

Here are a few things you can check and try to resolve the issue:

  1. Ensure Session Persistence: Make sure that sessions are being persisted correctly in your production environment. If sessions are not persisted, the intended URL will not be saved, and thus the redirect cannot work as expected.

  2. Check Middleware: Ensure that the middleware responsible for storing the intended URL is running correctly in production. This is typically the Authenticate middleware.

  3. Frontend Handling: Since you're using Vue.js, you might be handling authentication through AJAX requests. If that's the case, the redirect()->intended() method won't work directly because the AJAX request won't follow the redirect. Instead, you should return the intended URL to the Vue.js application and let it handle the redirection.

Here's an example of how you might modify your store method to return the intended URL to your Vue.js frontend:

public function store(LoginRequest $request)
{
    $request->authenticate();
    $request->session()->regenerate();

    // Get the intended URL
    $intendedUrl = Session::get('url.intended', RouteServiceProvider::HOME);

    // Clear the intended URL from the session
    Session::forget('url.intended');

    // Return the intended URL to the frontend
    return response()->json([
        'intended_url' => $intendedUrl
    ]);
}

Then, in your Vue.js application, you would handle the response and redirect the user:

// Example of handling the response in Vue.js (using Axios for the HTTP request)
axios.post('/login', this.loginForm)
    .then(response => {
        // Redirect to the intended URL or default to the home page
        window.location.href = response.data.intended_url;
    })
    .catch(error => {
        // Handle error (e.g., show login error message)
    });
  1. Check Production Configuration: Make sure that your production environment is configured correctly, including the .env file settings for session driver, session domain, etc.

  2. Inspect Network Traffic: Use the browser's developer tools to inspect the network traffic during the login process. Check the response from the server to see if it includes a redirect or the intended URL.

  3. Check for JavaScript Errors: Ensure that there are no JavaScript errors in the console that might be preventing the redirection from occurring.

  4. Review Server Logs: Check the server logs for any errors that might indicate why the redirection is not working as expected.

By following these steps, you should be able to diagnose and fix the issue with the redirect()->intended() method in your Laravel + Vue.js application.

Snapey's avatar

intended only works if you actually intended to go somewhere else and was interrupted by needing to login

1 like
safrinbaharudin's avatar

@Snapey In my case, it does not matter if the user are trying to go somewhere or it's the starting point of my app, user need to refresh the page after logged in in order to see the page that they are suppose to see, whether its a home page or somewhere else before interrupted by login.

Snapey's avatar

it does matter because you are saying that intended is not working. Im saying it might not even be in play

safrinbaharudin's avatar

@Snapey Understand, I was just wondering why it work perfectly on local while running on npm run dev , but after built and publish to production, it's not working as expected.

safrinbaharudin's avatar

@Randy_Johnson this one will always redirect user to the url('/') page after login which is not what it suppose to be. It suppose to redirect back to the page before interrupted by login.

safrinbaharudin's avatar

@Randy_Johnson I solve it temporarily as I mention on my first post:

public function store(LoginRequest $request): RedirectResponse
    {
        $request->authenticate();

        $request->session()->regenerate();

        $url = parse_url(Session::get('url.intended'));
        $path = isset($url['path']) ? $url['path'] : RouteServiceProvider::HOME;

        return redirect($path);

        // return redirect()->intended(RouteServiceProvider::HOME);

        // return redirect()->route('url.intended');
    }

Please or to participate in this conversation.