I'm makign a role guard to be used in specefic controllers. I'm following the Documentation way of using it globally in the app.module level. Here is the code.
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.enableCors();
app.useGlobalGuards(new RoleGuard(new Reflector()));
await app.listen(3000);
}
Now the Code for my RoleGuard works fine and returns console.logged results when they're expected, Only that the request.user which should be added by the passport middleware is not existant. Here is the code for the guard:
export class RoleGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const roles = this.reflector.get<string[]>('roles', context.getHandler());
if (!roles) {
console.log('no roles');
return true;
}
const request = context.switchToHttp().getRequest();
const user = request.user;
console.dir(user);
return this.matchRoles(roles, user);
}
The console.dir displays undefined, But when trying to console.dir the request it works fine, only that it doesnt contain the user proprety.