|
1 | 1 | import { ExecutionContext, Injectable } from '@nestjs/common'; |
2 | 2 | import { Reflector } from '@nestjs/core'; |
3 | 3 | import { AuthGuard } from '@nestjs/passport'; |
4 | | -import { IsPublic } from '../decorator'; |
| 4 | +import { IsPublic, SkipApplicationCheck } from '../decorator'; |
5 | 5 | import { AppException, ERROR_CODE } from '../../exceptions'; |
6 | 6 | import { RequestAuthData } from '../interfaces/request-auth-data.interface'; |
7 | 7 | import { PrismaService } from '../../prisma/prisma.service'; |
8 | 8 | import { PermissionManager } from '../../utils'; |
| 9 | +import { RawApiApplication } from '../../prisma/types'; |
9 | 10 |
|
10 | 11 | @Injectable() |
11 | 12 | export class JwtGuard extends AuthGuard('jwt') { |
12 | | - constructor(private reflector: Reflector, private prisma: PrismaService) { |
| 13 | + constructor( |
| 14 | + private reflector: Reflector, |
| 15 | + private prisma: PrismaService, |
| 16 | + ) { |
13 | 17 | super(); |
14 | 18 | } |
15 | 19 |
|
16 | 20 | async canActivate(context: ExecutionContext) { |
17 | 21 | const request = context.switchToHttp().getRequest() as { user: RequestAuthData }; |
| 22 | + const canSkipApplicationHeader = this.reflector.get(SkipApplicationCheck, context.getHandler()); |
18 | 23 | const applicationId = context.switchToHttp().getRequest().headers['x-application']; |
19 | | - if (!applicationId) throw new AppException(ERROR_CODE.APPLICATION_HEADER_MISSING); |
20 | | - const application = await this.prisma.apiApplication.findUnique({ |
21 | | - where: { id: applicationId }, |
22 | | - }); |
23 | | - if (!application) { |
24 | | - throw new AppException(ERROR_CODE.NO_SUCH_APPLICATION, applicationId); |
| 24 | + let application: RawApiApplication | null = null; |
| 25 | + if (!applicationId && !canSkipApplicationHeader) throw new AppException(ERROR_CODE.APPLICATION_HEADER_MISSING); |
| 26 | + else if (applicationId) { |
| 27 | + application = await this.prisma.apiApplication.findUnique({ |
| 28 | + where: { id: applicationId }, |
| 29 | + }); |
| 30 | + if (!application) throw new AppException(ERROR_CODE.NO_SUCH_APPLICATION, applicationId); |
25 | 31 | } |
26 | 32 | // Check whether the user is logged in |
27 | 33 | let loggedIn = true; |
|
0 commit comments