Skip to content

Commit

Permalink
refac: auth login 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 9, 2024
1 parent 682256f commit 8fd2bf9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hero.alignlab.domain.auth.application

import com.hero.alignlab.common.encrypt.EncryptData
import com.hero.alignlab.common.encrypt.Encryptor
import com.hero.alignlab.common.extension.coExecute
import com.hero.alignlab.config.database.TransactionTemplates
import com.hero.alignlab.domain.auth.model.AuthContextImpl
import com.hero.alignlab.domain.auth.model.AuthUser
Expand All @@ -12,14 +13,13 @@ import com.hero.alignlab.domain.auth.model.request.SignUpRequest
import com.hero.alignlab.domain.auth.model.response.SignInResponse
import com.hero.alignlab.domain.auth.model.response.SignUpResponse
import com.hero.alignlab.domain.user.application.CredentialUserInfoService
import com.hero.alignlab.domain.user.application.UserService
import com.hero.alignlab.domain.user.application.UserInfoService
import com.hero.alignlab.domain.user.domain.CredentialUserInfo
import com.hero.alignlab.domain.user.domain.UserInfo
import com.hero.alignlab.domain.user.model.response.UserInfoResponse
import com.hero.alignlab.exception.ErrorCode
import com.hero.alignlab.exception.InvalidRequestException
import com.hero.alignlab.exception.InvalidTokenException
import com.hero.alignlab.common.extension.coExecute
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
Expand All @@ -28,7 +28,7 @@ import java.time.LocalDateTime

