Skip to content

Commit

Permalink
feat: OAuth Sign Up
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 30, 2024
1 parent bd4f514 commit 1263263
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hero.alignlab.domain.auth.model.request

data class OAuthLoginRequest(
data class OAuthSignInRequest(
/** oauth accessToken */
val accessToken: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.hero.alignlab.domain.auth.model.request

data class OAuthSignUpRequest(
/** oauth accessToken */
val accessToken: String,
)
Original file line number Diff line number Diff line change
@@ -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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
}

0 comments on commit 1263263

Please sign in to comment.