Skip to content

Commit

Permalink
구글 로그인 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
HeeSung98 committed Dec 5, 2023
1 parent 3046538 commit d474cc7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ResponseEntity<UserDTO> google(@RequestBody GoogleUserDTO googleUserDTO)
log.info(" ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ googleToken controller ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ");
log.info("googleUserDTO: " + googleUserDTO);

UserDTO result = userService.profile(googleUserDTO.getEmail());
UserDTO result = userService.googleUserCheck(googleUserDTO);

return new ResponseEntity<>(result, HttpStatus.OK);
}
Expand Down
3 changes: 3 additions & 0 deletions user/src/main/java/com/weather/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.weather.user.service;


import com.weather.user.dto.GoogleUserDTO;
import com.weather.user.dto.UserDTO;
import com.weather.user.entity.User;

Expand All @@ -19,6 +20,8 @@ public interface UserService {

void remove(String email);

UserDTO googleUserCheck(GoogleUserDTO googleUserDTO);

default User dtoToEntity(UserDTO userDTO) {
User user = User.builder()
.email(userDTO.getEmail())
Expand Down
31 changes: 31 additions & 0 deletions user/src/main/java/com/weather/user/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.weather.user.service;

import com.weather.user.dto.GoogleUserDTO;
import com.weather.user.dto.UserDTO;
import com.weather.user.entity.User;
import com.weather.user.entity.UserRole;
import com.weather.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

import java.util.Optional;
Expand Down Expand Up @@ -127,4 +130,32 @@ public void remove(String email) {
}

}

@Override
public UserDTO googleUserCheck(GoogleUserDTO googleUserDTO) {
log.info("ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ");
String email = googleUserDTO.getEmail();
String image = googleUserDTO.getPicture();
Optional<User> optionalUser = userRepository.findByEmail(email, true);

if(optionalUser.isPresent()) {
User socialUser = optionalUser.get();
UserDTO result = entityToDTO(socialUser);
return result;
} else {
User socialUser = User.builder()
.email(email)
.password(passwordEncoder.encode("1234"))
.image(image)
.fromSocial(true)
.status(true)
.build();
socialUser.addRole(UserRole.USER);

userRepository.save(socialUser);

UserDTO result = entityToDTO(socialUser);
return result;
}
}
}

0 comments on commit d474cc7

Please sign in to comment.