Add password update
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.controller.auth.ChangePasswordRequest;
|
||||
import com.faf223.expensetrackerfaf.dto.UserCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.UserDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.UserMapper;
|
||||
import com.faf223.expensetrackerfaf.model.Credential;
|
||||
import com.faf223.expensetrackerfaf.model.User;
|
||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||
import com.faf223.expensetrackerfaf.service.AuthenticationService;
|
||||
import com.faf223.expensetrackerfaf.service.UserService;
|
||||
import com.faf223.expensetrackerfaf.util.errors.ErrorResponse;
|
||||
import com.faf223.expensetrackerfaf.util.exceptions.UserNotCreatedException;
|
||||
@@ -34,11 +36,12 @@ public class UserController {
|
||||
private final UserService userService;
|
||||
private final UserMapper userMapper;
|
||||
private final CredentialRepository credentialRepository;
|
||||
private final AuthenticationService authenticationService;
|
||||
|
||||
@PatchMapping()
|
||||
public ResponseEntity<UserDTO> updateUser(@RequestBody @Valid UserCreationDTO userDTO,
|
||||
BindingResult bindingResult) {
|
||||
if(bindingResult.hasErrors())
|
||||
if (bindingResult.hasErrors())
|
||||
throw new UserNotCreatedException(ErrorResponse.from(bindingResult).getMessage());
|
||||
|
||||
User user = userMapper.toUser(userDTO);
|
||||
@@ -53,6 +56,15 @@ public class UserController {
|
||||
}
|
||||
}
|
||||
|
||||
@PatchMapping("/update-password")
|
||||
public ResponseEntity<Void> updateUserPassword(@RequestBody ChangePasswordRequest password) {
|
||||
|
||||
System.out.println("Hi");
|
||||
|
||||
authenticationService.updatePassword(password.getPassword());
|
||||
return ResponseEntity.status(HttpStatus.OK).build();
|
||||
}
|
||||
|
||||
@GetMapping("/get-user-data")
|
||||
public ResponseEntity<Map<String, String>> getUser() {
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.faf223.expensetrackerfaf.controller.auth;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class ChangePasswordRequest {
|
||||
private String password;
|
||||
}
|
||||
@@ -15,6 +15,8 @@ import com.faf223.expensetrackerfaf.util.exceptions.UserNotFoundException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.security.oauth2.core.user.OAuth2User;
|
||||
@@ -27,6 +29,7 @@ import java.util.Optional;
|
||||
public class AuthenticationService {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final UserService userService;
|
||||
private final CredentialRepository credentialRepository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
private final JwtService jwtService;
|
||||
@@ -136,4 +139,22 @@ public class AuthenticationService {
|
||||
}
|
||||
}
|
||||
|
||||
public void updatePassword(String newPassword) {
|
||||
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||
User user = userService.getUserByEmail(userDetails.getUsername());
|
||||
Optional<Credential> credential = credentialRepository.findByUser(user);
|
||||
|
||||
if (credential.isPresent()) {
|
||||
|
||||
Credential updatedCredential = credential.get();
|
||||
updatedCredential.setPassword(passwordEncoder.encode(newPassword));
|
||||
credentialRepository.save(updatedCredential);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user