-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from MEOGO-DSM/5-signup-signin-check
🏄 :: (Meogo-5) signup signin check
- Loading branch information
Showing
9 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/org/meogo/domain/user/exception/PasswordMismatchException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/org/meogo/domain/user/presentation/dto/request/UserCheckRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/org/meogo/domain/user/presentation/dto/request/UserSignInRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/org/meogo/domain/user/presentation/dto/request/UserSignUpRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
) |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/org/meogo/domain/user/service/CheckAccountIdService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/main/kotlin/org/meogo/domain/user/service/UserSignInService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/main/kotlin/org/meogo/domain/user/service/UserSignUpService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters