0
votes

i have two guards admin and users and here users are of different type

-users -BD -SPOC -RECRUITOR -LEADERSHIP

now I have some controllers which i want both admin and user to access.if anyone of them is logged in.but i dont want to allow anybody else to access it without login.

This is my controller

class AdminController extends Controller {

protected $redirectTo = '/admin/dashboard';
protected $redirectAfterLogout = '/admin/login';

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

}

in the above controller if i use middleware -auth:admin then only admin will be able to access it and if i use middleware-auth only then i will take the user to user login screen.i want if anybody between admin or user of type leadership is logged in then they are able to access this controller.

i want or condition in middleware to check either this or that ie middleware between auth or auth:admin not both how do i do this? i m using laravel.i m new to this please help

2
Laravel uses middlewares to handle requests. You can specify middleware in routes. You can also use __construct() function to specify permissions to access controller methods. Read the doc MiddlewareEazy Sam

2 Answers

0
votes

Your can use multiple middlewares in your controllers

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

Admin and user both have access to page

Same you can do in your routes

Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => ['auth:web,admin']], function() {
Route::get('url','somecontroller@method');
});

You need one of above

Web is the default guard defined in your config/auth.php

When you are using this somewhere

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

Will work same as below, because web guard is placed by default

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

You can use the Middleware concept.Middleware is checking in route file.Below way to check user auth.

Route::group(['middleware' => 'admin.user'], function () {
// Your routes which you want to allow after login
}