29
29
30
30
import java .io .IOException ;
31
31
import java .util .Collections ;
32
+ import java .util .List ;
32
33
import java .util .Optional ;
33
34
import java .util .UUID ;
34
35
35
36
@ Slf4j
36
37
@ Service
37
- @ RequiredArgsConstructor
38
38
public class GoogleLoginService {
39
39
40
40
private final JwtTokenProvider jwtTokenProvider ;
41
41
private final UserRepository userRepository ;
42
42
private static final String GOOGLE_USER_INFO_URL = "https://www.googleapis.com/userinfo/v2/me" ;
43
43
private static final String GOOGLE_REVOKE_URL = "https://oauth2.googleapis.com/revoke?token=" ;
44
44
45
- @ Value ("${spring.security.oauth2.client.registration.google.client-id}" )
46
- private String clientId ;
45
+ private final List <String > validClientIds ;
46
+
47
+ public GoogleLoginService (
48
+ JwtTokenProvider jwtTokenProvider ,
49
+ UserRepository userRepository ,
50
+ @ Value ("${google.web.client-id}" ) String webClientId ,
51
+ @ Value ("${google.app.client-id}" ) String appClientId
52
+ ) {
53
+ this .jwtTokenProvider = jwtTokenProvider ;
54
+ this .userRepository = userRepository ;
55
+ this .validClientIds = List .of (webClientId , appClientId );
56
+ }
57
+
47
58
48
59
public Authentication handleLogin (OAuthGoogleRequestDto oAuthGoogleRequestDto , User user , HttpServletResponse response ) throws IOException {
49
60
user .updateSocialLoginToken (oAuthGoogleRequestDto .getRefreshToken ());
@@ -111,10 +122,14 @@ public Authentication handleRegister(OAuthGoogleRequestDto oAuthGoogleRequestDto
111
122
response .setContentType ("application/json" );
112
123
response .setCharacterEncoding ("UTF-8" );
113
124
125
+ String msg = savedUser .getRole ().name ().equals ("GUEST" ) ? "유저의 ROLE이 GUEST이므로 온보딩API를 호출해 온보딩을 진행해야합니다." : "로그인에 성공하였습니다." ;
126
+ // JSON 응답 생성
114
127
String responseBody = String .format (
115
- "{\" message\" : \" %s\" , \" role\" : \" %s\" }" ,
116
- "회원가입이 완료되었습니다. ROLE이 GUEST이므로 온보딩이 필요합니다." ,
117
- savedUser .getRole ().name ()
128
+ "{ \" status\" : \" success\" , \" code\" : \" 200\" , \" message\" : \" %s\" , \" data\" : { " +
129
+ "\" userId\" : %d, \" email\" : \" %s\" , \" name\" : \" %s\" , " +
130
+ "\" spareTime\" : %d, \" note\" : %s, \" punctualityScore\" : %f, \" role\" : \" %s\" } }" ,
131
+ msg , savedUser .getId (), savedUser .getEmail (), savedUser .getName (),
132
+ savedUser .getSpareTime (), savedUser .getNote () != null ? "\" " + savedUser .getNote () + "\" " : null , savedUser .getPunctualityScore (), savedUser .getRole ().name ()
118
133
);
119
134
120
135
response .getWriter ().write (responseBody );
@@ -127,7 +142,7 @@ public GoogleIdToken.Payload verifyIdentityToken(String identityToken) throws Ex
127
142
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier .Builder (
128
143
new NetHttpTransport (),
129
144
GsonFactory .getDefaultInstance ())
130
- .setAudience (Collections . singletonList ( clientId ) ) // aud 확인
145
+ .setAudience (validClientIds ) // aud 확인
131
146
.build ();
132
147
133
148
GoogleIdToken idToken = verifier .verify (identityToken ); // Google의 공개 키를 사용하여 idToken 서명을 검증
0 commit comments