Skip to content

Commit

Permalink
refactor: jwt config 구현 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
yubinquitous committed Nov 9, 2023
1 parent 12ee18c commit 0067593
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 28 deletions.
6 changes: 4 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ 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: [
TypeOrmModule.forRootAsync(typeOrmConfig),
ConfigModule.forRoot({
isGlobal: true,
envFilePath: './BE-config/.env',
load: [ftConfig, userConfig],
load: [ftConfig, userConfig, pgadminConfig, jwtConfig],
}),
AuthModule,
UsersModule,
Expand Down
11 changes: 6 additions & 5 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import {
Controller,
HttpException,
HttpStatus,
Inject,
Logger,
Post,
Res,
UploadedFile,
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';
Expand All @@ -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<typeof userConfig>,
) {
this.nicknamePrefix = this.configService.getOrThrow<string>(
'FIRST_NICKNAME_PREFIX',
);
this.nicknamePrefix = this.userConfigure.FIRST_NICKNAME_PREFIX;
}
private readonly logger = new Logger(AuthController.name);

Expand Down
16 changes: 10 additions & 6 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -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<typeof jwtConfig>) => jwtConfigure,
}),
MulterModule.registerAsync(multerConfig),
],
controllers: [AuthController],
Expand Down
2 changes: 1 addition & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class AuthService {
private readonly userRepository: UserRepository,
private readonly jwtService: JwtService,
@Inject(userConfig.KEY)
private userConfigure: ConfigType<typeof userConfig>,
private readonly userConfigure: ConfigType<typeof userConfig>,
) {
this.nicknamePrefix = this.userConfigure.FIRST_NICKNAME_PREFIX;
}
Expand Down
10 changes: 6 additions & 4 deletions src/auth/jwt-access.strategy.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<typeof jwtConfig>,
) {
super({
secretOrKey: configService.getOrThrow<string>('JWT_SECRET'),
secretOrKey: jwtConfigure.secret,
jwtFromRequest: ExtractJwt.fromExtractors([
(request) => request.cookies?.accessToken,
]),
Expand Down
22 changes: 12 additions & 10 deletions src/config/jwt.config.ts
Original file line number Diff line number Diff line change
@@ -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<string>('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<string>('JWT_EXPIRATION_TIME'),
expiresIn,
},
}),
};
} satisfies JwtModuleOptions;
});
19 changes: 19 additions & 0 deletions src/config/pgadmin.config.ts
Original file line number Diff line number Diff line change
@@ -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,
};
});

0 comments on commit 0067593

Please sign in to comment.