$this->auth->role is nothing, should be something like this: $this->auth->user()->role
Feb 20, 2015
2
Level 2
MiddleWare for Roles?
Hi, Why isn't this working:
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class AdminAuthenticate {
protected $auth;
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
public function handle($request, Closure $next)
{
if ($this->auth->guest() || $this->auth->role != 'admin')
{
if ($request->ajax())
{
return response('Unauthorized.', 401);
}
else
{
return redirect()->guest('auth/login');
}
}
return $next($request);
}
}
And for the controllers:
public function __construct()
{
$this->middleware('authAdmin');
}
Kernel
protected $routeMiddleware = [
'auth' => 'App\Http\Middleware\Authenticate',
'authAdmin' => 'App\Http\Middleware\AdminAuthenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'App\Http\Middleware\RedirectIfAuthenticated',
];
I want a Middleware, so only Users that are logged in and have as role in the Usertable "admin".
Level 88
1 like
Please or to participate in this conversation.