Skip to content

Commit

Permalink
refactor: user config 구현 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
yubinquitous committed Nov 9, 2023
1 parent e77e8ec commit 12ee18c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { AuthModule } from './auth/auth.module';
import ftConfig from './config/ft.config';
import { typeOrmConfig } from './config/typeorm.config';
import { UsersModule } from './users/users.module';
import userConfig from './config/user.config';

@Module({
imports: [
TypeOrmModule.forRootAsync(typeOrmConfig),
ConfigModule.forRoot({
isGlobal: true,
envFilePath: '.env',
load: [ftConfig],
envFilePath: './BE-config/.env',
load: [ftConfig, userConfig],
}),
AuthModule,
UsersModule,
Expand Down
1 change: 1 addition & 0 deletions src/auth/auth-42.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class Auth42Service {
fortyTwoId: response.data.id,
};
this.logger.log('user: ', userData);

return userData;
} catch (error) {
this.logger.error(error);
Expand Down
15 changes: 9 additions & 6 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ConfigService } from '@nestjs/config';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { ConfigType } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import userConfig from 'src/config/user.config';
import { User } from 'src/users/entities/user.entity';
import { UserRepository } from './../users/users.repository';
import { User42Dto } from './dto/user-42.dto';
Expand All @@ -17,11 +18,10 @@ export class AuthService {
constructor(
private readonly userRepository: UserRepository,
private readonly jwtService: JwtService,
private readonly configService: ConfigService,
@Inject(userConfig.KEY)
private userConfigure: ConfigType<typeof userConfig>,
) {
this.nicknamePrefix = this.configService.getOrThrow<string>(
'FIRST_NICKNAME_PREFIX',
);
this.nicknamePrefix = this.userConfigure.FIRST_NICKNAME_PREFIX;
}

private async createMfaCode(): Promise<string> {
Expand Down Expand Up @@ -53,10 +53,13 @@ export class AuthService {
const payload = { id: user.id, email: user.email };
// accessToken 생성
const accessToken = await this.jwtService.signAsync(payload);

// refreshToken 생성
const refreshToken = await this.jwtService.signAsync({ id: payload.id });

// refreshToken을 DB에 저장한다.
await this.userRepository.update(user.id, { refreshToken });

return { jwtAccessToken: accessToken, jwtRefreshToken: refreshToken };
}

Expand Down
14 changes: 14 additions & 0 deletions src/config/user.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { registerAs } from '@nestjs/config';
import { z } from 'zod';

export const USER_CONFIG = 'user';

export default registerAs(USER_CONFIG, () => {
const FIRST_NICKNAME_PREFIX = z
.string()
.parse(process.env.FIRST_NICKNAME_PREFIX);

return {
FIRST_NICKNAME_PREFIX,
};
});

0 comments on commit 12ee18c

Please sign in to comment.