Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Spark - redirect on login

I have a very simple problem. I just want to direct the user to somewhere other than '/home' after they login. This is not difficult if you can alter the spark software and retain those changes in every deployment. However, composer reinstalls everything when things are deployed and it is generally bad practice to make changes to core vendor software.

This seems like it should be a very basic and simple thing for the creators to work into the software. So, how do I do it?

I have tried ...

Altering the redirectTo and redirectPath variables in the auth controller and the password controller in my app.

Adding a login controller to my app - independent of spark - and then resetting the same variables.

Attempting to call the afterLoginRedirectTo and afterAuthRedirectTo functions in the Spark service provider. This returned an error indicating that the functions did not exist.

Not sure where to go from here.

like image 950
Joshua Foxworth Avatar asked Jan 22 '17 23:01

Joshua Foxworth


1 Answers

After having the same issue I've done some digging and found a way of setting something other than home, I've changed a fair bit of stuff, but hopefully this works for you too!

TLDR

Spark::afterLoginRedirectTo('somenewplace');

Option 1

The variable used is: $afterLoginRedirectTo from vendor\laravel\spark\src\Configuration\ManagesAppOptions.php

You can set this within the SparkServiceProvider@boot method:

Spark::afterLoginRedirectTo('somenewplace');

Spark has its own LoginController \vendor\laravel\spark\src\Http\Controllers\Auth\LoginController.php

which has an authenticated method to handle the two factor auth settings:

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

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

RedirectPath() is from the RedirectsUsers trait which is still in vendor and does the following:

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

redirectTo in the LoginController is set in the construct method:

$this->redirectTo = Spark::afterLoginRedirect();

Option 2

Create a new route to override the login function.

in web.php specify a new route for post login:

Route::post('/login', 'Auth\NewLoginController@login');

You can then extend the LoginController and override the authenticated method:

 class LoginController extends \Laravel\Spark\Http\Controllers\Auth\LoginController
 {
   public function authenticated(Request $request, $user)
   {
    /**
     * @var $user User
     * Set some logic here of your own for new redirect location
     */
    if ($user->last_page_accessed != null) {
      $this->redirectTo = $user->last_page_accessed;
    }
    return parent::authenticated($request, $user);
  }
}
like image 53
ganey Avatar answered Sep 22 '22 18:09

ganey