Skip to content
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 @@ -25,7 +25,7 @@ public class SocialAuthService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;

public User join(String providerId, String nickname, String password, ProviderType providerType) {
public User join(String providerId, String nickname, String password, String email, ProviderType providerType) {
if (userRepository.existsByNickname(nickname)) {
throw new ErrorException(AuthErrorCode.ALREADY_EXIST_NICKNAME);
}
Expand All @@ -34,7 +34,7 @@ public User join(String providerId, String nickname, String password, ProviderTy
String encodedPassword = passwordEncoder.encode(raw);

User user = User.builder()
.email(null)
.email(email)
.nickname(nickname)
.fullName(nickname)
.password(encodedPassword)
Expand All @@ -47,11 +47,12 @@ public User join(String providerId, String nickname, String password, ProviderTy
return userRepository.save(user);
}

public User modifyOrJoin(String providerId, String nickname, String password, ProviderType providerType) {
public User modifyOrJoin(String providerId, String nickname, String password, String email,
ProviderType providerType) {
User user = userRepository.findByProviderId(providerId).orElse(null);

if (user == null) {
return join(providerId, nickname, password, providerType);
return join(providerId, nickname, password, email, providerType);
}

user.update(nickname, nickname, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class User extends BaseEntity {
)
private Long id;

@Column(length = 100)
@Column(nullable = false, length = 100, unique = true)
private String email;

@Column(nullable = false, name = "full_name", length = 30)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,20 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

log.debug("Kakao attributes={}", attributes);

// 1) properties.nickname (구버전/일반)
String nickname = null;
Object propsObj = attributes.get("properties");
if (propsObj instanceof Map<?, ?> props) {
Object nn = props.get("nickname");
if (nn != null)
nickname = nn.toString();
}

// 2) kakao_account.profile.nickname (신규/권장 구조)
if (nickname == null) {
Object accObj = attributes.get("kakao_account");
if (accObj instanceof Map<?, ?> acc) {
Object profileObj = acc.get("profile");
if (profileObj instanceof Map<?, ?> profile) {
Object nicknameObj = profile.get("nickname");
if (nicknameObj != null)
nickname = nicknameObj.toString();
}
String nickname = "";
String email = "";

Object accObj = attributes.get("kakao_account");
if (accObj instanceof Map<?, ?> acc) {
Object profileObj = acc.get("profile");
if (profileObj instanceof Map<?, ?> profile) {
Object nicknameObj = profile.get("nickname");
if (nicknameObj != null)
nickname = nicknameObj.toString();
}

Object emailObj = acc.get("email");
email = emailObj.toString();
}

if (nickname == null || nickname.isBlank()) {
Expand All @@ -66,7 +60,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic

String kakaoId = oAuth2User.getName();

User user = socialAuthService.modifyOrJoin(kakaoId, nickname, "", ProviderType.KAKAO);
User user = socialAuthService.modifyOrJoin(kakaoId, nickname, "", email, ProviderType.KAKAO);

Collection<GrantedAuthority> authorities =
List.of(new SimpleGrantedAuthority("ROLE_NORMAL"));
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ spring:
client-id: ${KAKAO_CLIENT_ID}
client-secret: ${KAKAO_CLIENT_SECRET}
client-authentication-method: client_secret_post
scope: profile_nickname, profile_image
scope: profile_nickname, profile_image, account_email
client-name: Kakao
authorization-grant-type: authorization_code
redirect-uri: '{baseUrl}/login/oauth2/code/kakao'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE UNIQUE INDEX IF NOT EXISTS ux_users_email
ON users (email);

ALTER TABLE users
ALTER COLUMN email SET NOT NULL;