Skip to content

Commit

Permalink
refactor: user 조회 함수 통일화
Browse files Browse the repository at this point in the history
  • Loading branch information
Muungi committed Feb 26, 2024
1 parent 7415f76 commit a402f47
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class AuthService {
}

async validateUser(email: string, password: string): Promise<Record<string, string>> {
const user = await this.usersService.findOne(email);
const user = await this.usersService.findUserByEmail(email);
if (!user || !bcrypt.compareSync(password, user.password)) {
throw new ValidationException('로그인 실패');
}
Expand All @@ -47,7 +47,7 @@ export class AuthService {
}

async refreshJWT(userId: string): Promise<Record<string, string>> {
const user = await this.usersService.getUserById(userId);
const user = await this.usersService.findUserById(userId);
if (!user) {
throw new HttpException('유효하지 않은 refreshToken', HttpStatus.BAD_REQUEST);
}
Expand Down
24 changes: 13 additions & 11 deletions backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export class UsersService {
}
}

async findOne(email: string): Promise<User | null> {
async findUserByEmail(email: string): Promise<User | null> {
const user = await this.usersRepository.findOne({ where: { email } });
return user;
}

async getUserById(userId: string): Promise<User | null> {
async findUserById(userId: string): Promise<User | null> {
return await this.usersRepository.findOne({ where: { id: userId } });
}

Expand All @@ -65,30 +65,32 @@ export class UsersService {
}

async sendVerificationEmail(email: string) {
await this.findUserByEmail(email);
const user = await this.findUserByEmail(email);
if (!user) {
throw new HttpException('해당 이메일의 사용자를 찾을 수 없습니다.', HttpStatus.NOT_FOUND);
}
await this.mailService.sendVerficationCode(email);
}

async checkEmailVarifacted(email: string) {
const user = await this.findUserByEmail(email);
if (!user) {
throw new HttpException('해당 이메일의 사용자를 찾을 수 없습니다.', HttpStatus.NOT_FOUND);
}
if (!user.verified) {
return false;
}
return true;
}

async changePassword(email: string, password: string) {
const user = await this.findUserByEmail(email);
if (!user) {
throw new ValidationException('비밀번호 변경 실패');
}
const salt = await bcrypt.genSalt();
const hashedPassword = await bcrypt.hash(password, salt);
user.password = hashedPassword;
await this.usersRepository.save(user);
}

async findUserByEmail(email: string) {
const user = await this.usersRepository.findOne({ where: { email } });
if (!user) {
throw new HttpException('해당 이메일을 찾을 수 없습니다.', HttpStatus.NOT_FOUND);
}
return user;
}
}

0 comments on commit a402f47

Please sign in to comment.