-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSignUpService.java
More file actions
103 lines (88 loc) · 4.41 KB
/
SignUpService.java
File metadata and controls
103 lines (88 loc) · 4.41 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
95
96
97
98
99
100
101
102
103
package com.example.solidconnection.auth.service.signup;
import static com.example.solidconnection.common.exception.ErrorCode.NICKNAME_ALREADY_EXISTED;
import static com.example.solidconnection.common.exception.ErrorCode.SIGN_UP_TOKEN_INVALID;
import static com.example.solidconnection.common.exception.ErrorCode.USER_ALREADY_EXISTED;
import com.example.solidconnection.auth.dto.SignInResponse;
import com.example.solidconnection.auth.dto.SignUpRequest;
import com.example.solidconnection.auth.service.signin.SignInService;
import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.location.country.service.InterestedCountryService;
import com.example.solidconnection.location.region.service.InterestedRegionService;
import com.example.solidconnection.siteuser.domain.AuthType;
import com.example.solidconnection.siteuser.domain.Role;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.domain.UserStatus;
import com.example.solidconnection.siteuser.repository.SiteUserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/*
* 우리 서버에서 인증되었음을 확인하기 위한 signUpToken 을 검증한다.
* - 사용자 정보를 DB에 저장한다.
* - 관심 국가와 지역을 DB에 저장한다.
* - 관심 국가와 지역은 site_user_id를 참조하므로, 사용자 저장 후 저장한다.
* - 바로 로그인하도록 액세스 토큰과 리프레시 토큰을 발급한다.
* */
@Service
@RequiredArgsConstructor
public class SignUpService {
private final SignInService signInService;
private final SiteUserRepository siteUserRepository;
private final InterestedRegionService interestedRegionService;
private final InterestedCountryService interestedCountryService;
private final SignUpTokenProvider signUpTokenProvider;
private final PasswordTemporaryStorage passwordTemporaryStorage;
@Transactional
public SignInResponse signUp(SignUpRequest signUpRequest) {
// 검증
signUpTokenProvider.validateSignUpToken(signUpRequest.signUpToken());
String email = signUpTokenProvider.parseEmail(signUpRequest.signUpToken());
AuthType authType = signUpTokenProvider.parseAuthType(signUpRequest.signUpToken());
validateNicknameNotDuplicated(signUpRequest.nickname());
validateUserNotDuplicated(email, authType);
// 임시 저장된 비밀번호 가져오기
String password = getTemporarySavedPassword(email, authType);
// 사용자 저장
SiteUser siteUser = siteUserRepository.save(new SiteUser(
email,
signUpRequest.nickname(),
signUpRequest.profileImageUrl(),
signUpRequest.exchangeStatus(),
Role.MENTEE,
authType,
password,
UserStatus.ACTIVE
));
// 관심 지역, 국가 저장
interestedRegionService.saveInterestedRegion(siteUser, signUpRequest.interestedRegions());
interestedCountryService.saveInterestedCountry(siteUser, signUpRequest.interestedCountries());
// 로그인
SignInResponse response = signInService.signIn(siteUser);
// 회원가입을 위해 저장한 데이터(SignUpToken, 비밀번호) 삭제
clearSignUpData(email, authType);
return response;
}
private void validateNicknameNotDuplicated(String nickname) {
if (siteUserRepository.existsByNickname(nickname)) {
throw new CustomException(NICKNAME_ALREADY_EXISTED);
}
}
private void validateUserNotDuplicated(String email, AuthType authType) {
if (siteUserRepository.existsByEmailAndAuthType(email, authType)) {
throw new CustomException(USER_ALREADY_EXISTED);
}
}
private String getTemporarySavedPassword(String email, AuthType authType) {
if (authType == AuthType.EMAIL) {
return passwordTemporaryStorage.findByEmail(email)
.orElseThrow(() -> new CustomException(SIGN_UP_TOKEN_INVALID));
}
return null;
}
private void clearSignUpData(String email, AuthType authType) {
if (authType == AuthType.EMAIL) {
passwordTemporaryStorage.deleteByEmail(email);
}
signUpTokenProvider.deleteByEmail(email);
}
}