Search code examples
laravellaravel-permission

Laravel - How to display Access Denied if not Super Admin


I have this Controller:

    class AdminLoginController extends Controller
    {
        public function index() {
            return view('auth.admin.login');
        }
    }

route\web

    Route::get('/admin/login', 'Auth\AdminLoginController@index')->name('admin.login');

I am using spatie

How do I do it that if the logged in user is not 'Super Admin'

> Auth::user()->hasRole('Super Admin')

The application should display Access Denied. But if its super admin, it should redirect to dashboard.

How can I get this done?

Thanks


Solution

    1. Add new role in config/auth.php such as admin role.

    2. Redefine redirect url in login controller for that role. (You can set it with Access Denied page)

    3. Please add constructor in Dashboard controller as following

      public function __construct() { $this->middleware('auth:super_admin'); }