diff --git a/src/app.module.ts b/src/app.module.ts index f06a32e..5c6228a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -5,9 +5,11 @@ import { AppController } from './app.controller'; import { AppService } from './app.service'; import { AuthModule } from './auth/auth.module'; import ftConfig from './config/ft.config'; +import jwtConfig from './config/jwt.config'; +import pgadminConfig from './config/pgadmin.config'; import { typeOrmConfig } from './config/typeorm.config'; -import { UsersModule } from './users/users.module'; import userConfig from './config/user.config'; +import { UsersModule } from './users/users.module'; @Module({ imports: [ @@ -15,7 +17,7 @@ import userConfig from './config/user.config'; ConfigModule.forRoot({ isGlobal: true, envFilePath: './BE-config/.env', - load: [ftConfig, userConfig], + load: [ftConfig, userConfig, pgadminConfig, jwtConfig], }), AuthModule, UsersModule, diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index e31fcbf..6b68dcf 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -3,6 +3,7 @@ import { Controller, HttpException, HttpStatus, + Inject, Logger, Post, Res, @@ -10,9 +11,10 @@ import { UseGuards, UseInterceptors, } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; +import { ConfigType } from '@nestjs/config'; import { FileInterceptor } from '@nestjs/platform-express'; import { Response } from 'express'; +import userConfig from 'src/config/user.config'; import { User } from 'src/users/entities/user.entity'; import { UsersService } from './../users/users.service'; import { Auth42Service } from './auth-42.service'; @@ -29,11 +31,10 @@ export class AuthController { private readonly authService: AuthService, private readonly auth42Service: Auth42Service, private readonly usersService: UsersService, - private readonly configService: ConfigService, + @Inject(userConfig.KEY) + private readonly userConfigure: ConfigType, ) { - this.nicknamePrefix = this.configService.getOrThrow( - 'FIRST_NICKNAME_PREFIX', - ); + this.nicknamePrefix = this.userConfigure.FIRST_NICKNAME_PREFIX; } private readonly logger = new Logger(AuthController.name); diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index 7e2988d..7b17f95 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -1,23 +1,27 @@ +import { Module } from '@nestjs/common'; +import { ConfigType } from '@nestjs/config'; import { JwtModule } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; import { MulterModule } from '@nestjs/platform-express'; import { TypeOrmModule } from '@nestjs/typeorm'; -import { jwtConfig } from '../config/jwt.config'; +import jwtConfig from '../config/jwt.config'; import { multerConfig } from '../config/multer.config'; +import { User } from '../users/entities/user.entity'; +import { UserRepository } from '../users/users.repository'; +import { UsersService } from '../users/users.service'; import { Auth42Service } from './auth-42.service'; import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; import { JwtAccessStrategy } from './jwt-access.strategy'; -import { User } from '../users/entities/user.entity'; -import { UserRepository } from '../users/users.repository'; -import { UsersService } from '../users/users.service'; -import { Module } from '@nestjs/common'; @Module({ imports: [ TypeOrmModule.forFeature([User]), PassportModule, - JwtModule.registerAsync(jwtConfig), + JwtModule.registerAsync({ + inject: [jwtConfig.KEY], + useFactory: (jwtConfigure: ConfigType) => jwtConfigure, + }), MulterModule.registerAsync(multerConfig), ], controllers: [AuthController], diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index a359453..d45aa9c 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -19,7 +19,7 @@ export class AuthService { private readonly userRepository: UserRepository, private readonly jwtService: JwtService, @Inject(userConfig.KEY) - private userConfigure: ConfigType, + private readonly userConfigure: ConfigType, ) { this.nicknamePrefix = this.userConfigure.FIRST_NICKNAME_PREFIX; } diff --git a/src/auth/jwt-access.strategy.ts b/src/auth/jwt-access.strategy.ts index 37d5c78..16745fb 100644 --- a/src/auth/jwt-access.strategy.ts +++ b/src/auth/jwt-access.strategy.ts @@ -1,8 +1,9 @@ -import { Injectable, UnauthorizedException } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; +import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'; +import { ConfigType } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { InjectRepository } from '@nestjs/typeorm'; import { ExtractJwt, Strategy } from 'passport-jwt'; +import jwtConfig from 'src/config/jwt.config'; import { User } from 'src/users/entities/user.entity'; import { UserRepository } from 'src/users/users.repository'; @@ -15,10 +16,11 @@ export class JwtAccessStrategy extends PassportStrategy(Strategy) { constructor( @InjectRepository(UserRepository) private readonly userRepository: UserRepository, - private readonly configService: ConfigService, + @Inject(jwtConfig.KEY) + private readonly jwtConfigure: ConfigType, ) { super({ - secretOrKey: configService.getOrThrow('JWT_SECRET'), + secretOrKey: jwtConfigure.secret, jwtFromRequest: ExtractJwt.fromExtractors([ (request) => request.cookies?.accessToken, ]), diff --git a/src/config/jwt.config.ts b/src/config/jwt.config.ts index ad950cd..1500865 100644 --- a/src/config/jwt.config.ts +++ b/src/config/jwt.config.ts @@ -1,13 +1,15 @@ -import { ConfigService, ConfigModule } from '@nestjs/config'; -import { JwtModuleAsyncOptions } from '@nestjs/jwt'; +import { registerAs } from '@nestjs/config'; +import type { JwtModuleOptions } from '@nestjs/jwt'; +import { z } from 'zod'; -export const jwtConfig: JwtModuleAsyncOptions = { - imports: [ConfigModule], - inject: [ConfigService], - useFactory: (configService: ConfigService) => ({ - secret: configService.get('JWT_SECRET'), +export default registerAs('jwt', () => { + const secret = z.string().parse(process.env.JWT_SECRET); + const expiresIn = z.string().parse(process.env.JWT_EXPIRATION_TIME); + + return { + secret, signOptions: { - expiresIn: configService.get('JWT_EXPIRATION_TIME'), + expiresIn, }, - }), -}; + } satisfies JwtModuleOptions; +}); diff --git a/src/config/pgadmin.config.ts b/src/config/pgadmin.config.ts new file mode 100644 index 0000000..b79751a --- /dev/null +++ b/src/config/pgadmin.config.ts @@ -0,0 +1,19 @@ +import { registerAs } from '@nestjs/config'; +import { z } from 'zod'; + +export const PGADMIN_CONFIG = 'pgadmin'; + +export default registerAs(PGADMIN_CONFIG, () => { + const PGADMIN_DEFAULT_EMAIL = z + .string() + .parse(process.env.PGADMIN_DEFAULT_EMAIL); + + const PGADMIN_DEFAULT_PASSWORD = z + .string() + .parse(process.env.PGADMIN_DEFAULT_PASSWORD); + + return { + PGADMIN_DEFAULT_EMAIL, + PGADMIN_DEFAULT_PASSWORD, + }; +});