diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/application/OAuthFacade.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/application/OAuthFacade.kt index c36ce36..c2dc7d4 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/auth/application/OAuthFacade.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/application/OAuthFacade.kt @@ -1,11 +1,17 @@ package com.hero.alignlab.domain.auth.application +import com.hero.alignlab.common.extension.executes +import com.hero.alignlab.config.database.TransactionTemplates import com.hero.alignlab.domain.auth.model.OAuthProvider -import com.hero.alignlab.domain.auth.model.request.OAuthLoginRequest +import com.hero.alignlab.domain.auth.model.request.OAuthSignInRequest +import com.hero.alignlab.domain.auth.model.request.OAuthSignUpRequest import com.hero.alignlab.domain.auth.model.response.OAuthCheckSignUpResponse -import com.hero.alignlab.domain.auth.model.response.OAuthLoginResponse +import com.hero.alignlab.domain.auth.model.response.OAuthSignInResponse +import com.hero.alignlab.domain.auth.model.response.OAuthSignUpResponse import com.hero.alignlab.domain.user.application.OAuthUserInfoService import com.hero.alignlab.domain.user.application.UserInfoService +import com.hero.alignlab.domain.user.domain.OAuthUserInfo +import com.hero.alignlab.domain.user.domain.UserInfo import io.github.oshai.kotlinlogging.KotlinLogging import org.springframework.stereotype.Service import java.time.LocalDateTime @@ -16,6 +22,7 @@ class OAuthFacade( private val oAuthUserInfoService: OAuthUserInfoService, private val userInfoService: UserInfoService, private val jwtTokenService: JwtTokenService, + private val txTemplates: TransactionTemplates, ) { companion object { private val TOKEN_EXPIRED_DATE = LocalDateTime.of(2024, 12, 29, 0, 0, 0) @@ -31,14 +38,47 @@ class OAuthFacade( return OAuthCheckSignUpResponse(isExists) } - suspend fun signIn(provider: OAuthProvider, request: OAuthLoginRequest): OAuthLoginResponse? { + suspend fun signIn(provider: OAuthProvider, request: OAuthSignInRequest): OAuthSignInResponse? { val oauthInfo = oAuthService.getOAuthInfo(provider, request.accessToken) val userInfo = userInfoService.findByOAuthOrThrow(provider.toProvider(), oauthInfo.oauthId) val token = jwtTokenService.createToken(userInfo.id, TOKEN_EXPIRED_DATE) - return OAuthLoginResponse( + return OAuthSignInResponse( + uid = userInfo.id, + nickname = userInfo.nickname, + accessToken = token + ) + } + + suspend fun signUp(provider: OAuthProvider, request: OAuthSignUpRequest): OAuthSignUpResponse { + val oauthInfo = oAuthService.getOAuthInfo(provider, request.accessToken) + + val isExists = oAuthUserInfoService.existsByOauthIdAndProvider(oauthInfo.oauthId, provider.toProvider()) + + val userInfo = when (isExists) { + true -> userInfoService.findByOAuthOrThrow(provider.toProvider(), oauthInfo.oauthId) + false -> { + txTemplates.writer.executes { + val createdUser = userInfoService.saveSync(UserInfo(nickname = oauthInfo.nickname)) + + oAuthUserInfoService.saveSync( + OAuthUserInfo( + uid = createdUser.id, + provider = provider.toProvider(), + oauthId = oauthInfo.oauthId + ) + ) + + createdUser + } + } + } + + val token = jwtTokenService.createToken(userInfo.id, TOKEN_EXPIRED_DATE) + + return OAuthSignUpResponse( uid = userInfo.id, nickname = userInfo.nickname, accessToken = token diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthLoginRequest.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthSignInRequest.kt similarity index 58% rename from src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthLoginRequest.kt rename to src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthSignInRequest.kt index 7bbe95a..b31f4ea 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthLoginRequest.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthSignInRequest.kt @@ -1,5 +1,6 @@ package com.hero.alignlab.domain.auth.model.request -data class OAuthLoginRequest( +data class OAuthSignInRequest( + /** oauth accessToken */ val accessToken: String ) diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthSignUpRequest.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthSignUpRequest.kt new file mode 100644 index 0000000..d57be96 --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/model/request/OAuthSignUpRequest.kt @@ -0,0 +1,6 @@ +package com.hero.alignlab.domain.auth.model.request + +data class OAuthSignUpRequest( + /** oauth accessToken */ + val accessToken: String, +) diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthLoginResponse.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthSignInResponse.kt similarity index 83% rename from src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthLoginResponse.kt rename to src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthSignInResponse.kt index 8910fa5..af888a5 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthLoginResponse.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthSignInResponse.kt @@ -1,6 +1,6 @@ package com.hero.alignlab.domain.auth.model.response -data class OAuthLoginResponse( +data class OAuthSignInResponse( val uid: Long, val nickname: String, /** hero access token */ diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthSignUpResponse.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthSignUpResponse.kt new file mode 100644 index 0000000..24e8fa5 --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/model/response/OAuthSignUpResponse.kt @@ -0,0 +1,8 @@ +package com.hero.alignlab.domain.auth.model.response + +data class OAuthSignUpResponse( + val uid: Long, + val nickname: String, + /** hero access token */ + val accessToken: String, +) diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/resource/OAuthResource.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/resource/OAuthResource.kt index 9bd3958..a22c5c0 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/auth/resource/OAuthResource.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/resource/OAuthResource.kt @@ -1,9 +1,11 @@ package com.hero.alignlab.domain.auth.resource +import com.hero.alignlab.common.extension.wrapCreated import com.hero.alignlab.common.extension.wrapOk import com.hero.alignlab.domain.auth.application.OAuthFacade import com.hero.alignlab.domain.auth.model.OAuthProvider -import com.hero.alignlab.domain.auth.model.request.OAuthLoginRequest +import com.hero.alignlab.domain.auth.model.request.OAuthSignInRequest +import com.hero.alignlab.domain.auth.model.request.OAuthSignUpRequest import io.swagger.v3.oas.annotations.Operation import io.swagger.v3.oas.annotations.tags.Tag import org.springframework.http.MediaType @@ -29,6 +31,13 @@ class OAuthResource( @PostMapping("/api/v1/oauth/{provider}/sign-in") suspend fun signIn( @PathVariable provider: OAuthProvider, - @RequestBody request: OAuthLoginRequest, + @RequestBody request: OAuthSignInRequest, ) = oAuthFacade.signIn(provider, request).wrapOk() + + @Operation(summary = "회원가입") + @PostMapping("/api/v1/oauth/{provider}/sign-up") + suspend fun signUp( + @PathVariable provider: OAuthProvider, + @RequestBody request: OAuthSignUpRequest, + ) = oAuthFacade.signUp(provider, request).wrapCreated() }