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 22, 2024
1 parent e145f80 commit 7a8f771
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Mono<Void> profileUpload(@RequestPart(value = "file", required = true) Fi
@PatchMapping("/password")
@ResponseStatus(HttpStatus.NO_CONTENT)
public Mono<Void> changePassword(@Valid @RequestBody ChangePasswordRequest request) {
return changePasswordService.execute(request);
return changePasswordService.currentUser(request);
}

@GetMapping("/gen")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.example.daemawiki.domain.user.service;

import com.example.daemawiki.domain.mail.repository.AuthMailRepository;
import com.example.daemawiki.domain.user.dto.ChangePasswordRequest;
import com.example.daemawiki.domain.user.model.User;
import com.example.daemawiki.domain.user.repository.UserRepository;
import com.example.daemawiki.domain.user.service.facade.UserFacade;
import com.example.daemawiki.global.exception.h403.UnVerifiedEmailException;
import com.example.daemawiki.global.exception.h500.ExecuteFailedException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
Expand All @@ -13,21 +16,36 @@ public class ChangePassword {
private final PasswordEncoder passwordEncoder;
private final UserFacade userFacade;
private final UserRepository userRepository;
private final AuthMailRepository authMailRepository;

public ChangePassword(PasswordEncoder passwordEncoder, UserFacade userFacade, UserRepository userRepository) {
public ChangePassword(PasswordEncoder passwordEncoder, UserFacade userFacade, UserRepository userRepository, AuthMailRepository authMailRepository) {
this.passwordEncoder = passwordEncoder;
this.userFacade = userFacade;
this.userRepository = userRepository;
this.authMailRepository = authMailRepository;
}

public Mono<Void> execute(ChangePasswordRequest request) {
public Mono<Void> currentUser(ChangePasswordRequest request) {
return userFacade.currentUser()
//.filter(user -> passwordEncoder.matches(request.oldPassword(), user.getPassword()))
//.switchIfEmpty(Mono.error(PasswordMismatchException.EXCEPTION))
.flatMap(user -> {
user.changePassword(passwordEncoder.encode(request.newPassword()));
return userRepository.save(user);
})
.flatMap(user -> changePasswordAndSaveUser(request.newPassword(), user));
}

public Mono<Void> NonLoggedInUser(ChangePasswordRequest request) {
return userFacade.findByEmailNotNull(request.email())
.flatMap(user -> authMailRepository.findByMail(request.email())
.flatMap(verified -> {
if (!verified) {
return Mono.error(UnVerifiedEmailException.EXCEPTION);
}

return changePasswordAndSaveUser(request.newPassword(), user);
}));
}

private Mono<Void> changePasswordAndSaveUser(String newPassword, User user) {
user.changePassword(passwordEncoder.encode(newPassword));

return userRepository.save(user)
.onErrorMap(e -> ExecuteFailedException.EXCEPTION)
.then();
}
Expand Down

0 comments on commit 7a8f771

Please sign in to comment.