diff --git a/src/main/java/com/example/daemawiki/domain/user/dto/ChangePasswordRequest.java b/src/main/java/com/example/daemawiki/domain/user/dto/ChangePasswordRequest.java new file mode 100644 index 00000000..0d982fd1 --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/user/dto/ChangePasswordRequest.java @@ -0,0 +1,7 @@ +package com.example.daemawiki.domain.user.dto; + +public record ChangePasswordRequest( + String oldPassword, + String newPassword +) { +} diff --git a/src/main/java/com/example/daemawiki/domain/user/model/User.java b/src/main/java/com/example/daemawiki/domain/user/model/User.java index 3b55dd87..8e522778 100644 --- a/src/main/java/com/example/daemawiki/domain/user/model/User.java +++ b/src/main/java/com/example/daemawiki/domain/user/model/User.java @@ -44,4 +44,8 @@ public void editDetail(UserDetail detail) { this.detail = detail; } + public void changePassword(String password) { + this.password = password; + } + } diff --git a/src/main/java/com/example/daemawiki/domain/user/service/ChangePassword.java b/src/main/java/com/example/daemawiki/domain/user/service/ChangePassword.java new file mode 100644 index 00000000..93260a62 --- /dev/null +++ b/src/main/java/com/example/daemawiki/domain/user/service/ChangePassword.java @@ -0,0 +1,34 @@ +package com.example.daemawiki.domain.user.service; + +import com.example.daemawiki.domain.user.dto.ChangePasswordRequest; +import com.example.daemawiki.domain.user.repository.UserRepository; +import com.example.daemawiki.domain.user.service.facade.UserFacade; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.stereotype.Service; +import reactor.core.publisher.Mono; + +@Service +public class ChangePassword { + private final PasswordEncoder passwordEncoder; + private final UserFacade userFacade; + private final UserRepository userRepository; + + public ChangePassword(PasswordEncoder passwordEncoder, UserFacade userFacade, UserRepository userRepository) { + this.passwordEncoder = passwordEncoder; + this.userFacade = userFacade; + this.userRepository = userRepository; + } + + public Mono execute(ChangePasswordRequest request) { + return userFacade.currentUser() + .filter(user -> passwordEncoder.matches(request.oldPassword(), user.getPassword())) + .switchIfEmpty(Mono.error(new RuntimeException())) + .map(user -> { + user.changePassword(passwordEncoder.encode(request.newPassword())); + return user; + }) + .flatMap(userRepository::save) + .then(); + } + +}