I would like to have administrator authorization. so I have new class in middleware
class Admin {
public function handle($request, Closure $next)
{
if ( Auth::check() && Auth::user()->isAdmin() )
{
return $next($request);
}
Session::flash('message', 'You need to be an administrator to visit this page.');
return redirect('/');
}
}
Then register it in Kernel.php by adding
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'admin' => \App\Http\Middleware\Admin::class, //added line
];
I also defined isAdmin() in my user model. It works when I do this in the route:
get('protected', ['middleware' => ['auth', 'admin'], function() {
return "this page requires that you be logged in and an Admin";
}]);
But I want to use it like Auth::admin() as Auth::guest(), where should I implement this function? Do I need a abstract admin() in Guard.php first?
I can get around with Auth::user()->isAdmin(), but I still want to know the right way to do it.
Thanks.
Thanks.