0
votes

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

3
You can use protected $redirectTo = '/dashboard';Nikhil Radadiya

3 Answers

1
votes

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');
1
votes

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

0
votes

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