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
13 changes: 13 additions & 0 deletions api/src/main/kotlin/handler/UserHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ class UserHandler(
userService.attachSocial(user, socialLoginRequest, AuthProvider.KAKAO)
}

suspend fun attachApple(req: ServerRequest): ServerResponse =
handle(req) {
val user = req.getContext().user!!
val socialLoginRequest: SocialLoginRequest = req.awaitBody()
userService.attachSocial(user, socialLoginRequest, AuthProvider.APPLE)
}

suspend fun detachFacebook(req: ServerRequest): ServerResponse =
handle(req) {
val user = req.getContext().user!!
Expand All @@ -141,6 +148,12 @@ class UserHandler(
userService.detachSocial(user, AuthProvider.KAKAO)
}

suspend fun detachApple(req: ServerRequest): ServerResponse =
handle(req) {
val user = req.getContext().user!!
userService.detachSocial(user, AuthProvider.APPLE)
}

suspend fun checkAuthProviders(req: ServerRequest): ServerResponse =
handle(req) {
val user = req.getContext().user!!
Expand Down
2 changes: 2 additions & 0 deletions api/src/main/kotlin/router/MainRouter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,11 @@ class MainRouter(
POST("/facebook", userHandler::attachFacebook)
POST("/google", userHandler::attachGoogle)
POST("/kakao", userHandler::attachKakao)
POST("/apple", userHandler::attachApple)
DELETE("/facebook", userHandler::detachFacebook)
DELETE("/google", userHandler::detachGoogle)
DELETE("/kakao", userHandler::detachKakao)
DELETE("/apple", userHandler::detachApple)
}
"/users".nest {
GET("/me", userHandler::getUserMe)
Expand Down
39 changes: 39 additions & 0 deletions api/src/main/kotlin/router/docs/UserDocs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,30 @@ import org.springframework.web.bind.annotation.RequestMethod
],
),
),
RouterOperation(
path = "/v1/user/apple",
method = [RequestMethod.POST],
produces = [MediaType.APPLICATION_JSON_VALUE],
operation =
Operation(
operationId = "attachApple",
requestBody =
RequestBody(
content = [
Content(
schema = Schema(implementation = SocialLoginRequest::class),
mediaType = MediaType.APPLICATION_JSON_VALUE,
),
],
),
responses = [
ApiResponse(
responseCode = "200",
content = [Content(schema = Schema(implementation = TokenResponse::class))],
),
],
),
),
RouterOperation(
path = "/v1/user/facebook",
method = [RequestMethod.DELETE],
Expand Down Expand Up @@ -364,5 +388,20 @@ import org.springframework.web.bind.annotation.RequestMethod
],
),
),
RouterOperation(
path = "/v1/user/apple",
method = [RequestMethod.DELETE],
produces = [MediaType.APPLICATION_JSON_VALUE],
operation =
Operation(
operationId = "detachApple",
responses = [
ApiResponse(
responseCode = "200",
content = [Content(schema = Schema(implementation = TokenResponse::class))],
),
],
),
),
)
annotation class UserDocs
2 changes: 1 addition & 1 deletion core/src/main/kotlin/users/dto/SocialLoginRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.wafflestudio.snu4t.users.dto
import com.fasterxml.jackson.annotation.JsonAlias

data class SocialLoginRequest(
@JsonAlias("fb_token")
@JsonAlias("fb_token", "apple_token")
val token: String,
)
2 changes: 2 additions & 0 deletions core/src/main/kotlin/users/repository/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ interface UserRepository : CoroutineCrudRepository<User, String> {

suspend fun existsByCredentialKakaoSubAndActiveTrue(kakaoSub: String): Boolean

suspend fun existsByCredentialAppleSubAndActiveTrue(appleSub: String): Boolean

fun findAllByNicknameStartingWith(nickname: String): Flow<User>

suspend fun findByCredentialAppleTransferSubAndActiveTrue(appleTransferSub: String): User?
Expand Down
13 changes: 12 additions & 1 deletion core/src/main/kotlin/users/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,18 @@ class UserServiceImpl(
kakaoEmail = kakaoCredential.kakaoEmail
}
}
AuthProvider.APPLE -> throw IllegalStateException("Apple login is not supported")
AuthProvider.APPLE -> {
if (user.credential.appleSub != null) throw AlreadySocialAccountException
if (userRepository.existsByCredentialAppleSubAndActiveTrue(oauth2UserResponse.socialId)) {
throw DuplicateSocialAccountException
}
val appleCredential = authService.buildAppleCredential(oauth2UserResponse)
user.credential.apply {
appleSub = appleCredential.appleSub
appleEmail = appleCredential.appleEmail
appleTransferSub = appleCredential.appleTransferSub
}
}
AuthProvider.LOCAL -> throw IllegalStateException("Cannot attach local account")
}

Expand Down
Loading