Skip to content

Commit

Permalink
Merge pull request #6 from MEOGO-DSM/5-signup-signin-check
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-5) signup signin check
  • Loading branch information
meltapplee authored Sep 2, 2024
2 parents 2254d0f + e7c7fa0 commit 5bd41b4
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.meogo.domain.user.exception

import org.meogo.global.error.exception.ErrorCode
import org.meogo.global.error.exception.MeogoException

object PasswordMismatchException : MeogoException(
ErrorCode.PASSWORD_MISMATCH
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.meogo.domain.user.presentation

import lombok.RequiredArgsConstructor
import org.meogo.domain.user.presentation.dto.request.UserCheckRequest
import org.meogo.domain.user.presentation.dto.request.UserSignInRequest
import org.meogo.domain.user.presentation.dto.request.UserSignUpRequest
import org.meogo.domain.user.service.CheckAccountIdService
import org.meogo.domain.user.service.UserSignInService
import org.meogo.domain.user.service.UserSignUpService
import org.meogo.global.jwt.dto.TokenResponse
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid

@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
class UserController(
private val userSignUpService: UserSignUpService,
private val userSignInService: UserSignInService,
private val userCheckAccountIdService: CheckAccountIdService
) {
@PostMapping("/signup")
fun signUp(
@Valid @RequestBody
request: UserSignUpRequest
): TokenResponse =
userSignUpService.execute(request)

@PostMapping("/signin")
fun signIn(@RequestBody request: UserSignInRequest): TokenResponse =
userSignInService.execute(request)

@PostMapping("/check")
fun checkAccountId(@RequestBody request: UserCheckRequest): Boolean =
userCheckAccountIdService.execute(request)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.meogo.domain.user.presentation.dto.request

data class UserCheckRequest(
val accountId: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.meogo.domain.user.presentation.dto.request

data class UserSignInRequest(
val accountId: String,
val password: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.meogo.domain.user.presentation.dto.request

import javax.validation.constraints.NotNull
import javax.validation.constraints.Size

data class UserSignUpRequest(
@field:NotNull
@field:Size(min = 4, max = 20, message = "아이디는 4~20자 이내여야 합니다")
val accountId: String,

@field:NotNull
@field:Size(min = 12, max = 20, message = "비밀번호는 12~20자 이내여야 합니다")
val password: String,

@field:NotNull
val name: String,

val profile: String?,

val enrolledSchool: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.meogo.domain.user.service

import org.meogo.domain.user.presentation.dto.request.UserCheckRequest
import org.meogo.domain.user.repository.UserRepository
import org.springframework.stereotype.Service

@Service
class CheckAccountIdService(
private val userRepository: UserRepository
) {
fun execute(request: UserCheckRequest): Boolean {
return userRepository.existsByAccountId(request.accountId)
}
}
28 changes: 28 additions & 0 deletions src/main/kotlin/org/meogo/domain/user/service/UserSignInService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.meogo.domain.user.service

import org.meogo.domain.user.exception.PasswordMismatchException
import org.meogo.domain.user.exception.UserNotFoundException
import org.meogo.domain.user.presentation.dto.request.UserSignInRequest
import org.meogo.domain.user.repository.UserRepository
import org.meogo.global.jwt.JwtTokenProvider
import org.meogo.global.jwt.dto.TokenResponse
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class UserSignInService(
private val userRepository: UserRepository,
private val passwordEncoder: PasswordEncoder,
private val jwtTokenProvider: JwtTokenProvider
) {
@Transactional
fun execute(request: UserSignInRequest): TokenResponse {
val user = userRepository.findByAccountId(request.accountId)
?: throw UserNotFoundException

if (!passwordEncoder.matches(request.password, user.password)) throw PasswordMismatchException

return jwtTokenProvider.getToken(user.accountId)
}
}
31 changes: 31 additions & 0 deletions src/main/kotlin/org/meogo/domain/user/service/UserSignUpService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.meogo.domain.user.service

import org.meogo.domain.user.domain.User
import org.meogo.domain.user.domain.UserRole
import org.meogo.domain.user.presentation.dto.request.UserSignUpRequest
import org.meogo.domain.user.repository.UserRepository
import org.meogo.global.jwt.JwtTokenProvider
import org.meogo.global.jwt.dto.TokenResponse
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service

@Service
class UserSignUpService(
private val jwtTokenProvider: JwtTokenProvider,
private val userRepository: UserRepository,
private val passwordEncoder: PasswordEncoder

) {
fun execute(request: UserSignUpRequest): TokenResponse {
val user = User(
name = request.name,
accountId = request.accountId,
password = passwordEncoder.encode(request.password),
enrolledSchool = request.enrolledSchool?.toInt(),
role = UserRole.USER
)
userRepository.save(user)

return jwtTokenProvider.getToken(user.accountId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum class ErrorCode(
) {
INVALID_TOKEN(401, "Invalid Token"),
EXPIRED_TOKEN(401, "Expired Token"),
PASSWORD_MISMATCH(401, "Password Mismatch"),

USER_NOT_FOUND(404, "User not found"),

Expand Down

0 comments on commit 5bd41b4

Please sign in to comment.