'NestJS Roles authorization

i try to establish an authorization with nestjs and Role guards. I have an error with type.

this is role.guards.ts

@Injectable()
export class RolesGuard implements CanActivate {
  constructor(private reflector: Reflector) {}

  canActivate(context: ExecutionContext): boolean {
    const { user } = context.switchToHttp().getRequest();
    if (!user) throw isUnauthorizedHttpException();

    const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
      context.getHandler(),
      context.getClass(),
    ]);

    if (user.role === 'admin') return true;
    if (requiredRoles !== user.role) throw noRoleAllowedHttpException();

    return requiredRoles.some((role) => user.role?.includes(role));
  }
}

The const {user} send me this error : Unsafe assignment of an any value. I try to fix it with out succes.

If some one have already have this error and fix it, thanks for helping me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source