This is my guard
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private reflector: Reflector) { }
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const userTypes = this.reflector.get<UserType[]>('userTypes', context.getHandler())
if (!userTypes || userTypes.length == 0) {
return true
}
const request = context.switchToHttp().getRequest()
const user: UserSafe = request.user
if (!user) {
return false
}
return userTypes.includes(user.type)
}
}
I'm using it in a controller like this,
@Controller('flight')
@UseGuards(AuthGuard)
export class FlightController {
constructor(private readonly context: ContextService) { }
@Get()
async test(): Promise<string> {
return "Hello World!";
}
}
The code mentioned on the doc is not working it self, so can't use this anywhere. createDecorator is highlighted red.
import { Reflector } from '@nestjs/core';
export const Roles = Reflector.createDecorator<string[]>();