Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] 비밀번호 재설정 / 변경 API #75

Merged
merged 6 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class WebSecurityConfig(
c.requestMatchers("/users/nickname").permitAll()
c.requestMatchers("/users/eid").permitAll()
c.requestMatchers("/users/reissue").permitAll()
c.requestMatchers("/users/password-reset").permitAll()
c.anyRequest().authenticated()
}
.apply(JwtSecurityConfig(jwtUtils, redisTemplate))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ enum class BaseResponseCode(status: HttpStatus, message: String) {
NOT_EXIST_EMAIL(HttpStatus.BAD_REQUEST, "해당 이메일로 가입한 사용자를 찾을 수 없습니다."),
INVALID_PASSWORD(HttpStatus.BAD_REQUEST, "사용자의 비밀번호가 일치하지 않습니다."),
NOT_FOUND_USER(HttpStatus.NOT_FOUND, "사용자를 찾을 수 없습니다."),
INVALID_PHONE(HttpStatus.BAD_REQUEST, "사용자의 휴대폰 번호와 일치하지 않습니다."),
NOT_FOUND_EID(HttpStatus.NOT_FOUND, "사업자를 찾을 수 없습니다."),
NOT_EMPTY_EID(HttpStatus.BAD_REQUEST, "사업자 정보를 입력해주세요. "),
INVALID_EID(HttpStatus.BAD_REQUEST, "정상 사업자가 아닙니다. (휴업자 or 폐업자)"),
DUPLICATE_PASSWORD(HttpStatus.BAD_REQUEST, "사용자의 비밀번호와 변경하려는 비밀번호가 동일합니다."),

// User - type
INVALID_USER_TYPE_NAME(HttpStatus.BAD_REQUEST, "올바르지 않은 사용자 역할입니다."),
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/com/psr/psr/user/controller/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,25 @@ class UserController(
return BaseResponse(userService.reissueToken(tokenDto))
}

/**
* 비밀번호 변경화면 with Token
*/
@PatchMapping("/password-change")
@ResponseBody
fun changePassword(@AuthenticationPrincipal userAccount: UserAccount, @RequestBody @Validated passwordReq: ChangePasswordReq) : BaseResponse<Any>{
userService.changePassword(userAccount.getUser(), passwordReq)
return BaseResponse(BaseResponseCode.SUCCESS)
}

/**
* 비밀번호 재설정 except Token
*/
@PatchMapping("/password-reset")
@ResponseBody
fun resetPassword(@RequestBody @Validated resetPasswordReq: ResetPasswordReq) : BaseResponse<Any>{
userService.resetPassword(resetPasswordReq)
return BaseResponse(BaseResponseCode.SUCCESS)
}


}
19 changes: 19 additions & 0 deletions src/main/kotlin/com/psr/psr/user/dto/ChangePasswordReq.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.psr.psr.user.dto

import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Pattern

data class ChangePasswordReq (
@field:NotBlank
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message 처리 안되어 있는 req 가 많아서 시간될 때 한번에 처리할게용!

@field:Pattern(
regexp = "^.*(?=^.{8,15}\$)(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#\$%^&+=]).*\$",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나중에 이것도 constant로 빼는거 어때유 !

message = "비밀번호를 숫자, 문자, 특수문자 포함 8~15자리 이내로 입력해주세요"
)
val currentPassword: String,
@field:NotBlank
@field:Pattern(
regexp = "^.*(?=^.{8,15}\$)(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#\$%^&+=]).*\$",
message = "비밀번호를 숫자, 문자, 특수문자 포함 8~15자리 이내로 입력해주세요"
)
val password: String
)
23 changes: 23 additions & 0 deletions src/main/kotlin/com/psr/psr/user/dto/ResetPasswordReq.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.psr.psr.user.dto

import jakarta.validation.constraints.Email
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Pattern

data class ResetPasswordReq (
@field:NotBlank(message = "이메일을 입력해주세요.")
@field:Email(message = "올바르지 않은 이메일 형식입니다.")
val email: String,
@field:NotBlank(message = "휴대폰을 입력해주세요.")
@field:Pattern(
regexp = "^01([0|1|6|7|8|9])-?([0-9]{3,4})-?([0-9]{4})\$",
message = "올바르지 않은 휴대폰 형식입니다."
)
val phone: String,
@field:NotBlank(message = "비밀번호를 입력해주세요.")
@field:Pattern(
regexp = "^.*(?=^.{8,15}\$)(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!@#\$%^&+=]).*\$",
message = "비밀번호를 숫자, 문자, 특수문자 포함 8~15자리 이내로 입력해주세요"
)
val password: String
)
24 changes: 24 additions & 0 deletions src/main/kotlin/com/psr/psr/user/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,28 @@ class UserService(
// token 생성
return jwtUtils.createToken(authentication, user.type)
}

// 비밀번호 변경
@Transactional
fun changePassword(user: User, passwordReq: ChangePasswordReq) {
// 현재 비밀번호 일치 여부
if(!passwordEncoder.matches(passwordReq.currentPassword, user.password)) throw BaseException(INVALID_PASSWORD)
// 현재 비밀번호와 변경하려는 비밀번호 일치 여부
if(passwordReq.currentPassword == passwordReq.password) throw BaseException(DUPLICATE_PASSWORD)

// 비밀번호 변경
user.password = passwordEncoder.encode(passwordReq.password)
userRepository.save(user)
}

// 비밀번호 재설정
fun resetPassword(passwordReq: ResetPasswordReq) {
val user = userRepository.findByEmail(passwordReq.email).orElseThrow{BaseException(NOT_EXIST_EMAIL)}
if(user.phone != passwordReq.phone) throw BaseException(INVALID_PHONE)
if(passwordEncoder.matches(passwordReq.password, user.password)) throw BaseException(DUPLICATE_PASSWORD)

// 비밀번호 변경
user.password = passwordEncoder.encode(passwordReq.password)
userRepository.save(user)
}
}
Loading