Skip to content
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

[LINKER-X] 비회원 로그인시 일단 무조건 회원가입 #30

Merged
merged 1 commit into from
Jan 27, 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 @@ -5,14 +5,13 @@
import com.imlinker.coreapi.core.auth.security.oauth2.vendor.OAuthVendorAttributeResolver;
import com.imlinker.domain.auth.OAuthVendor;
import com.imlinker.domain.common.Email;
import com.imlinker.domain.common.URL;
import com.imlinker.domain.user.UserService;
import com.imlinker.error.ApplicationException;
import com.imlinker.error.ErrorType;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -24,7 +23,6 @@
@Slf4j
@Component
public class CustomAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {

private final UserService userService;

private final JwtTokenProvider jwtTokenProvider;
Expand Down Expand Up @@ -58,35 +56,25 @@ public void onAuthenticationSuccess(
OAuthVendorAttributeResolver resolver =
vendorAttributeResolverRegistry.get(oAuth2User.getVendor());

String oAuthId = resolver.getOAuthId(oAuth2User.getAttributes());
Email email = resolver.getEmail(oAuth2User.getAttributes());
boolean isMember = userService.isMember(email);

if (isMember) {
String accessToken = jwtTokenProvider.generateToken(email, TokenType.ACCESS_TOKEN);
String refreshToken = jwtTokenProvider.generateToken(email, TokenType.REFRESH_TOKEN);
boolean isMember = userService.isMember(oAuth2User.getVendor(), oAuthId);

String redirectUri =
clientOriginHost
+ "?"
+ String.format(
"%s?accessToken=%s&refreshToken=%s", clientOriginHost, accessToken, refreshToken);
getRedirectStrategy().sendRedirect(request, response, redirectUri);
} else {
String oAuthId = resolver.getOAuthId(oAuth2User.getAttributes());
if (!isMember) {
String nickname = resolver.getNickname(oAuth2User.getAttributes());
String profileImgUrl = resolver.getProfileImgUrl(oAuth2User.getAttributes());

String redirectUri =
clientOriginHost
+ "?"
+ URLEncoder.encode(
String.format(
"oAuthId=%s&nickname=%s&profileImgUrl=%s&email=%s",
oAuthId, nickname, profileImgUrl, email.getValue()),
StandardCharsets.UTF_8);
URL profileImgUrl = URL.of(resolver.getProfileImgUrl(oAuth2User.getAttributes()));

getRedirectStrategy().sendRedirect(request, response, redirectUri);
userService.createUser(oAuthId, nickname, email, profileImgUrl, oAuth2User.getVendor());
}

String accessToken = jwtTokenProvider.generateToken(email, TokenType.ACCESS_TOKEN);
String refreshToken = jwtTokenProvider.generateToken(email, TokenType.REFRESH_TOKEN);

String redirectUri =
String.format(
"%s?accessToken=%s&refreshToken=%s", clientOriginHost, accessToken, refreshToken);
getRedirectStrategy().sendRedirect(request, response, redirectUri);
Comment on lines +74 to +77
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최종적으로 비회원도 회원가입 진행후 동일하게 accessToken과 refreshToken을 내립니다

}

private String getHostFromCache(String state) {
Expand Down
6 changes: 2 additions & 4 deletions domain/src/main/java/com/imlinker/domain/common/Email.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import com.imlinker.error.ApplicationException;
import com.imlinker.error.ErrorType;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Getter
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Email {
Expand Down
8 changes: 3 additions & 5 deletions domain/src/main/java/com/imlinker/domain/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
import com.imlinker.error.ApplicationException;
import com.imlinker.error.ErrorType;
import java.util.regex.Pattern;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Getter
@ToString
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class URL {

private static final String URL_REGEX =
"^((http|https)://)?(www\\.)?([a-zA-Z0-9가-힣]+)\\.[a-z]{2,6}(:\\d{1,5})?(/[a-zA-Z0-9가-힣%._~/?#-]*)?$";
"^((http|https)://)?(www\\.)?([a-zA-Z0-9가-힣.]+)\\.[a-z]{2,6}(:\\d{1,5})?(/[a-zA-Z0-9가-힣%._~/?#-]*)?$";

private static final Pattern URL_PATTERN = Pattern.compile(URL_REGEX);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.imlinker.domain.user;

import com.imlinker.domain.common.Email;
import com.imlinker.domain.auth.OAuthVendor;
import java.util.Optional;

public interface UserRepository {

Optional<User> findById(Long id);

Optional<User> findByEmail(Email email);
Optional<User> findByOAuthVendorAndOAuthIdentifier(
OAuthVendor oAuthVendor, String oAuthIdentifier);
Comment on lines +10 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이메일은 변경 가능한 값이기 떄문에, kakao에서 내려주는 id(oAuthIdentifier)와
oAuthVendor(kakao, ...)를 통해서 회원 여부를 판단했습니다


User save(User user);
}
29 changes: 27 additions & 2 deletions domain/src/main/java/com/imlinker/domain/user/UserService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
package com.imlinker.domain.user;

import com.imlinker.domain.auth.OAuthVendor;
import com.imlinker.domain.common.Email;
import com.imlinker.domain.common.URL;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;

public boolean isMember(Email email) {
return userRepository.findByEmail(email).isPresent();
public boolean isMember(OAuthVendor oAuthVendor, String oAuthIdentifier) {
return userRepository
.findByOAuthVendorAndOAuthIdentifier(oAuthVendor, oAuthIdentifier)
.isPresent();
}

public User createUser(
String oAuthId, String name, Email email, URL profileImgUrl, OAuthVendor oAuthVendor) {
log.info(
"[회원가입][시작] oAuthId: {}, name: {}, email: {}, profileImgUrl: {}, oAuthVendor: {}",
oAuthId,
name,
email,
profileImgUrl,
oAuthVendor);
return userRepository.save(
User.builder()
.oAuthIdentifier(oAuthId)
.name(name)
.email(email)
.profileImgUrl(profileImgUrl)
.oAuthVendor(oAuthVendor)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.imlinker.storage.user;

import com.imlinker.domain.common.Email;
import com.imlinker.domain.auth.OAuthVendor;
import com.imlinker.domain.user.User;
import com.imlinker.domain.user.UserRepository;
import com.imlinker.storage.user.mapper.UserMapper;
Expand All @@ -21,9 +21,10 @@ public Optional<User> findById(Long id) {
}

@Override
public Optional<User> findByEmail(Email email) {
Optional<UserEntity> entity = repo.findByEmail(email.getValue());
return entity.map(UserMapper::toModel);
public Optional<User> findByOAuthVendorAndOAuthIdentifier(
OAuthVendor oAuthVendor, String oAuthIdentifier) {
return repo.findByOauthVendorAndOauthIdentifier(oAuthVendor.name(), oAuthIdentifier)
.map(UserMapper::toModel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class UserEntity extends BaseTimeEntity {
private Long id;

@Column(name = "oauth_vendor")
private String oAuthVendor;
private String oauthVendor;

@Column(name = "oauth_identifier")
private String oAuthIdentifier;
private String oauthIdentifier;

@Column(name = "name")
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserJpaRepository extends JpaRepository<UserEntity, Long> {
Optional<UserEntity> findByEmail(String email);
Optional<UserEntity> findByOauthVendorAndOauthIdentifier(
String oAuthVendor, String oAuthIdentifier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class UserMapper {
public static User toModel(UserEntity entity) {
return User.builder()
.id(entity.getId())
.oAuthVendor(OAuthVendor.of(entity.getOAuthVendor()))
.oAuthIdentifier(entity.getOAuthIdentifier())
.oAuthVendor(OAuthVendor.of(entity.getOauthVendor()))
.oAuthIdentifier(entity.getOauthIdentifier())
.name(entity.getName())
.profileImgUrl(URL.of(entity.getProfileImgUrl()))
.email(Email.of(entity.getEmail()))
Expand All @@ -25,8 +25,8 @@ public static User toModel(UserEntity entity) {
public static UserEntity toEntity(User model) {
return UserEntity.builder()
.id(model.getId())
.oAuthVendor(model.getOAuthVendor().name())
.oAuthIdentifier(model.getOAuthIdentifier())
.oauthVendor(model.getOAuthVendor().name())
.oauthIdentifier(model.getOAuthIdentifier())
.name(model.getName())
.profileImgUrl(model.getProfileImgUrl().getValue())
.email(model.getEmail().getValue())
Expand Down
Loading