0

I have a laravel application

login page is in route /login

there is a logged in user and clicks on a login button (or basically open URL /login)

application redirects the user to /home but I want to be redirected to /dashboard

I changed the redirect fields in Auth controllers to /dashboard. results when a user signs in, application redirects him to /dashboard but what about a logged in user?

I use laravel 5.4, thank you for helping

1
  • 3
    You can use protected $redirectTo = '/dashboard'; Commented May 22, 2017 at 6:25

3 Answers 3

1

You should use the RedirectIfAuthenticated middleware that is supplied with Laravel located in App\Http\Middleware\RedirectIfAuthenticated.

Then, in the following block, make sure it's set to /dashboard.

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

Then add the middleware to your login route by either wrapping it in a group:

Route::group(['middleware' => 'guest'], function(){
    //Auth routes for non-authenticated users
});

Or you can do it on the route directly:

->middleware('guest');
Sign up to request clarification or add additional context in comments.

1 Comment

tnx. that wrong redirection was in this middle-ware. after fixing that, every thing worked fine
1

Goto login controller which is located in

app->Http->Auth->LoginController

Set

protected $redirectTo = '/dashboard'

Hope it works.

Source : https://laravel.com/docs/5.4/authentication#included-authenticating

1 Comment

I've done this. it works when you login not when you are logged in and open URL /login
0

Since you want to redirect logged in user you can override showLoginForm() method in LoginController like this:

public function showLoginForm()
{
    if (auth()->check()) {
        return redirect('/dashboard');
    }

    return view('auth.login');
}

But I guess a better way to solve the problem could be just hiding login button or link from logged in users:

@if (auth()->guest())
    {{-- Show register and login links --}}
@endif

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.