0
votes

I have changed the protected $redirectTo = '/tasks'; in both the LoginController and RegisterController. Also, I have changed the redirect path in the middleware RedirectIfAuthenticated as follows:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/tasks');
    }

    return $next($request);
}

Even though I have done these changes, nothing works, and the pages are redirected to the /login path.

2

2 Answers

0
votes

That's because $redirectTo will take effect after you have logged in. First of all you need to login. That's why it's sending you to /login path. So if you want to change where it redirects after Login or Registration, you change those $redirectTo properties. If you don't want a user to login at all, you should remove the auth middleware for that route.

EDIT: So what you have missed is that laravel redirects you back to the intended page and if there is none it will redirect to $redirectTo. So if you try to go to homepage and that needs authenticated user, after login it redirects back to the homepage and not /tasks because that's where you were trying to go. If you want to always redirect to that path and not to intended path you can do something as below.

protected function sendLoginResponse(Request $request)
{
    $request->session()->regenerate();

    $this->clearLoginAttempts($request);

    return redirect()->to($this->redirectTo);
}

Add this code to your LoginController and you will always be redirected to $redirectTo.

0
votes

After seeing your code, I got that your IF statement is not returning true. You have to first see, what is it returning. Also, redirect to your desired location without any condition just to test the code.

Also, I am using this code for redirection after login in my application.

public function handle($request, Closure $next)
{
    if ($this->auth->check()) {
        return redirect('/home');
    }

    return $next($request);
}