I have created a guard
JavaScript
x
16
16
1
@Injectable()
2
export class EmailConfirmationGuard implements CanActivate {
3
canActivate(context: ExecutionContext) {
4
const request: RequestWithUser = context.switchToHttp().getRequest();
5
6
console.log(request.user);
7
8
9
if (!request.user?.hasEmailConfirmed) {
10
throw new UnauthorizedException("Confirm your email first before updating your profile");
11
}
12
13
return true;
14
}
15
}
16
And i am using it on of my endpoints
JavaScript
1
6
1
@UseGuards(JwtAuthGuard)
2
@UseGuards(EmailConfirmationGuard)
3
@Post("/update-profile")
4
@UseInterceptors(FileInterceptor("file"))
5
async updateProfile(@UploadedFile() file: Express.Multer.File, @Body("full-name") fullname: string,@Request() req) {
6
The point is it is faling because getRequest is not returning the authenticated user it is returning undefined
JavaScript
1
2
1
const request: RequestWithUser = context.switchToHttp().getRequest();
2
How can i return the authenticated user from the response ?
Advertisement
Answer
You should use your JwtAuthGuard at your controller level since nest doesn’t have an order to run the decorators.
JavaScript
1
9
1
@UseGuards(JwtAuthGuard)
2
export class YourController{
3
4
@UseGuards(EmailConfirmationGuard)
5
@Post()
6
public async yourFunction() {}
7
8
}
9