jeudi 26 mai 2016

Redirect admin to dashboard after login

I need to redirect an admin user to the dashboard after he logs in. To achieve this, I've overriden the redirectPath() method that is located in RedirectUsers trait. Here is the code:

public function redirectPath()
{
    if (property_exists($this, 'redirectPath')) {
        return $this->redirectPath;
    }

    // MODIFICATIONS ------------------------------
    if(request()->user()->hasRole('admin')){
        return '/dashboard';
    }
    // ENDMODIFICATIONS ---------------------------

    return property_exists($this, 'redirectTo') ? $this->redirectTo : '/home';
}

The problem

Dashboard redirection works only in some cases:

  • At first login, the admin is redirected to the home page.
  • At subsequent logins, the admin is redirected to the dashboard.
  • But if php artisan cache:clear command is run, admin gets redirected to the home page again.

Why is this happening and how do I implement this functionality correctly?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire