Skip to content

Commit

Permalink
API 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
unanchoi committed Jan 16, 2024
1 parent a7c8c2c commit a8d9e92
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
3 changes: 2 additions & 1 deletion http/application/get-application-page1.http
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// get from http-client.private.env.json
GET {{host}}/api/application/1/page1
Authorization: Bearer {{new-token}}
Authorization: Bearer {{token}}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

public interface UserJpaRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
boolean existsByEmail(String email);

default User findByIdOrThrow(long id) {
return findById(id).orElseThrow(() -> new UnauthorizedException("인증되지 않은 유저입니다."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public OAuthResponse authenticateUser(String authCode) throws Exception {
.build();

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<GoogleLoginResponse> apiResponse = restTemplate.postForEntity(valueConfig.getGoogleAuthUrl() + "/token", googleOAuthRequest, GoogleLoginResponse.class);
ResponseEntity<GoogleLoginResponse> apiResponse = restTemplate.postForEntity(
valueConfig.getGoogleAuthUrl() + "/token", googleOAuthRequest, GoogleLoginResponse.class);

GoogleLoginResponse googleLoginResponse = apiResponse.getBody();

Expand All @@ -58,14 +59,22 @@ public OAuthResponse authenticateUser(String authCode) throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(resultJson);
val email = jsonNode.get("email").asText();
final boolean isExistUser = userRepository.existsByEmail(email);

val newUser = User.create(
email,
jsonNode.get("name").asText(),
"google",
jsonNode.get("picture").asText());
if (!isExistUser) {
val newUser = User.create(
email,
jsonNode.get("name").asText(),
"google",
jsonNode.get("picture").asText());
userRepository.save(newUser);
val authentication = new UsernamePasswordAuthenticationToken(newUser.getUserId(), null, null);
val tokenVO = generateToken(authentication);
newUser.updateRefreshToken(tokenVO.refreshToken());
return OAuthResponse.of(tokenVO.accessToken(), tokenVO.refreshToken());
}

User user = userRepository.findByEmail(email).orElseGet(() -> userRepository.save(newUser));
User user = userRepository.findByEmail(email).orElseThrow(() -> new Exception("존재하지 않는 유저입니다."));
val authentication = new UsernamePasswordAuthenticationToken(user.getUserId(), null, null);
val tokenVO = generateToken(authentication);
user.updateRefreshToken(tokenVO.refreshToken());
Expand Down

0 comments on commit a8d9e92

Please sign in to comment.