Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
비밀번호 변경 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Feb 16, 2024
1 parent c5b3829 commit 1ffddf9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.daemawiki.domain.user.dto;

public record ChangePasswordRequest(
String oldPassword,
String newPassword
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ public void editDetail(UserDetail detail) {
this.detail = detail;
}

public void changePassword(String password) {
this.password = password;
}

}
Original file line number Diff line number Diff line change
@@ -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<Void> 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();
}

}

0 comments on commit 1ffddf9

Please sign in to comment.