Skip to content

Commit

Permalink
✅ 카카오 이메일, 닉네임 유저 조회 구현 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdals4716 committed Jul 29, 2024
1 parent 390d5b4 commit 4060526
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public ResponseEntity<UserDTO> getUserByUid(@PathVariable String uid) {
return ResponseEntity.ok(user);
}

//닉네임으로 유저 조회
@GetMapping("/nickname/{nickname}")
public ResponseEntity<UserDTO> getUserByNickname(@PathVariable String nickname) {
UserDTO user = userService.getUserByNickname(nickname);
return ResponseEntity.ok(user);
}

//아이디 중복 확인
@GetMapping("/check-uid")
public ResponseEntity<Boolean> isUidDuplicate(@RequestBody String uid) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/example/MangoWafflee/DTO/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class UserDTO {
private String name;
private String nickname;
private String image;
private String email;

public static UserDTO entityToDto(UserEntity userEntity) {
return new UserDTO(
Expand All @@ -23,12 +24,13 @@ public static UserDTO entityToDto(UserEntity userEntity) {
userEntity.getPassword(),
userEntity.getName(),
userEntity.getNickname(),
userEntity.getImage()
userEntity.getImage(),
userEntity.getEmail()
);
}

public UserEntity dtoToEntity() {
return new UserEntity(id, uid, password, name, nickname, image);
return new UserEntity(id, uid, password, name, nickname, image, email);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class UserEntity implements UserDetails {
private String password;
private String image;
private String name;
private String email;

@Column(unique = true)
private String nickname;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) {
UserEntity userEntity;
if (userEntityOptional.isPresent()) {
userEntity = userEntityOptional.get();
// 이름 업데이트
// 이름 및 이메일 업데이트
userEntity.setName(name);
userEntity.setEmail(email);
} else {
// 존재하지 않으면 새로 생성
userEntity = UserEntity.builder()
.uid(String.valueOf(id))
.name(name)
.email(email)
.password(passwordEncoder.encode("OAuth2_User_Password")) // 비밀번호 설정
.build();
userRepository.save(userEntity);
Expand All @@ -60,3 +62,4 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) {
return new CustomOAuth2User(userEntity, oAuth2User.getAttributes());
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public interface UserService {
UserDTO createUser(UserDTO userDTO);
UserDTO getUserByUid(String uid);
UserDTO getUserByNickname(String nickname);
boolean isUidDuplicate(String uid);
boolean isNicknameDuplicate(String nickname);
JWTDTO login(String uid, String password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public UserDTO getUserByUid(String uid) {
return UserDTO.entityToDto(userEntity);
}

//닉네임으로 유저 조회
@Override
public UserDTO getUserByNickname(String nickname) {
UserEntity userEntity = userRepository.findByNickname(nickname);
if (userEntity == null) {
throw new RuntimeException("유저의 nickname이 " + nickname + "인 사용자를 찾을 수 없습니다");
}
return UserDTO.entityToDto(userEntity);
}

//아이디 중복 확인
@Override
public boolean isUidDuplicate(String uid) {
Expand Down Expand Up @@ -169,19 +179,23 @@ public UserDTO updateNickname(String uid, String nickname) {
public JWTDTO loginWithOAuth2(OAuth2User oAuth2User) {
String uid = oAuth2User.getAttribute("id").toString();
Map<String, Object> properties = oAuth2User.getAttribute("properties");
Map<String, Object> kakaoAccount = oAuth2User.getAttribute("kakao_account");
String name = properties != null ? (String) properties.get("nickname") : null;
String email = kakaoAccount != null ? (String) kakaoAccount.get("email") : null;

UserEntity userEntity = userRepository.findByUid(uid).orElse(null);

if (userEntity == null) {
userEntity = UserEntity.builder()
.uid(uid)
.name(name)
.email(email)
.password(passwordEncoder.encode("oauth2user"))
.build();
userRepository.save(userEntity);
} else {
userEntity.setName(name);
userEntity.setEmail(email);
userRepository.save(userEntity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="container" style="display: flex; justify-content: center; align-content: center; align-items: center; flex-direction: column; margin: 200px auto;">
<h1>카카오 로그인</h1>
<a href="/oauth2/authorization/kakao">
<img src="/static/kakao_login_large_narrow.png" alt="카카오 로그인">
<img src="/kakao_login_large_narrow.png" alt="카카오 로그인">
</a>
</div>
</body>
Expand Down

0 comments on commit 4060526

Please sign in to comment.