Redirect Users after login to their respective dashboard based on role (admin, user) Laravel 9

Viewed 24

I want to redirect a user to user-dashboard after login while redirect an admin to admin-dashboard after login in Laravel 9. How can i achieve this?

1 Answers

If you are using use Illuminate\Foundation\Auth\AuthenticatesUsers; in your loginController you can do:

Assuming you are using Roles Package, you can do the check with HasRole or whatever field/method your project has..

public function authenticated()
{
    if(auth()->user()->hasRole('admin'))
    {
        return redirect('/admin-dashboard');
    } 

    return redirect('/user-dashboard');
}
Related