diff --git a/.gitignore b/.gitignore index b986ce5..ffd97e3 100644 --- a/.gitignore +++ b/.gitignore @@ -33,8 +33,5 @@ lerna-debug.log* !.vscode/launch.json !.vscode/extensions.json -#auth secrets -/src/constants.ts - #env file .env \ No newline at end of file diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index 5c9cdbb..5b3b27a 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -5,20 +5,33 @@ import { JwtStrategy } from './jwt.strategy'; import { UsersModule } from '../users/users.module'; import { PassportModule } from '@nestjs/passport'; import { JwtModule } from '@nestjs/jwt'; -import { jwtConstants } from 'src/constants'; -import { APP_GUARD } from '@nestjs/core'; import { RoleGuard } from './role.guard'; +import { ConfigService } from '@nestjs/config'; @Module({ imports: [ UsersModule, PassportModule, - JwtModule.register({ - secret: jwtConstants.secret, - signOptions: { expiresIn: '60s' }, //60초 이후 토큰만료 + //.env에서 값 사용을 위함. + JwtModule.registerAsync({ + useFactory: (configService: ConfigService) => { + return { + secret: configService.get('jwtConstants.secret'), + signOptions: { + expiresIn: configService.get('expirationTime'), + }, + }; + }, + inject: [ConfigService], }), ], - providers: [AuthService, LocalStrategy, JwtStrategy, RoleGuard], + providers: [ + AuthService, + LocalStrategy, + JwtStrategy, + RoleGuard, + ConfigService, + ], exports: [AuthService], }) export class AuthModule {} diff --git a/src/auth/jwt.strategy.ts b/src/auth/jwt.strategy.ts index 6c3cfe7..d1ca999 100644 --- a/src/auth/jwt.strategy.ts +++ b/src/auth/jwt.strategy.ts @@ -1,15 +1,15 @@ import { ExtractJwt, Strategy } from 'passport-jwt'; import { PassportStrategy } from '@nestjs/passport'; import { Injectable } from '@nestjs/common'; -import { jwtConstants } from 'src/constants'; +import { ConfigService } from '@nestjs/config'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { - constructor() { + constructor(private readonly configService: ConfigService) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, - secretOrKey: jwtConstants.secret, + secretOrKey: configService.get('jwtConstants.secret'), }); } diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 770dded..c7dd547 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -7,4 +7,11 @@ export default () => ({ password: process.env.DATABASE_PASSWORD || '', port: process.env.DATABASE_PORT || '', }, + jwtConstants: { + secret: process.env.JWT_SECRET_KEY || '', + }, + bcryptConstant: { + saltOrRounds: Number(process.env.BCRYPT_CONSTANT_VALUE), + }, + expirationTime: process.env.JWT_EXPIRATION_TIME, }); diff --git a/src/users/users.module.ts b/src/users/users.module.ts index f8cc930..3f4775d 100644 --- a/src/users/users.module.ts +++ b/src/users/users.module.ts @@ -1,10 +1,11 @@ import { Module } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { TypeOrmModule } from '@nestjs/typeorm'; import { User } from './entities/user.entity'; import { UsersService } from './users.service'; @Module({ - imports: [TypeOrmModule.forFeature([User])], + imports: [TypeOrmModule.forFeature([User]), ConfigService], providers: [UsersService], exports: [UsersService], }) diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 4813288..5c696b7 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -4,9 +4,9 @@ import { Repository } from 'typeorm'; import { CreateUserDto } from './dto/create-user.dto'; import { User } from './entities/user.entity'; import * as bcrypt from 'bcrypt'; -import { bcryptConstant } from 'src/constants'; import { LoginUserDto } from './dto/login-user.dto'; import { UpdateUserDto } from './dto/update-user.dto'; +import { ConfigService } from '@nestjs/config'; @Injectable() export class UsersService { @@ -16,6 +16,7 @@ export class UsersService { //Test 할 때 Repository만 바꿔주면 testDB에 쿼리를 날릴 수 있어서 편리하다. @InjectRepository(User) private usersRepository: Repository, + private configservice: ConfigService, ) {} async findOne(userId: string) { @@ -82,7 +83,7 @@ export class UsersService { const hashedPassword = await bcrypt.hash( userData.userPassword, - bcryptConstant.saltOrRounds, + this.configservice.get('bcryptConstant.saltOrRounds'), ); await this.usersRepository.save({ //usersRepository.save가 DB에 저장시키는거 @@ -93,7 +94,7 @@ export class UsersService { } // 회원 삭제 logic async deleteUser(deleteData: LoginUserDto) { - const isMatch = this.checkLoginData(deleteData); + const isMatch = await this.checkLoginData(deleteData); if (isMatch) { return await this.usersRepository.delete({ userId: deleteData.userId }); } else { @@ -117,7 +118,7 @@ export class UsersService { else { const hashedPassword = await bcrypt.hash( updateData.userPassword, - bcryptConstant.saltOrRounds, + this.configservice.get('bcryptConstant.saltOrRounds'), ); updateData.userPassword = hashedPassword; }