@Service
class AuthFacade(
private val userService: UserService,
private val userInfoService: UserInfoService,
private val credentialUserInfoService: CredentialUserInfoService,
private val jwtTokenService: JwtTokenService,
private val encryptor: Encryptor,
Expand All @@ -46,7 +46,7 @@ class AuthFacade(
return@handle
}

val user = userService.getUserByIdOrThrowSync(payload.id)
val user = userInfoService.getUserByIdOrThrowSync(payload.id)

sink.next(
AuthUserImpl(
Expand All @@ -65,7 +65,7 @@ class AuthFacade(
}

val userInfo = txTemplates.writer.coExecute {
val userInfo = userService.save(UserInfo(nickname = request.username))
val userInfo = userInfoService.saveSync(UserInfo(nickname = request.username))

credentialUserInfoService.save(
CredentialUserInfo(
Expand All @@ -84,21 +84,16 @@ class AuthFacade(
}

suspend fun signIn(request: SignInRequest): SignInResponse {
val credentialUserInfo = credentialUserInfoService.findByUsernameAndPassword(
username = request.username,
password = request.password
)

val userInfo = userService.getUserByIdOrThrowSync(credentialUserInfo.uid)

val userInfo = userInfoService.findByCredential(request.username, request.password)

val accessToken = jwtTokenService.createToken(userInfo.id, TOKEN_EXPIRED_DATE)

return SignInResponse(accessToken)
}

suspend fun getUserInfo(user: AuthUser): UserInfoResponse {
val userInfo = withContext(Dispatchers.IO) {
userService.getUserByIdOrThrowSync(user.uid)
userInfoService.getUserByIdOrThrowSync(user.uid)
}

return UserInfoResponse.from(userInfo)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hero.alignlab.domain.user.application

import com.hero.alignlab.common.encrypt.EncryptData
import com.hero.alignlab.common.encrypt.Encryptor
import com.hero.alignlab.domain.user.domain.UserInfo
import com.hero.alignlab.domain.user.infrastructure.UserInfoRepository
import com.hero.alignlab.domain.user.model.response.UserInfoResponse
Expand All @@ -11,19 +13,20 @@ import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service

@Service
class UserService(
class UserInfoService(
private val userInfoRepository: UserInfoRepository,
private val encryptor: Encryptor,
) {
fun getUserByIdOrThrowSync(id: Long): UserInfo {
return userInfoRepository.findByIdOrNull(id)
return getUserByIdOrNullSync(id)
?: throw NotFoundException(ErrorCode.NOT_FOUND_USER_ERROR)
}

fun getUserByIdOrNullSync(id: Long): UserInfo? {
return userInfoRepository.findByIdOrNull(id)
}

fun save(userInfo: UserInfo): UserInfo {
fun saveSync(userInfo: UserInfo): UserInfo {
return userInfoRepository.save(userInfo)
}

Expand All @@ -34,4 +37,13 @@ class UserService(

return UserInfoResponse.from(userInfo)
}

suspend fun findByCredential(username: String, password: String): UserInfo {
return withContext(Dispatchers.IO) {
userInfoRepository.findByCredential(
username = username,
password = EncryptData.enc(password, encryptor)
)
} ?: throw NotFoundException(ErrorCode.NOT_FOUND_USER_ERROR)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,14 @@ package com.hero.alignlab.domain.user.infrastructure

import com.hero.alignlab.common.encrypt.EncryptData
import com.hero.alignlab.domain.user.domain.CredentialUserInfo
import com.hero.alignlab.domain.user.domain.QCredentialUserInfo
import com.hero.alignlab.domain.user.domain.QUserInfo
import jakarta.persistence.EntityManager
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = true)
@Repository
interface CredentialUserInfoRepository : JpaRepository<CredentialUserInfo, Long>, CredentialUserInfoQRepository {
interface CredentialUserInfoRepository : JpaRepository<CredentialUserInfo, Long> {
fun existsByUsername(username: String): Boolean

fun findByUsernameAndPassword(username: String, password: EncryptData): CredentialUserInfo?
}

@Transactional(readOnly = true)
interface CredentialUserInfoQRepository {

}

class CredentialUserInfoRepositoryImpl : CredentialUserInfoQRepository,
QuerydslRepositorySupport(CredentialUserInfo::class.java) {
@Autowired
@Qualifier("heroEntityManager")
override fun setEntityManager(entityManager: EntityManager) {
super.setEntityManager(entityManager)
}

private val qCredentialUserInfo = QCredentialUserInfo.credentialUserInfo
private val qUserInfo = QUserInfo.userInfo


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
package com.hero.alignlab.domain.user.infrastructure

import com.hero.alignlab.common.encrypt.EncryptData
import com.hero.alignlab.domain.user.domain.QCredentialUserInfo
import com.hero.alignlab.domain.user.domain.QUserInfo
import com.hero.alignlab.domain.user.domain.UserInfo
import com.querydsl.jpa.impl.JPAQuery
import jakarta.persistence.EntityManager
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport
import org.springframework.stereotype.Repository
import org.springframework.transaction.annotation.Transactional

@Transactional(readOnly = true)
@Repository
interface UserInfoRepository : JpaRepository<UserInfo, Long>
interface UserInfoRepository : JpaRepository<UserInfo, Long>, UserInfoQRepository

@Transactional(readOnly = true)
interface UserInfoQRepository {
fun findByCredential(username: String, password: EncryptData): UserInfo?
}

class UserInfoQRepositoryImpl : UserInfoQRepository, QuerydslRepositorySupport(UserInfo::class.java) {
@Autowired
@Qualifier("heroEntityManager")
override fun setEntityManager(entityManager: EntityManager) {
super.setEntityManager(entityManager)
}

private val qUserInfo = QUserInfo.userInfo
private val qCredentialUserInfo = QCredentialUserInfo.credentialUserInfo

override fun findByCredential(username: String, password: EncryptData): UserInfo? {
return JPAQuery<UserInfo>(entityManager)
.select(qUserInfo)
.from(qUserInfo)
.join(qCredentialUserInfo).on(qUserInfo.id.eq(qCredentialUserInfo.uid))
.where(
qCredentialUserInfo.username.eq(username),
qCredentialUserInfo.password.eq(password)
).fetchFirst()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.hero.alignlab.domain.user.resource

import com.hero.alignlab.config.swagger.SwaggerTag.DEV_TAG
import com.hero.alignlab.domain.user.application.UserService
import com.hero.alignlab.common.extension.wrapOk
import com.hero.alignlab.config.swagger.SwaggerTag.DEV_TAG
import com.hero.alignlab.domain.user.application.UserInfoService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.MediaType
Expand All @@ -14,12 +14,12 @@ import org.springframework.web.bind.annotation.RestController
@Tag(name = DEV_TAG)
@RestController
@RequestMapping(produces = [MediaType.APPLICATION_JSON_VALUE])
class DevUserResource(
private val userService: UserService,
class DevUserInfoResource(
private val userInfoService: UserInfoService,
) {
@Operation(summary = "유저 정보 조회")
@GetMapping("/api/dev/v1/users/{id}")
suspend fun getUserInfo(
@PathVariable id: Long,
) = userService.getUserInfo(id).wrapOk()
) = userInfoService.getUserInfo(id).wrapOk()
}

0 comments on commit 8fd2bf9

Please sign in to comment.