I'm using JWT to get access to the controller route which gives back an array of users. I'm using prisma as ORM and postgres. It works well without The Auth Guars. I'm getting 401 unauthorized.
@Injectable()
export class UserService {
constructor(private prisma: PrismaService) {}
async get_profile(userId: number) {
const user = await this.prisma.user.findUnique({
where: {
id: userId,
},
});
return user;
}
async get_all_profiles() {
return await this.prisma.user.findMany();
}
}
@Controller('user')
export class UserController {
constructor(private userService: UserService) {}
@UseGuards(AtGuard)
@UseInterceptors(ManySerializeInterceptor)
@Get('profiles')
async get_all_profiles() {
return await this.userService.get_all_profiles();
}
@UseGuards(AtGuard)
@UseInterceptors(OneSerializeInterceptor)
@Get('profile')
async get_profile(@GetCurrentUserId() id: number) {
return await this.userService.get_profile(id);
}
}```
```ts
@Injectable()
export class AtGuard extends AuthGuard('jwt') {
constructor(private reflector: Reflector) {
super();
}
canActivate(context: ExecutionContext) {
const isPublic = this.reflector.getAllAndOverride('isPublic', [
context.getHandler(),
context.getClass(),
]);
if (isPublic) return true;
return super.canActivate(context);
}
}
