What I've been trying is to have the middleware
do different tasks, based on the user's type.
Here is a Route group.
Route::group(array('prefix' => 'api', 'middleware' => 'admin'), function () {
Route::resource('animal', 'AnimalController');
//Other resources
});
My User
model has 2 types, which can be accessed by the following way.
$this->user()->user_type_id;
Following this advice, I was trying this task, and the handle
function in App\Http\Middleware\Authenticate
looks like this right now.
public function handle($request, Closure $next)
{
if ($this->auth->guest()) {
if ((Auth::user()->user_type_id == 2) {
//If the user is of type 2, this will be triggered
if ($request->ajax()) {
return response('You are type 2.', 401);
}
//Maybe there are other libes here
} else if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('auth/login');
}
}
return $next($request);
}
I thought that this would lead the user of type 2 to get response('You are type 2.', 401)
, when the user visits a URL prefixed by api/animal
, but I saw a message Unauthorized.
in the response.
Is it possible to have the authentication middleware work in such a way? Any advice will be appreciated.