-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from DevKor-github/main
[Merge] main to deploy
- Loading branch information
Showing
8 changed files
with
133 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
@Getter | ||
public class OAuthGoogleRequestDto { | ||
private String accessToken; | ||
private String refreshToken; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
ontime-back/src/main/java/devkor/ontime_back/global/oauth/google/GoogleLoginService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package devkor.ontime_back.global.oauth.google; | ||
|
||
import devkor.ontime_back.dto.OAuthGoogleRequestDto; | ||
import devkor.ontime_back.dto.OAuthGoogleUserDto; | ||
import devkor.ontime_back.entity.Role; | ||
import devkor.ontime_back.entity.SocialType; | ||
import devkor.ontime_back.entity.User; | ||
import devkor.ontime_back.global.jwt.JwtTokenProvider; | ||
import devkor.ontime_back.repository.UserRepository; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class GoogleLoginService { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
private final UserRepository userRepository; | ||
private static final String GOOGLE_USER_INFO_URL = "https://www.googleapis.com/userinfo/v2/me"; | ||
|
||
|
||
public OAuthGoogleUserDto getUserInfoFromAccessToken(String accessToken) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Authorization", "Bearer " + accessToken); | ||
|
||
HttpEntity<String> entity = new HttpEntity<>(headers); | ||
|
||
ResponseEntity<OAuthGoogleUserDto> response = restTemplate.exchange( | ||
GOOGLE_USER_INFO_URL, | ||
org.springframework.http.HttpMethod.GET, | ||
entity, | ||
OAuthGoogleUserDto.class | ||
); | ||
|
||
return response.getBody(); | ||
} | ||
public Authentication handleLogin(OAuthGoogleRequestDto oAuthGoogleRequestDto, User user, HttpServletResponse response) throws IOException { | ||
user.updateSocialLoginToken(oAuthGoogleRequestDto.getRefreshToken()); | ||
|
||
String accessToken = jwtTokenProvider.createAccessToken(user.getEmail(), user.getId()); | ||
String refreshToken = jwtTokenProvider.createRefreshToken(); | ||
|
||
jwtTokenProvider.updateRefreshToken(user.getEmail(), refreshToken); | ||
jwtTokenProvider.sendAccessAndRefreshToken(response, accessToken, refreshToken); | ||
|
||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
|
||
String msg = user.getRole().name().equals("GUEST") ? "유저의 ROLE이 GUEST이므로 온보딩API를 호출해 온보딩을 진행해야합니다." : "로그인에 성공하였습니다."; | ||
// JSON 응답 생성 | ||
String responseBody = String.format( | ||
"{ \"status\": \"success\", \"code\": \"200\", \"message\": \"%s\", \"data\": { " + | ||
"\"userId\": %d, \"email\": \"%s\", \"name\": \"%s\", " + | ||
"\"spareTime\": %d, \"note\": \"%s\", \"punctualityScore\": %f, \"role\": \"%s\" } }", | ||
msg, user.getId(), user.getEmail(), user.getName(), | ||
user.getSpareTime(), user.getNote(), user.getPunctualityScore(), user.getRole().name() | ||
); | ||
|
||
response.getWriter().write(responseBody); | ||
response.getWriter().flush(); | ||
|
||
return new UsernamePasswordAuthenticationToken(user, null, Collections.singletonList(new SimpleGrantedAuthority(user.getRole().name()))); | ||
} | ||
|
||
public Authentication handleRegister(OAuthGoogleRequestDto oAuthGoogleRequestDto, OAuthGoogleUserDto oAuthGoogleUserDto, HttpServletResponse response) throws IOException { | ||
User newUser = User.builder() | ||
.socialType(SocialType.GOOGLE) | ||
.socialId(oAuthGoogleUserDto.getSub()) | ||
.email(oAuthGoogleUserDto.getEmail()) | ||
.name(oAuthGoogleUserDto.getName()) | ||
.imageUrl(oAuthGoogleUserDto.getPicture()) | ||
.role(Role.GUEST) | ||
.socialLoginToken(oAuthGoogleRequestDto.getRefreshToken()) | ||
.build(); | ||
|
||
User savedUser = userRepository.save(newUser); | ||
|
||
String accessToken = jwtTokenProvider.createAccessToken(newUser.getEmail(), newUser.getId()); | ||
jwtTokenProvider.sendAccessToken(response, accessToken); | ||
|
||
response.setContentType("application/json"); | ||
response.setCharacterEncoding("UTF-8"); | ||
|
||
String responseBody = String.format( | ||
"{\"message\": \"%s\", \"role\": \"%s\"}", | ||
"회원가입이 완료되었습니다. ROLE이 GUEST이므로 온보딩이 필요합니다.", | ||
savedUser.getRole().name() | ||
); | ||
|
||
response.getWriter().write(responseBody); | ||
response.getWriter().flush(); | ||
|
||
return new UsernamePasswordAuthenticationToken(newUser, null, Collections.singletonList(new SimpleGrantedAuthority(newUser.getRole().name()))); | ||
} | ||
|
||
|
||
} |