Skip to content

Commit

Permalink
mv constants.ts to .env and Complete CRUD on USER
Browse files Browse the repository at this point in the history
  • Loading branch information
rnin9 committed Aug 5, 2021
1 parent 227443c commit f8b35aa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,5 @@ lerna-debug.log*
!.vscode/launch.json
!.vscode/extensions.json

#auth secrets
/src/constants.ts

#env file
.env
25 changes: 19 additions & 6 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('jwtConstants.secret'),
signOptions: {
expiresIn: configService.get<string | number>('expirationTime'),
},
};
},
inject: [ConfigService],
}),
],
providers: [AuthService, LocalStrategy, JwtStrategy, RoleGuard],
providers: [
AuthService,
LocalStrategy,
JwtStrategy,
RoleGuard,
ConfigService,
],
exports: [AuthService],
})
export class AuthModule {}
6 changes: 3 additions & 3 deletions src/auth/jwt.strategy.ts
Original file line number Diff line number Diff line change
@@ -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<string>('jwtConstants.secret'),
});
}

Expand Down
7 changes: 7 additions & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
3 changes: 2 additions & 1 deletion src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -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],
})
Expand Down
9 changes: 5 additions & 4 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +16,7 @@ export class UsersService {
//Test 할 때 Repository만 바꿔주면 testDB에 쿼리를 날릴 수 있어서 편리하다.
@InjectRepository(User)
private usersRepository: Repository<User>,
private configservice: ConfigService,
) {}

async findOne(userId: string) {
Expand Down Expand Up @@ -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에 저장시키는거
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit f8b35aa

Please sign in to comment.