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

christian_H's avatar

Change redirect location after login

Hey everyone, I have what should be a simple issue that is driving me crazy. I'm using laravel 5.2 with Spark 1.0. I'm trying to redirect the user to a different route after a successful login. I assumed it would be as simple as changing the $redirectTo variable in the LoginController like so:

protected $redirectTo = '/newLoginRoute';

However I've tried this and it continues to use the old route. I've also tried changing the same $redirectTo property in the AuthController, as well as hardcoding it into the RedirectsUsers trait like so:

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

Despite all this, I'm still redirected using the old route.

Can anyone point me in the right direction here? It seems to me that $redirectTo in the LoginController should be what controls this, but since that didn't work I'm really stumped. I have a feeling I overlooked something so I'm hoping another pair of eyes can notice it, thanks!

Edit: per the laravel 5.2 docs, "When a user is successfully authenticated, they will be redirected to the / URI. You can customize the post-authentication redirect location by defining a redirectTo property on the AuthController". I've tried that with no result, did Spark change something?

0 likes
9 replies
jlrdw's avatar

Don't you need a return like

return redirect('home/dashboard');
Snapey's avatar
Snapey
Best Answer
Level 122

There is also the intended route - that is, where the user was going when they were caught by middleware and asked to login.

What I have done in the past is not have a dedicated login route, but rather when the user clicks on the login link they are actually clicking on something like /dashboard

Middleware says hey, only authenticated users can go to dashboard, and intercepts the flow. The user logs in and then is directed to the place they were going in the first place such as /dashboard.

So, I would start at the beginning. What were you trying to do when you ended up at the login form?

1 like
christian_H's avatar

@Snapey thanks, you saved me a huge headache! That's exactly what was happening, it was using the intended route instead of the $redirectTo property.

aarongwebster's avatar

I think this can even by done dynamically.

SparkServiceProvider.php

public function booted()
{   
   if ( isset($_POST["redirectPath"]) ){
      Spark::afterLoginRedirectTo($_POST["redirectPath"]);
   }
}

login.blade.php

@if (isset($_GET["redirectPath"]))
<input name="redirectPath" value="{{ $_GET["redirectPath"] }}" type="hidden" />
@endif

Handler.php -> unauthenticated method

//old remove it
//return redirect()->guest('login')

//new
return redirect()->guest('login?redirectPath=' . $request->getRequestUri() );

joshuaf's avatar

None of these things work for me.

Spark::afterLoginRedirectTo('/new-uri'); returns an error that the function does not exist and the link above returned a 404.

The answer as to seeking to intended path does not deal with cases where the user is simply chooses to login. Even still, it does not address what should actually be done to solve the problem.

Changing the redirectTo and redirectPath variables solves the problem on my local machine. However, this is not a long term solution as deployments where composer installs the software overwrites these variables. Changes in the laravel password and auth controller have no effect.

Anyone find an actual solution anywhere?

2 likes
SYPOMark's avatar

@joshuaf I am having this exact same issue - I think. I am trying to redirect users to the settings screen once they have logged in, thus bypassing the home screen all together.

I've tried updating /spark/install-stubs/app/Http/Middleware/RedirectIfAuthenticated.php by altering

if (Auth::guard($guard)->check()) {
    return redirect('/home');
}

to

if (Auth::guard($guard)->check()) {
    return redirect('/settings');
}

but this has no effect. In fact, changing '/home' to anything at all, doesn't work either.

Any pointers?

With kind regards,

Mark

SYPOMark's avatar

@joshuaf I don't know if this is the solution you're looking for, but, I fixed what I was trying to achieve by changing a line in /app/Http/Controllers/HomeController.php Where it was originally written:

    public function show()
    {
        return view('home');
    }

I've changed it to:

    public function show()
    {
        return view('vendor/spark/settings');
    }

This has created the interesting problem of the Settings link in the right hand nav pointing to a page that looks the same, but is at a different address, but, I can fix that later!

Hope it helps...

toddmcbrearty's avatar

What i do is move my Route::post('login'... route to my web.php file:

Route::post('/login', '\App\Http\Controllers\Auth\LoginController@login');

This uses the login controller in controllers/auth.

Extend that login controller to use sparks:

class LoginController extends \Laravel\Spark\Http\Controllers\Auth\LoginController

now you can override the spark functions so for redirecting i do this

public function authenticated(Request $request, $user)
    {
        if (Spark::usesTwoFactorAuth() && $user->uses_two_factor_auth) {
            return $this->redirectForTwoFactorAuth($request, $user);
        }

        return redirect($this->redirectPath());
    }

With this method you aren't changing any original files. And because web.php gets loaded after routes.php you over write the original route so no need to even comment out the routes.php

Please or to participate in this conversation.