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

GTHell's avatar

Redirect the previous link after login?

I can get it work passing some parameter but is there a standard way?

0 likes
5 replies
grenadecx's avatar

Seems odd because this sounds like the default action when using Laravel.

If you visited a link that required authentication and you got redirected to the login page, after login you should be redirected back.

If you visited the login page directly, you will be redirected to the whatever is set in the auth controllers $redirectTo variable. And if you where already authenticated, you will be redirected to the whatever is set inside the RedirectIfAuthenticated middleware.

tykus's avatar

Use intended() method:

return redirect()->intended('dashboard');

The intended method on the redirector will redirect the user to the URL they were attempting to access before being intercepted by the authentication middleware. A fallback URI may be given to this method in case the intended destination is not available.

https://laravel.com/docs/5.6/authentication#authenticating-users

GTHell's avatar

I have setup 2 auth. One is original user Auth and another is Admin auth. I didn't touch the Login controller so the redirect()->intended('dashboard') is left untouch. It seem to work on fresh install though but my project is way to far to redo.

Here's the code that I customize if user not authenticated will redirect to the right login page: App\Exceptions\Handler.php

....
/**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Exception $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof AuthenticationException) {
            $guard = array_get($exception->guards(), 0);

            switch ($guard) {
                case "admin":
                    return redirect((route('admin.login')));
                    break;
                default:
                    return redirect((route('login')));
                    break;
            }
        }

        return parent::render($request, $exception);
    }
.....

LoginController.php (Left un-touch)

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

Seem like the session fail and redirect to default route which is '/dashboard'. I don't know how to debug that, though.

grenadecx's avatar

In that case, you could customize the LoginController and override the function that comes with the AuthenticatesUsers trait (which in turns includes the RedirectsUsers trait):

public function redirectPath()
{
    if (method_exists($this, 'redirectTo')) {
        return $this->redirectTo();
    }

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

I would suggest in your render function, just store url()->previous() inside a session variable and then just set the $this->redirectTo to that session variable if it exists.

1 like
GTHell's avatar

@grenadecx that's what I'm going to do if I can't get answer to my original problem. I'm just not sure why this happen and what is the different doing it manually vs redirect()->intended().

If I can fix the redirect()->intended() then I don't need to write extra code.

Please or to participate in this conversation.