Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ubfacility's avatar

Issues with fetching user roles for use in route access control

my RoleMiddleware below does not return the user's roles

public function handle($request, Closure $next, $role, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            throw UnauthorizedException::notLoggedIn();
        }

        $roles = is_array($role)
            ? $role
            : explode('|', $role);
        if (! Auth::guard($guard)->user()->hasAnyRole($roles)) {
            throw UnauthorizedException::forRoles($roles);
        }

        return $next($request);
    }

I have added everything correctly to the route and I can't seem to understand why the roles connected to a particular user does not get returned.

0 likes
2 replies
ubfacility's avatar

I keep getting the error Forbidden. User does not have the right roles.

ubfacility's avatar

and the roles exist and are connected to the user

Please or to participate in this conversation.