1
1
package swm .betterlife .antifragile .domain .auth .service ;
2
2
3
+ import static swm .betterlife .antifragile .common .util .S3ImageCategory .PROFILE ;
3
4
import static swm .betterlife .antifragile .domain .member .entity .RoleType .ROLE_USER ;
4
5
5
6
import com .mongodb .client .result .UpdateResult ;
6
7
import java .time .LocalDateTime ;
8
+ import java .util .Optional ;
7
9
import lombok .RequiredArgsConstructor ;
8
10
import lombok .extern .slf4j .Slf4j ;
9
11
import org .springframework .beans .factory .annotation .Value ;
17
19
import org .springframework .security .crypto .password .PasswordEncoder ;
18
20
import org .springframework .stereotype .Service ;
19
21
import org .springframework .transaction .annotation .Transactional ;
22
+ import org .springframework .web .multipart .MultipartFile ;
20
23
import swm .betterlife .antifragile .common .exception .MemberNotFoundException ;
21
24
import swm .betterlife .antifragile .common .jwt .util .JwtProvider ;
25
+ import swm .betterlife .antifragile .common .util .S3ImageComponent ;
22
26
import swm .betterlife .antifragile .domain .auth .dto .request .AuthLoginRequest ;
23
27
import swm .betterlife .antifragile .domain .auth .dto .request .AuthLogoutRequest ;
24
28
import swm .betterlife .antifragile .domain .auth .dto .request .AuthSignUpRequest ;
@@ -48,25 +52,26 @@ public class AuthService {
48
52
private final PasswordEncoder passwordEncoder ;
49
53
private final TokenService tokenService ;
50
54
private final MongoTemplate mongoTemplate ;
55
+ private final S3ImageComponent s3ImageComponent ;
51
56
52
57
@ Transactional
53
58
public AuthLoginResponse login (AuthLoginRequest authLoginRequest ) {
54
- String username
55
- = authLoginRequest .loginType ().name () + ":" + authLoginRequest .email (); //todo: common분리
56
59
String password = authLoginRequest .password ();
57
60
58
- Authentication authentication = getAuthenticate (username , password );
61
+ Authentication authentication
62
+ = getAuthenticate (authLoginRequest .email (), password , authLoginRequest .loginType ());
59
63
60
64
Member member = memberRepository .getMember (
61
65
authLoginRequest .email (), authLoginRequest .loginType ()
62
66
);
63
- TokenIssueResponse tokenIssueResponse
64
- = jwtProvider .issueToken (authentication );
65
- return AuthLoginResponse .from (member , tokenIssueResponse );
67
+ TokenIssueResponse tokenIssue = jwtProvider .issueToken (authentication );
68
+ return AuthLoginResponse .from (member , tokenIssue );
66
69
}
67
70
68
71
@ Transactional
69
- public AuthSignUpResponse signUp (AuthSignUpRequest authSignUpRequest ) {
72
+ public AuthSignUpResponse signUp (
73
+ AuthSignUpRequest authSignUpRequest , MultipartFile profileImgFile
74
+ ) {
70
75
if (memberRepository .existsByEmailAndLoginType (
71
76
authSignUpRequest .email (), authSignUpRequest .loginType ())
72
77
) {
@@ -77,14 +82,27 @@ public AuthSignUpResponse signUp(AuthSignUpRequest authSignUpRequest) {
77
82
String encodedPassword = passwordEncoder .encode (password );
78
83
79
84
Member member = Member .builder ()
80
- .email (authSignUpRequest .email ())
81
- .password (encodedPassword )
82
- .loginType (authSignUpRequest .loginType ())
83
- .roleType (ROLE_USER )
84
- .build ();
85
-
86
- return AuthSignUpResponse .from (memberRepository .save (member ));
87
-
85
+ .email (authSignUpRequest .email ())
86
+ .password (encodedPassword )
87
+ .loginType (authSignUpRequest .loginType ())
88
+ .age (authSignUpRequest .age ())
89
+ .gender (authSignUpRequest .gender ())
90
+ .job (authSignUpRequest .job ())
91
+ .profileImgFilename (
92
+ Optional .ofNullable (profileImgFile )
93
+ .map (file -> s3ImageComponent .uploadImage (PROFILE , file ))
94
+ .orElse (null )
95
+ )
96
+ .roleType (ROLE_USER )
97
+ .build ();
98
+
99
+ Member savedMember = memberRepository .save (member );
100
+
101
+ Authentication authentication =
102
+ getAuthenticate (authSignUpRequest .email (), password , authSignUpRequest .loginType ());
103
+ TokenIssueResponse tokenIssue = jwtProvider .issueToken (authentication );
104
+
105
+ return AuthSignUpResponse .from (savedMember , tokenIssue );
88
106
}
89
107
90
108
@ Transactional
@@ -105,7 +123,11 @@ public void delete(String memberId) {
105
123
}
106
124
}
107
125
108
- private Authentication getAuthenticate (String username , String password ) {
126
+ private Authentication getAuthenticate (
127
+ String email , String password ,
128
+ LoginType loginType
129
+ ) {
130
+ String username = loginType .name () + ":" + email ;
109
131
UsernamePasswordAuthenticationToken authenticationToken
110
132
= new UsernamePasswordAuthenticationToken (username , password );
111
133
return authenticationManagerBuilder .getObject ().authenticate (authenticationToken );
0 commit comments