Skip to content

Commit

Permalink
Merge pull request #272 from team9502/dev
Browse files Browse the repository at this point in the history
๋ฐฐํฌ
  • Loading branch information
daeundada authored Jul 1, 2024
2 parents ec47d52 + fff6af9 commit 2802ad1
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import team9502.sinchulgwinong.domain.auth.dto.request.UserSignupRequestDTO;
import team9502.sinchulgwinong.domain.oauth.dto.request.SocialLoginRequestDTO;
import team9502.sinchulgwinong.domain.oauth.enums.SocialType;
import team9502.sinchulgwinong.domain.oauth.service.SocialLoginService;
import team9502.sinchulgwinong.global.response.GlobalApiResponse;
Expand Down Expand Up @@ -39,7 +39,7 @@ public class SocialLoginController {
examples = @ExampleObject(value = "{ \"code\": \"500\", \"message\": \"์„œ๋ฒ„ ์—๋Ÿฌ\", \"data\": null }")))
})
public ResponseEntity<GlobalApiResponse<Object>> submitAdditionalInfo(
@RequestBody @Valid UserSignupRequestDTO requestDTO) {
@RequestBody @Valid SocialLoginRequestDTO requestDTO) {

socialLoginService.createOrUpdateSocialLogin(requestDTO, SocialType.GOOGLE);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package team9502.sinchulgwinong.domain.oauth.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class SocialLoginRequestDTO {

@NotBlank(message = "์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
@Schema(description = "๊ตฌ๊ธ€์—์„œ ์ œ๊ณต๋ฐ›์€ ์ฝ”๋“œ", example = "4/0AX4XfW....")
private String code;

@NotBlank(message = "์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
@Schema(description = "์‚ฌ์šฉ์ž ์ด๋ฆ„", example = "๊น€์€์ฑ„")
private String username;

@Schema(description = "์‚ฌ์šฉ์ž ๋ณ„๋ช…", example = "๋Œ€๊ตฌ์ด์žก์ด")
private String nickname;

@Schema(description = "์ „ํ™”๋ฒˆํ˜ธ", example = "01012345678")
private String phoneNumber;

@Schema(description = "์•ฝ๊ด€ ๋™์˜ ์—ฌ๋ถ€", example = "true")
private boolean agreeToTerms;

@NotBlank(message = "๋กœ๊ทธ์ธ ํƒ€์ž…์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
@Schema(description = "๋กœ๊ทธ์ธ ํƒ€์ž…", example = "GOOGLE")
private String loginType;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import team9502.sinchulgwinong.domain.auth.dto.request.UserSignupRequestDTO;
import team9502.sinchulgwinong.domain.oauth.dto.request.SocialLoginRequestDTO;
import team9502.sinchulgwinong.domain.oauth.enums.SocialType;
import team9502.sinchulgwinong.domain.point.enums.SpType;
import team9502.sinchulgwinong.domain.point.service.PointService;
Expand Down Expand Up @@ -40,14 +40,61 @@ public class SocialLoginService {
private String redirectUri;

@Transactional
public User createOrUpdateSocialLogin(UserSignupRequestDTO requestDTO, SocialType socialType) {
return userRepository.findByEmail(requestDTO.getEmail())
.orElseGet(() -> createUser(requestDTO, socialType));
public void createOrUpdateSocialLogin(SocialLoginRequestDTO requestDTO, SocialType socialType) {
String email = getEmailFromCode(requestDTO.getCode());
userRepository.findByEmail(email)
.orElseGet(() -> createUser(requestDTO, socialType, email));
}

@Transactional
public String handleGoogleCallback(String code) throws Exception {
String email = getEmailFromCode(code);

// ๊ตฌ๊ธ€ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ถ”๊ฐ€๋กœ ๊ฐ€์ ธ์™€ username๊ณผ nickname์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
String userInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo";
HttpHeaders userInfoHeaders = new HttpHeaders();
userInfoHeaders.setBearerAuth(getAccessToken(code));

HttpEntity<Void> userInfoRequest = new HttpEntity<>(userInfoHeaders);
ResponseEntity<String> userInfoResponse = restTemplate.exchange(userInfoUrl, HttpMethod.GET, userInfoRequest, String.class);

if (userInfoResponse.getStatusCode() == HttpStatus.OK) {
ObjectMapper mapper = new ObjectMapper();
JsonNode userInfo = mapper.readTree(userInfoResponse.getBody());
String username = userInfo.get("name").asText();
String nickname = userInfo.get("given_name").asText();

// ์ž„์‹œ๋กœ ์†Œ์…œ ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž ์ •๋ณด ์„ธ์…˜์— ์ €์žฅ
// ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ์ถ”๊ฐ€ ์ •๋ณด ์ž…๋ ฅ ํผ์œผ๋กœ ๋ฆฌ๋””๋ ‰์…˜ ํ›„ ์ด ์ •๋ณด๋ฅผ ์‚ฌ์šฉ
return "redirect:/social-login/additional-info?code=" + code + "&username=" + username + "&nickname=" + nickname;
} else {
throw new RuntimeException("์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ ์‹คํŒจ");
}
}

private String getEmailFromCode(String code) {
String accessToken = getAccessToken(code);
String userInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo";
HttpHeaders userInfoHeaders = new HttpHeaders();
userInfoHeaders.setBearerAuth(accessToken);

HttpEntity<Void> userInfoRequest = new HttpEntity<>(userInfoHeaders);
ResponseEntity<String> userInfoResponse = restTemplate.exchange(userInfoUrl, HttpMethod.GET, userInfoRequest, String.class);

if (userInfoResponse.getStatusCode() == HttpStatus.OK) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode userInfo = mapper.readTree(userInfoResponse.getBody());
return userInfo.get("email").asText();
} catch (Exception e) {
throw new RuntimeException("์ด๋ฉ”์ผ ์ถ”์ถœ ์‹คํŒจ", e);
}
} else {
throw new RuntimeException("์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ ์‹คํŒจ");
}
}

private String getAccessToken(String code) {
restTemplate.setMessageConverters(Arrays.asList(new FormHttpMessageConverter(), new StringHttpMessageConverter(StandardCharsets.UTF_8)));

String accessTokenUrl = "https://oauth2.googleapis.com/token";
Expand All @@ -65,45 +112,22 @@ public String handleGoogleCallback(String code) throws Exception {
ResponseEntity<String> response = restTemplate.postForEntity(accessTokenUrl, request, String.class);

if (response.getStatusCode() == HttpStatus.OK) {
String responseBody = response.getBody();
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(responseBody);
String accessToken = jsonNode.get("access_token").asText();

String userInfoUrl = "https://www.googleapis.com/oauth2/v2/userinfo";
HttpHeaders userInfoHeaders = new HttpHeaders();
userInfoHeaders.setBearerAuth(accessToken);

HttpEntity<Void> userInfoRequest = new HttpEntity<>(userInfoHeaders);
ResponseEntity<String> userInfoResponse = restTemplate.exchange(userInfoUrl, HttpMethod.GET, userInfoRequest, String.class);

if (userInfoResponse.getStatusCode() == HttpStatus.OK) {
// Google ์‚ฌ์šฉ์ž ์ •๋ณด ํŒŒ์‹ฑ
JsonNode userInfo = mapper.readTree(userInfoResponse.getBody());
String email = userInfo.get("email").asText();
String username = userInfo.get("name").asText();
String nickname = userInfo.get("given_name").asText();

// ์ž„์‹œ๋กœ ์†Œ์…œ ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž ์ •๋ณด ์„ธ์…˜์— ์ €์žฅ
// ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ์ถ”๊ฐ€ ์ •๋ณด ์ž…๋ ฅ ํผ์œผ๋กœ ๋ฆฌ๋””๋ ‰์…˜ ํ›„ ์ด ์ •๋ณด๋ฅผ ์‚ฌ์šฉ
return "redirect:/social-login/additional-info?email=" + email + "&username=" + username + "&nickname=" + nickname;
} else {
throw new RuntimeException("์‚ฌ์šฉ์ž ์ •๋ณด ์š”์ฒญ ์‹คํŒจ");
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response.getBody());
return jsonNode.get("access_token").asText();
} catch (Exception e) {
throw new RuntimeException("์•ก์„ธ์Šค ํ† ํฐ ์ถ”์ถœ ์‹คํŒจ", e);
}
} else {
// ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ๋กœ๊น…
System.out.println("Response Status: " + response.getStatusCode());
System.out.println("Response Body: " + response.getBody());

throw new RuntimeException("์•ก์„ธ์Šค ํ† ํฐ ์š”์ฒญ ์‹คํŒจ");
}
}

private User createUser(UserSignupRequestDTO requestDTO, SocialType socialType) {
private User createUser(SocialLoginRequestDTO requestDTO, SocialType socialType, String email) {
User newUser = User.builder()
.email(requestDTO.getEmail())
.email(email)
.nickname(requestDTO.getNickname())
.password(requestDTO.getPassword())
.username(requestDTO.getUsername())
.phoneNumber(requestDTO.getPhoneNumber())
.loginType(socialType)
Expand Down

0 comments on commit 2802ad1

Please sign in to comment.