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

StealthMicro's avatar

Login form with a redirect url

I have a login form. Depending on what original page they landed on I need to have a redirect_url attached to the login form so when they login it takes them back to the same page only authenticated. I tried a simple

<input type='hidden' value'{{ request()->query('url') }}'>

where I simply have the

http://some.com/login?url=redirect_url

but that does not work then I tried the

<form wire:submit="create('{{ request()->query('url') }}')">

that works until on the login form I have a checkbox so you can login one of two ways and as soon as you hit that checkbox which then alters the form by hiding some form elements and then showing others then the value of request()->query('url') is lost in the create() method and the url becomes ''.

What would a better alternative to this whole mess be?

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like you're trying to implement a redirect after login based on the URL the user came from. The issue you're facing with the hidden input and Livewire's wire:submit is likely due to the way Livewire handles state and re-renders components.

Here's a solution that should work for you:

  1. Store the intended URL in the session before showing the login form. You can do this in your middleware or controller where you handle the redirection to the login page.
// In your controller or middleware
session(['url.intended' => request()->query('url', url()->previous())]);
  1. In your login form, you don't need to include the redirect URL as a hidden input. Livewire will maintain the state for you.

  2. In your Livewire component's create method (or the method that handles the login), you can retrieve the intended URL from the session and use it to redirect the user after a successful login.

public function create()
{
    // Perform the login logic...

    // After successful login, redirect to the intended URL
    return redirect()->to(session()->pull('url.intended', '/default-redirect-path'));
}
  1. If you're using the built-in Laravel authentication, you can customize the authenticated method in your LoginController to handle the redirection:
protected function authenticated(Request $request, $user)
{
    return redirect()->intended('/default-redirect-path');
}

Laravel's redirect()->intended() method automatically uses the url.intended session key to redirect the user to the intended URL after login, or to the default path if the session key is not set.

This approach should solve the problem of the URL being lost on re-render, as the intended URL is stored in the session and not in the component's state. It also leverages Laravel's built-in functionality to handle post-login redirections.

2 likes
Bogey's avatar

I have the following in my login.blade.php

@if($back != '/login')
    <input type="hidden" name="back" value="{{ $back }}" autocomplete="off">
@endif

In my login GET route method

public function create(): View
{
    return view('auth.login', [
        'back' => str_replace(url('/'), '', url()->previous()),
    ]);
}

And in the login POST route method

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

    $request->session()->regenerate();
    
    // Determining the real location we would be redirecting to upon successful log in
    $redirectTo = $request->back ?? RouteServiceProvider::HOME;

    return redirect($redirectTo);
}

and in RouteServiceProvider

public const HOME = '/';

And this works perfectly for me.

And for logout I have the following in the logout method

        return redirect()->back();

And I let my authorization take control after that. If they were in the user control when logging out, the get redirected to login page (besides, they are logging out, so they are done using your site... doesn't matter where they end up. They going to close the tab/browser anyways or go to a different website).

I used the Laravel Breeze for my authentication

Please or to participate in this conversation.