-
Notifications
You must be signed in to change notification settings - Fork 0
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
[feat] 소셜 로그인 API 구현 #15
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8cd298c
feat: 로그인 api controller 구현 (#14)
gardening-y 007054e
feat: 로그인 service 구현 (#14)
gardening-y 17ad9d8
feat: user repository 구현 (#14)
gardening-y 2274d28
feat: 로그인 user dto 구현 (#14)
gardening-y e055027
chore: redis 설정 (#14)
gardening-y b7e554a
feat: refresh token redis 구현 (#14)
gardening-y 96b80f1
feat: refresh token repository 구현 (#14)
gardening-y a11cecb
feat: enum platform과 string platform 매핑 기능 (#14)
gardening-y d4a5d9a
feat: user refresh token update 메서드 추가 (#14)
gardening-y 8bd323e
feat: 로그인 오류 메시지 추가 (#14)
gardening-y 0d56621
fix: 어노테이션 오류 수정 (#14)
gardening-y File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
doorip-api/src/main/java/org/doorip/user/api/UserApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.doorip.user.api; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.doorip.common.ApiResponse; | ||
import org.doorip.common.ApiResponseUtil; | ||
import org.doorip.message.SuccessMessage; | ||
import org.doorip.user.dto.request.UserSignInRequest; | ||
import org.doorip.user.dto.response.UserResponse; | ||
import org.doorip.user.service.UserService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api/users") | ||
@Controller | ||
public class UserApiController { | ||
private final UserService userService; | ||
|
||
@PostMapping("/signin") | ||
public ResponseEntity<ApiResponse<?>> signIn(@RequestHeader("Authorization") final String token, | ||
@RequestParam final UserSignInRequest request) { | ||
final UserResponse response = userService.signIn(token, request); | ||
return ApiResponseUtil.success(SuccessMessage.OK, response); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
doorip-api/src/main/java/org/doorip/user/dto/request/UserSignInRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package org.doorip.user.dto.request; | ||
|
||
public record UserSignInRequest( | ||
String platform | ||
) { | ||
} |
16 changes: 16 additions & 0 deletions
16
doorip-api/src/main/java/org/doorip/user/dto/response/UserResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.doorip.user.dto.response; | ||
|
||
import org.doorip.auth.jwt.Token; | ||
|
||
public record UserResponse( | ||
String accessToken, | ||
String refreshToken | ||
) { | ||
|
||
public static UserResponse of(Token token) { | ||
return new UserResponse( | ||
token.accessToken(), | ||
token.refreshToken() | ||
); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
doorip-api/src/main/java/org/doorip/user/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.doorip.user.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.doorip.auth.jwt.JwtProvider; | ||
import org.doorip.auth.jwt.Token; | ||
import org.doorip.exception.EntityNotFoundException; | ||
import org.doorip.message.ErrorMessage; | ||
import org.doorip.openfeign.apple.AppleOAuthProvider; | ||
import org.doorip.openfeign.kakao.KakaoOAuthProvider; | ||
import org.doorip.user.domain.Platform; | ||
import org.doorip.user.domain.RefreshToken; | ||
import org.doorip.user.domain.User; | ||
import org.doorip.user.dto.request.UserSignInRequest; | ||
import org.doorip.user.dto.response.UserResponse; | ||
import org.doorip.user.repository.RefreshTokenRepository; | ||
import org.doorip.user.repository.UserRepository; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static org.doorip.user.domain.Platform.APPLE; | ||
import static org.doorip.user.domain.Platform.getEnumPlatformFromStringPlatform; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Transactional(readOnly = true) | ||
public class UserService { | ||
private final UserRepository userRepository; | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
private final JwtProvider jwtProvider; | ||
private final AppleOAuthProvider appleOAuthProvider; | ||
private final KakaoOAuthProvider kakaoOAuthProvider; | ||
|
||
@Transactional | ||
public UserResponse signIn(String token, UserSignInRequest request) { | ||
Platform enumPlatform = getEnumPlatformFromStringPlatform(request.platform()); | ||
String platformId = getPlatformId(token, enumPlatform); | ||
User findUser = getUser(enumPlatform, platformId); | ||
Token issueToken = jwtProvider.issueToken(findUser.getId()); | ||
updateRefreshToken(issueToken.refreshToken(), findUser); | ||
|
||
return UserResponse.of(issueToken); | ||
} | ||
|
||
private String getPlatformId(String token, Platform platform) { | ||
if (platform == APPLE) { | ||
return appleOAuthProvider.getApplePlatformId(token); | ||
} | ||
return kakaoOAuthProvider.getKakaoPlatformId(token); | ||
} | ||
|
||
private User getUser(Platform platform, String platformId) { | ||
return userRepository.findUserByPlatformAndPlatformId(platform, platformId) | ||
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.USER_NOT_FOUND)); | ||
} | ||
|
||
private void updateRefreshToken(String refreshToken, User user) { | ||
user.updateRefreshToken(refreshToken); | ||
refreshTokenRepository.save(RefreshToken.createRefreshToken(user.getId(), refreshToken)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 20 additions & 2 deletions
22
doorip-domain/src/main/java/org/doorip/user/domain/Platform.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
package org.doorip.user.domain; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
import org.doorip.exception.InvalidValueException; | ||
import org.doorip.message.ErrorMessage; | ||
|
||
import java.util.Arrays; | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) | ||
public enum Platform { | ||
KAKAO, APPLE | ||
} | ||
APPLE("apple"), | ||
KAKAO("kakao"); | ||
|
||
private final String stringPlatform; | ||
|
||
public static Platform getEnumPlatformFromStringPlatform(String stringPlatform) { | ||
return Arrays.stream(values()) | ||
.filter(platform -> platform.stringPlatform.equals(stringPlatform)) | ||
.findFirst() | ||
.orElseThrow(() -> new InvalidValueException(ErrorMessage.INVALID_PLATFORM_TYPE)); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
doorip-domain/src/main/java/org/doorip/user/domain/RefreshToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.doorip.user.domain; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Builder(access = AccessLevel.PRIVATE) | ||
@Getter | ||
@RedisHash(value = "refreshToken", timeToLive = 604800000) | ||
public class RefreshToken { | ||
@Id | ||
private Long id; | ||
private String refreshToken; | ||
|
||
public static RefreshToken createRefreshToken(Long userId, String refreshToken) { | ||
return RefreshToken.builder() | ||
.id(userId) | ||
.refreshToken(refreshToken) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
doorip-domain/src/main/java/org/doorip/user/repository/RefreshTokenRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.doorip.user.repository; | ||
|
||
import org.doorip.user.domain.RefreshToken; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, Long> { | ||
} |
11 changes: 11 additions & 0 deletions
11
doorip-domain/src/main/java/org/doorip/user/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.doorip.user.repository; | ||
|
||
import org.doorip.user.domain.Platform; | ||
import org.doorip.user.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
Optional<User> findUserByPlatformAndPlatformId(Platform platform, String platformId); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
클래스 레벨에 Transactional 어노테이션이 read only 옵션으로 적용되어 있어서 해당 클래스의 모든 public 메서드에서 변경 감지 동작이 이루어지지 않게 됩니다. 따라서 User 엔티티의 refresh token을 update 하는 과정이 정상 수행되지 않을 것 같습니다. 엔티티 데이터의 변경이 필요한 메서드 위에 read only 옵션이 적용되지 않은 Transactional 어노테이션을 추가적으로 적용하여 해결하면 좋을 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
헉.. 오류 발견 감사합니다 :)