Skip to content

github, kakao 쇼설 로그인 수정 #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ public String googleCallback(@AuthenticationPrincipal OAuth2User principal, Mode

@GetMapping("/login/oauth2/code/github")
public String githubCallback(@AuthenticationPrincipal OAuth2User principal, Model model) {
model.addAttribute("name", principal.getAttribute("name"));
model.addAttribute("name", principal.getAttribute("login"));
return "home";
}

// @GetMapping("/login/oauth2/code/kakao")
// public String kakaoCallback(@AuthenticationPrincipal OAuth2User principal, Model model) {
// model.addAttribute("name", principal.getAttribute("nickname"));
// return "home";
// }
@GetMapping("/login/oauth2/code/kakao")
public String kakaoCallback(@AuthenticationPrincipal OAuth2User principal, Model model) {
model.addAttribute("name", principal.getAttribute("nickname"));
return "home";
}
}

7 changes: 3 additions & 4 deletions src/main/java/codeview/main/auth/dto/OAuth2UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ private static OAuth2UserInfo ofGoogle(Map<String, Object> attributes) {
}

private static OAuth2UserInfo ofKakao(Map<String, Object> attributes) {
Map<String, Object> account = (Map<String, Object>) attributes.get("kakao_account");
Map<String, Object> profile = (Map<String, Object>) account.get("profile");
Map<String, Object> kakaoAccount = (Map<String, Object>) attributes.get("kakao_account");
Map<String, Object> profile = (Map<String, Object>) kakaoAccount.get("profile");

return OAuth2UserInfo.builder()
.name((String) profile.get("nickname"))
.email((String) account.get("email"))
.profile((String) profile.get("profile_image_url"))
.build();
}
Expand All @@ -57,7 +56,7 @@ private static OAuth2UserInfo ofGithub(Map<String, Object> attributes) {
public Member toEntity() {
return Member.builder()
.name(name)
.email(email)
.email(email != null ? email : name + "@kakao.com")
.profile(profile)
.memberKey(KeyGenerator.generateKey())
.role(Member.Role.ROLE_USER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

OAuth2UserInfo oAuth2UserInfo = OAuth2UserInfo.of(registrationId, oAuth2UserAttributes);

// 이메일이 없는 경우 처리
Optional<Member> memberOptional = Optional.empty();
if (oAuth2UserInfo.getEmail() != null) {
memberOptional = memberRepository.findByEmail(oAuth2UserInfo.getEmail());
}

Optional<Member> memberOptional = memberRepository.findByEmail(oAuth2UserInfo.getEmail());
Member member;
if (memberOptional.isPresent()) {
member = memberOptional.get();
} else {
// 이메일이 없거나 기존 회원이 아닌 경우 새로 저장
member = getOrSave(oAuth2UserInfo);
}

Expand All @@ -55,15 +49,9 @@ private Member getOrSave(OAuth2UserInfo oAuth2UserInfo) {
member.setName(oAuth2UserInfo.getName());
member.setProfile(oAuth2UserInfo.getProfile());

if (oAuth2UserInfo.getEmail() != null) {
member.setEmail(oAuth2UserInfo.getEmail());
} else {
// 이메일이 없는 경우 다른 방법으로 식별자를 생성 (예: OAuth 제공자의 고유 ID 사용)
member.setEmail(oAuth2UserInfo.getName() + "@example.com");
}
member.setEmail(oAuth2UserInfo.getName() + "@kakao.com");

member.setRole(Member.Role.ROLE_USER);

return memberRepository.save(member);
}
}
Loading