0

I'm trying to redirect my user to 'homepage' after successful login.

I've been able to redirect after logout by adding the following to AuthController:

protected $redirectAfterLogout = 'homepage';

However, adding the following to AuthController does not work after login. It directs me to 'home."

protected $redirectPath = 'homepage';

I then changed the default redirect in the handle() function in RedirectIfAuthenticated to:

return redirect('homepage');

Not only does that not work, it gives me the following error:

This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS

Does anyone know how I can achieve redirecting to 'homepage' after login?

Edit - Adding Routes:

Route::get('/', function () {
    return view('welcome');  
});

Route::get('home', 'HomepageController@getIndex');
Route::get('homepage', 'HomepageController@getIndex');

Thanks for any guidance!

2
  • Can you show us your routes file? Commented Nov 5, 2015 at 13:11
  • Sure; I'll add them above. Commented Nov 5, 2015 at 22:17

2 Answers 2

0

Dude the attribute should be named as $redirectTo:

 $redirectTo = "homepage";

now if you got more than one rule like admin and user, stored within your user model as type field, you may override the value of this attribute within postLogin() function, override the function first then do your changes. i.e:

    // AuthController.php

    /**
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postLogin(Request $request)
    {
        $this->validate($request, [
            'email' => 'required|email', 'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');

        if ($this->auth->attempt($credentials, $request->has('remember')))
        {
            $user=User::find($this->auth->user()->id);
            if($user->type == "ADMIN")
                $this->redirectTo = "/dashboard";
            return redirect()->intended($this->redirectPath());
        }
        return redirect($this->loginPath())
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
                'email' => $this->getFailedLoginMessage(),
            ]);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

When I add $redirectTo = "homepage", I get redirected to root, since the following is in RedirectIfAuthenticated: return redirect('/');. If I change that redirect to redirect('homepage'), then I get ERR_TOO_MANY_REDIRECTS.
0

I got around this by simply modifying the existing Route::get('/') to the following:

Route::get('/', 'HomepageController@getIndex');

I really didn't need the Welcome View any longer, so this solution made the most sense.

Also, after modifying this Route, I was able to remove my previous 'home' and 'homepage' Routes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.