diff --git a/src/main/kotlin/com/gsm/notdo/domain/auth/apdater/output/persistence/AuthenticationQueryPersistenceAdapter.kt b/src/main/kotlin/com/gsm/notdo/domain/auth/apdater/output/persistence/AuthenticationQueryPersistenceAdapter.kt index 5eea03d..77a6615 100644 --- a/src/main/kotlin/com/gsm/notdo/domain/auth/apdater/output/persistence/AuthenticationQueryPersistenceAdapter.kt +++ b/src/main/kotlin/com/gsm/notdo/domain/auth/apdater/output/persistence/AuthenticationQueryPersistenceAdapter.kt @@ -4,6 +4,7 @@ import com.gsm.notdo.domain.auth.apdater.output.persistence.mapper.toDomain import com.gsm.notdo.domain.auth.apdater.output.persistence.repository.AuthenticationRepository import com.gsm.notdo.domain.auth.application.port.output.QueryAuthenticationPort import com.gsm.notdo.domain.auth.domain.Authentication +import com.gsm.notdo.domain.auth.domain.exception.AuthenticationNotFoundException import org.springframework.stereotype.Component @Component @@ -11,8 +12,12 @@ class AuthenticationQueryPersistenceAdapter( private val authenticationRepository: AuthenticationRepository ) : QueryAuthenticationPort { override fun findByEmailOrNull(email: String): Authentication { - val authenticationEntity = authenticationRepository.findByEmailOrNull(email) + val authenticationEntity = authenticationRepository.findByEmailOrNull(email) ?: throw AuthenticationNotFoundException() return authenticationEntity.toDomain(authenticationEntity) } + override fun existsByEmail(email: String): Boolean { + return authenticationRepository.existsByEmail(email) + } + } \ No newline at end of file diff --git a/src/main/kotlin/com/gsm/notdo/domain/auth/apdater/output/persistence/repository/AuthenticationRepository.kt b/src/main/kotlin/com/gsm/notdo/domain/auth/apdater/output/persistence/repository/AuthenticationRepository.kt index 3d3d36a..3e3c749 100644 --- a/src/main/kotlin/com/gsm/notdo/domain/auth/apdater/output/persistence/repository/AuthenticationRepository.kt +++ b/src/main/kotlin/com/gsm/notdo/domain/auth/apdater/output/persistence/repository/AuthenticationRepository.kt @@ -4,5 +4,6 @@ import com.gsm.notdo.domain.auth.apdater.output.persistence.entity.Authenticatio import org.springframework.data.repository.CrudRepository interface AuthenticationRepository : CrudRepository { - fun findByEmailOrNull(email: String): AuthenticationEntity + fun findByEmailOrNull(email: String): AuthenticationEntity? + fun existsByEmail(email: String): Boolean } \ No newline at end of file diff --git a/src/main/kotlin/com/gsm/notdo/domain/auth/application/port/output/PasswordEncodePort.kt b/src/main/kotlin/com/gsm/notdo/domain/auth/application/port/output/PasswordEncodePort.kt index 15a3225..445653e 100644 --- a/src/main/kotlin/com/gsm/notdo/domain/auth/application/port/output/PasswordEncodePort.kt +++ b/src/main/kotlin/com/gsm/notdo/domain/auth/application/port/output/PasswordEncodePort.kt @@ -1,4 +1,4 @@ -package com.gsm.notdo.domain.auth.port.output +package com.gsm.notdo.domain.auth.application.port.output interface PasswordEncodePort { fun encode(password: String): String diff --git a/src/main/kotlin/com/gsm/notdo/domain/auth/application/port/output/QueryAuthenticationPort.kt b/src/main/kotlin/com/gsm/notdo/domain/auth/application/port/output/QueryAuthenticationPort.kt index c5cfb23..0181c80 100644 --- a/src/main/kotlin/com/gsm/notdo/domain/auth/application/port/output/QueryAuthenticationPort.kt +++ b/src/main/kotlin/com/gsm/notdo/domain/auth/application/port/output/QueryAuthenticationPort.kt @@ -4,4 +4,5 @@ import com.gsm.notdo.domain.auth.domain.Authentication interface QueryAuthenticationPort { fun findByEmailOrNull(email: String): Authentication + fun existsByEmail(email: String): Boolean } \ No newline at end of file diff --git a/src/main/kotlin/com/gsm/notdo/domain/auth/application/service/SignUpService.kt b/src/main/kotlin/com/gsm/notdo/domain/auth/application/service/SignUpService.kt index 0d7604a..83994d7 100644 --- a/src/main/kotlin/com/gsm/notdo/domain/auth/application/service/SignUpService.kt +++ b/src/main/kotlin/com/gsm/notdo/domain/auth/application/service/SignUpService.kt @@ -5,48 +5,56 @@ import com.gsm.notdo.domain.auth.domain.RefreshToken import com.gsm.notdo.domain.auth.application.port.input.SignUpUseCase import com.gsm.notdo.domain.auth.application.port.input.dto.SignUpDto import com.gsm.notdo.domain.auth.application.port.output.JwtPort -import com.gsm.notdo.domain.auth.port.output.PasswordEncodePort +import com.gsm.notdo.domain.auth.application.port.output.PasswordEncodePort +import com.gsm.notdo.domain.auth.application.port.output.QueryAuthenticationPort import com.gsm.notdo.domain.auth.port.output.dto.TokenDto import com.gsm.notdo.domain.user.domain.User import com.gsm.notdo.domain.user.domain.exception.UserAlreadyExistException import com.gsm.notdo.domain.user.application.port.output.CommandUserPort import com.gsm.notdo.domain.user.application.port.output.QueryUserPort +import com.gsm.notdo.domain.user.domain.exception.UserNotFoundException +import com.gsm.notdo.domain.user.domain.exception.UserNotVerifyException import org.springframework.stereotype.Service import java.time.LocalDate import java.util.* @Service - class SignUpService( private val queryUserPort: QueryUserPort, private val commandUserPort: CommandUserPort, private val passwordEncoderPort: PasswordEncodePort, private val commandRefreshTokenPort: CommandRefreshTokenPort, + private val queryAuthenticationPort: QueryAuthenticationPort, private val jwtPort: JwtPort ) : SignUpUseCase { override fun execute(dto: SignUpDto): TokenDto { val user = createUser(dto) val token = jwtPort.receiveToken(user.id) + validateAuthentication(dto.email) createRefreshToken(user, token) return token } - private fun createUser(dto: SignUpDto): User { - val isExistUser = queryUserPort.existUserByEmail(dto.email) - - if(isExistUser) { - throw UserAlreadyExistException() + private fun validateAuthentication(email: String) { + val authentication = queryAuthenticationPort.findByEmailOrNull(email) + if(!authentication.isVerified) { + throw UserNotVerifyException() } - val user = User( - id = UUID.randomUUID(), - email = dto.email, - password = passwordEncoderPort.encode(dto.password), - nickname = dto.nickname, - createdAt = LocalDate.now() - ) - return commandUserPort.create(user) } + private fun createUser(dto: SignUpDto): User = + queryUserPort.existUserByEmail(dto.email) + .takeUnless { it } + ?.let { + User( + id = UUID.randomUUID(), + email = dto.email, + password = passwordEncoderPort.encode(dto.password), + nickname = dto.nickname, + createdAt = LocalDate.now() + ) + .also { commandUserPort.save(it) } + } ?: throw UserNotFoundException() private fun createRefreshToken(user: User, token: TokenDto) { val refreshToken = RefreshToken( diff --git a/src/main/kotlin/com/gsm/notdo/domain/auth/domain/exception/AuthenticationNotFoundException.kt b/src/main/kotlin/com/gsm/notdo/domain/auth/domain/exception/AuthenticationNotFoundException.kt new file mode 100644 index 0000000..e0aae50 --- /dev/null +++ b/src/main/kotlin/com/gsm/notdo/domain/auth/domain/exception/AuthenticationNotFoundException.kt @@ -0,0 +1,6 @@ +package com.gsm.notdo.domain.auth.domain.exception + +import com.gsm.notdo.domain.auth.domain.exception.error.AuthenticationErrorCode +import com.gsm.notdo.global.error.BasicException + +class AuthenticationNotFoundException : BasicException(AuthenticationErrorCode.AUTHENTICATION_NOT_FOUND) \ No newline at end of file diff --git a/src/main/kotlin/com/gsm/notdo/domain/auth/domain/exception/error/AuthenticationErrorCode.kt b/src/main/kotlin/com/gsm/notdo/domain/auth/domain/exception/error/AuthenticationErrorCode.kt new file mode 100644 index 0000000..4f628be --- /dev/null +++ b/src/main/kotlin/com/gsm/notdo/domain/auth/domain/exception/error/AuthenticationErrorCode.kt @@ -0,0 +1,14 @@ +package com.gsm.notdo.domain.auth.domain.exception.error + +import com.gsm.notdo.global.error.ErrorProperty + +enum class AuthenticationErrorCode( + private val message: String, + private val status: Int +) : ErrorProperty { + AUTHENTICATION_NOT_FOUND("authentication not found", 404); + + override fun status(): Int = status + + override fun message(): String = message +} \ No newline at end of file diff --git a/src/main/kotlin/com/gsm/notdo/domain/user/adapter/output/UserCommandPersistenceAdapter.kt b/src/main/kotlin/com/gsm/notdo/domain/user/adapter/output/UserCommandPersistenceAdapter.kt index 436cf08..b1ddd95 100644 --- a/src/main/kotlin/com/gsm/notdo/domain/user/adapter/output/UserCommandPersistenceAdapter.kt +++ b/src/main/kotlin/com/gsm/notdo/domain/user/adapter/output/UserCommandPersistenceAdapter.kt @@ -11,7 +11,7 @@ import org.springframework.stereotype.Component class UserCommandPersistenceAdapter( private val userRepository: UserRepository ) : CommandUserPort { - override fun create(domain: User): User { + override fun save(domain: User): User { val userEntity = domain.toEntity(domain) val saveEntity = userRepository.save(userEntity) return saveEntity.toDomain(saveEntity) diff --git a/src/main/kotlin/com/gsm/notdo/domain/user/application/port/output/CommandUserPort.kt b/src/main/kotlin/com/gsm/notdo/domain/user/application/port/output/CommandUserPort.kt index 05ddcf8..5b9d3e0 100644 --- a/src/main/kotlin/com/gsm/notdo/domain/user/application/port/output/CommandUserPort.kt +++ b/src/main/kotlin/com/gsm/notdo/domain/user/application/port/output/CommandUserPort.kt @@ -4,5 +4,5 @@ import com.gsm.notdo.domain.user.domain.User interface CommandUserPort { - fun create(domain: User) : User + fun save(domain: User) : User } \ No newline at end of file diff --git a/src/main/kotlin/com/gsm/notdo/domain/user/domain/exception/UserNotVerifyException.kt b/src/main/kotlin/com/gsm/notdo/domain/user/domain/exception/UserNotVerifyException.kt new file mode 100644 index 0000000..9e0e920 --- /dev/null +++ b/src/main/kotlin/com/gsm/notdo/domain/user/domain/exception/UserNotVerifyException.kt @@ -0,0 +1,6 @@ +package com.gsm.notdo.domain.user.domain.exception + +import com.gsm.notdo.domain.user.domain.exception.error.UserErrorCode +import com.gsm.notdo.global.error.BasicException + +class UserNotVerifyException : BasicException(UserErrorCode.USER_NOT_VERIFIED) \ No newline at end of file diff --git a/src/main/kotlin/com/gsm/notdo/domain/user/domain/exception/error/UserErrorCode.kt b/src/main/kotlin/com/gsm/notdo/domain/user/domain/exception/error/UserErrorCode.kt index e593cef..44ab90f 100644 --- a/src/main/kotlin/com/gsm/notdo/domain/user/domain/exception/error/UserErrorCode.kt +++ b/src/main/kotlin/com/gsm/notdo/domain/user/domain/exception/error/UserErrorCode.kt @@ -7,6 +7,7 @@ enum class UserErrorCode( private val status: Int ) : ErrorProperty { USER_ALREADY_EXIST("user already exist", 400), + USER_NOT_VERIFIED("user not verify", 403), USER_NOT_FOUND("user not found", 404); override fun status(): Int = status diff --git a/src/main/kotlin/com/gsm/notdo/global/security/adapter/PasswordEncodeAdapter.kt b/src/main/kotlin/com/gsm/notdo/global/security/adapter/PasswordEncodeAdapter.kt index 49d10c1..460a535 100644 --- a/src/main/kotlin/com/gsm/notdo/global/security/adapter/PasswordEncodeAdapter.kt +++ b/src/main/kotlin/com/gsm/notdo/global/security/adapter/PasswordEncodeAdapter.kt @@ -1,6 +1,6 @@ package com.gsm.notdo.global.security.adapter -import com.gsm.notdo.domain.auth.port.output.PasswordEncodePort +import com.gsm.notdo.domain.auth.application.port.output.PasswordEncodePort import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.stereotype.Component