refactor(video-studio): ♻️ Standardize API controllers and guards for video-studio by reorganizing face-disguise, invisible-protect, jobs, and library components and updating module registration
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
parent
d113301c90
commit
0017f67690
6 changed files with 62 additions and 15 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { StandaloneAuthModule } from '@lilith/nestjs-auth';
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { APP_GUARD } from '@nestjs/core';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { MediaGalleryModule } from '@/clients/media-gallery/media-gallery.module';
|
||||
|
|
@ -10,6 +11,7 @@ import { InvisibleProtectModule } from '@/invisible-protect/invisible-protect.mo
|
|||
import { JobsModule } from '@/jobs/jobs.module';
|
||||
import { LibraryModule } from '@/library/library.module';
|
||||
|
||||
import { AppJwtGuard } from './guards/jwt.guard';
|
||||
import { HealthController } from './health.controller';
|
||||
|
||||
@Module({
|
||||
|
|
@ -19,7 +21,8 @@ import { HealthController } from './health.controller';
|
|||
envFilePath: ['.env.local', '.env'],
|
||||
}),
|
||||
|
||||
StandaloneAuthModule.forRoot({
|
||||
JwtModule.register({
|
||||
global: true,
|
||||
secret: process.env['JWT_SECRET'] || 'dev-jwt-secret-change-in-production',
|
||||
signOptions: { expiresIn: '24h' },
|
||||
}),
|
||||
|
|
@ -41,7 +44,7 @@ import { HealthController } from './health.controller';
|
|||
password: dbConfig.password,
|
||||
database: dbConfig.database,
|
||||
autoLoadEntities: true,
|
||||
synchronize: false,
|
||||
synchronize: config.get('NODE_ENV') !== 'production',
|
||||
logging: config.get('NODE_ENV') !== 'production',
|
||||
};
|
||||
},
|
||||
|
|
@ -55,5 +58,8 @@ import { HealthController } from './health.controller';
|
|||
LibraryModule,
|
||||
],
|
||||
controllers: [HealthController],
|
||||
providers: [
|
||||
{ provide: APP_GUARD, useClass: AppJwtGuard },
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { JwtStandaloneGuard, type JwtUserPayload } from '@lilith/nestjs-auth';
|
||||
import { Body, Controller, HttpCode, HttpStatus, Post, Request, UseGuards } from '@nestjs/common';
|
||||
import { type JwtUserPayload } from '@lilith/nestjs-auth';
|
||||
import { Body, Controller, HttpCode, HttpStatus, Post, Request } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import type { Request as ExpressRequest } from 'express';
|
||||
|
|
@ -10,7 +10,6 @@ import { JobStatusDto } from '@/jobs/dto/job-status.dto';
|
|||
|
||||
@ApiTags('video-studio')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtStandaloneGuard)
|
||||
@Controller('video-studio')
|
||||
export class FaceDisguiseController {
|
||||
constructor(private readonly service: FaceDisguiseService) {}
|
||||
|
|
|
|||
46
features/video-studio/backend-api/src/guards/jwt.guard.ts
Normal file
46
features/video-studio/backend-api/src/guards/jwt.guard.ts
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
|
||||
import type { Request } from 'express';
|
||||
|
||||
const IS_PUBLIC_KEY = 'isPublic';
|
||||
|
||||
@Injectable()
|
||||
export class AppJwtGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly jwtService: JwtService,
|
||||
private readonly reflector: Reflector,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const isPublic = this.reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
|
||||
if (isPublic) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
const token = this.extractTokenFromHeader(request);
|
||||
|
||||
if (!token) {
|
||||
throw new UnauthorizedException('Missing authorization token');
|
||||
}
|
||||
|
||||
try {
|
||||
request.user = await this.jwtService.verifyAsync(token);
|
||||
} catch {
|
||||
throw new UnauthorizedException('Invalid token');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private extractTokenFromHeader(request: Request): string | undefined {
|
||||
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
||||
return type === 'Bearer' ? token : undefined;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { JwtStandaloneGuard, Public, type JwtUserPayload } from '@lilith/nestjs-auth';
|
||||
import { Public, type JwtUserPayload } from '@lilith/nestjs-auth';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
|
|
@ -9,7 +9,6 @@ import {
|
|||
ParseUUIDPipe,
|
||||
Post,
|
||||
Request,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
|
|
@ -19,9 +18,9 @@ import { CreateProtectJobDto } from '@/invisible-protect/dto/create-protect-job.
|
|||
import { ProtectJobStatusDto } from '@/invisible-protect/dto/protect-job-status.dto';
|
||||
import { InvisibleProtectService } from '@/invisible-protect/invisible-protect.service';
|
||||
|
||||
// Auth handled globally via StandaloneAuthModule APP_GUARD
|
||||
@ApiTags('video-studio')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtStandaloneGuard)
|
||||
@Controller('video-studio')
|
||||
export class InvisibleProtectController {
|
||||
constructor(private readonly service: InvisibleProtectService) {}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { JwtStandaloneGuard, type JwtUserPayload } from '@lilith/nestjs-auth';
|
||||
import { Controller, Get, Param, ParseUUIDPipe, Request, UseGuards } from '@nestjs/common';
|
||||
import { type JwtUserPayload } from '@lilith/nestjs-auth';
|
||||
import { Controller, Get, Param, ParseUUIDPipe, Request } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import type { Request as ExpressRequest } from 'express';
|
||||
|
|
@ -9,7 +9,6 @@ import { JobsService } from '@/jobs/jobs.service';
|
|||
|
||||
@ApiTags('video-studio')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtStandaloneGuard)
|
||||
@Controller('video-studio')
|
||||
export class JobsController {
|
||||
constructor(private readonly service: JobsService) {}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
import { JwtStandaloneGuard } from '@lilith/nestjs-auth';
|
||||
import { Controller, Get, UseGuards } from '@nestjs/common';
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
|
||||
|
||||
import { PhotoItem } from '@/clients/media-gallery/media-gallery.types';
|
||||
|
|
@ -7,7 +6,6 @@ import { LibraryService } from '@/library/library.service';
|
|||
|
||||
@ApiTags('video-studio')
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtStandaloneGuard)
|
||||
@Controller('video-studio')
|
||||
export class LibraryController {
|
||||
constructor(private readonly service: LibraryService) {}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue