Repository navigation
Expand file tree
/
Copy pathauth.service.js
More file actions
94 lines (78 loc) · 2.72 KB
/
Copy pathauth.service.js
File metadata and controls
94 lines (78 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { DB_ERROR, ERROR_MESSAGE } from '#constants';
import { ConflictException, UnauthorizedException } from '#exceptions';
export class AuthService {
#userRepository;
#passwordProvider;
#tokenProvider;
constructor({ userRepository, passwordProvider, tokenProvider }) {
this.#userRepository = userRepository;
this.#passwordProvider = passwordProvider;
this.#tokenProvider = tokenProvider;
}
//회원가입 비즈니스 로직
async signUp({ email, password, nickname }) {
//이메일 중복 검증
const existingUser = await this.#userRepository.findByEmail(email);
if (existingUser) {
throw new ConflictException(ERROR_MESSAGE.EMAIL_ALREADY_EXISTS);
}
//닉네임 중복 검증
const existNickname = await this.#userRepository.findByNickname(nickname);
if (existNickname) {
throw new ConflictException(ERROR_MESSAGE.NICKNAME_ALREADY_EXISTS);
}
//패스워드 해시 진행 (회원가입이니까)
const hashedPassword = await this.#passwordProvider.hash(password);
//회원 등록
try {
const user = await this.#userRepository.create({
email,
password: hashedPassword,
nickname,
});
//토큰 2개 발급
const tokens = this.#tokenProvider.generateTokens(user);
return { user, tokens };
} catch (error) {
if (error.sqlState === DB_ERROR.UNIQUE_VIOLATION) {
throw new ConflictException(ERROR_MESSAGE.RESOURCE_CONFLICT);
}
throw error;
}
}
//로그인 비즈니스 로직
async logIn({ email, password }) {
//유저가 존재하는지 이메일로 확인
const user = await this.#userRepository.findByEmail(email, {
includePassword: true,
});
if (!user || !user.password) {
throw new UnauthorizedException(ERROR_MESSAGE.INVALID_CREDENTIALS);
}
//패스워드 검증
const isPasswordValid = await this.#passwordProvider.compare(
password,
user.password,
);
if (!isPasswordValid) {
throw new UnauthorizedException(ERROR_MESSAGE.INVALID_CREDENTIALS);
}
//토큰 2개 발급
const tokens = this.#tokenProvider.generateTokens(user);
const { password: _password, ...rest } = user;
return { user: rest, tokens };
}
//새 토큰 발급
async refreshTokens(refreshToken) {
const payload = this.#tokenProvider.verifyRefreshToken(refreshToken);
if (!payload) {
throw new UnauthorizedException(ERROR_MESSAGE.INVALID_TOKEN);
}
const user = await this.#userRepository.findById(payload.userId);
if (!user) {
throw new UnauthorizedException(ERROR_MESSAGE.USER_NOT_FOUND_FROM_TOKEN);
}
const tokens = this.#tokenProvider.generateTokens(user);
return { user, tokens };
}
}