diff --git a/src/main/java/com/MeetMate/security/SecurityConfig.java b/src/main/java/com/MeetMate/security/SecurityConfig.java index 818f8e7..bf8367f 100644 --- a/src/main/java/com/MeetMate/security/SecurityConfig.java +++ b/src/main/java/com/MeetMate/security/SecurityConfig.java @@ -16,7 +16,7 @@ public class SecurityConfig { private final JwtAuthenticationFilter jwtAuthenticationFilter; - private final AuthenticationProvider authenticationProvider; + private final AuthenticationProvider authenticationProvider; @Bean public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception { diff --git a/src/main/java/com/MeetMate/user/UserController.java b/src/main/java/com/MeetMate/user/UserController.java index cbdd5c8..aa7aed4 100644 --- a/src/main/java/com/MeetMate/user/UserController.java +++ b/src/main/java/com/MeetMate/user/UserController.java @@ -1,5 +1,6 @@ package com.MeetMate.user; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.util.MultiValueMap; @@ -9,19 +10,15 @@ @RestController @RequestMapping(path = "api/test/user") +@RequiredArgsConstructor public class UserController { private final UserService userService; - @Autowired - public UserController(UserService userService) { - this.userService = userService; - } - @GetMapping(path = "get") @ResponseBody - public User getUser(@RequestParam(name = "id") Long userId) { - return userService.getUserById(userId); + public User getUser(@RequestParam String token) { + return userService.getUserByEmail(token); } @GetMapping(path = "getAll") @@ -35,10 +32,9 @@ public void registerNewUser(@RequestParam String token) { userService.registerNewUser(token); } - @PutMapping(path = "put") - public void updateUser(@RequestParam MultiValueMap formData) { - System.out.println(formData); - userService.updateUser(formData); + @PutMapping(path = "update") + public void updateUser(@RequestParam String token) { + userService.updateUser(token); } @PostMapping(path = "auth") @@ -47,8 +43,8 @@ public ResponseEntity authenticateUser(@RequestParam String token){ } @DeleteMapping(path = "delete") - public void deleteUser(@RequestParam(name = "id") Long userId) { - userService.deleteUser(userId); + public void deleteUser(@RequestParam String token) { + userService.deleteUser(token); } } diff --git a/src/main/java/com/MeetMate/user/UserService.java b/src/main/java/com/MeetMate/user/UserService.java index 6c29f78..3068ca9 100644 --- a/src/main/java/com/MeetMate/user/UserService.java +++ b/src/main/java/com/MeetMate/user/UserService.java @@ -23,14 +23,10 @@ public class UserService { private final PasswordEncoder passwordEncoder; private final AuthenticationManager authenticationManager; - public User getUserById(Long userId) { - Optional userOptional = userRepository.findUserById(userId); - return userRepository.findUserById(userId).orElseThrow(() -> new EntityNotFoundException("User does not exist")); - } - - public User getUserByEmail(String userEmail) { - Optional userOptional = userRepository.findUserByEmail(userEmail); - return userRepository.findUserByEmail(userEmail).orElseThrow(() -> new EntityNotFoundException("User does not exist")); + public User getUserByEmail(String token) { + String email = jwtService.extractClaimGeneric("email", token); + Optional userOptional = userRepository.findUserByEmail(email); + return userRepository.findUserByEmail(email).orElseThrow(() -> new EntityNotFoundException("User does not exist")); } public List getAllUsers() { @@ -41,10 +37,14 @@ public String registerNewUser(String token) { String email = jwtService.extractClaimGeneric("email", token); String name = jwtService.extractClaimGeneric("name", token); String password = jwtService.extractClaimGeneric("password", token); + LocalDate birthday = jwtService.extractClaimGeneric("birthday", token); - User user = new User(name, LocalDate.EPOCH, email, passwordEncoder.encode(password)); + User user = new User(name, birthday, email, passwordEncoder.encode(password)); - if (email != null && password != null && !email.isEmpty() && !password.isEmpty()) { + if (email != null && !email.isEmpty() + && password != null && !password.isEmpty() + && name != null && !name.isEmpty() + && birthday != null) { //check if user already exists Optional userOptional = userRepository.findUserByEmail(email); userRepository.findUserByEmail(email); @@ -55,7 +55,7 @@ public String registerNewUser(String token) { userRepository.save(user); return jwtService.generateToken(null, user); } - throw new EntityNotFoundException("User not found"); + throw new IllegalArgumentException("Required argument is missing"); } public String authenticateUser(String token) { @@ -71,28 +71,29 @@ public String authenticateUser(String token) { return jwtService.generateToken(null, user); } - //doesn't need repository methods @Transactional - public void updateUser(MultiValueMap data) { - long id; - try { - id = Long.parseLong(data.getFirst("id")); - } catch (NumberFormatException nfe) { - throw new IllegalStateException("Invalid id"); - } - String email = data.getFirst("email"); - String password = data.getFirst("password"); + public void updateUser(String token) { + String email = jwtService.extractClaimGeneric("email", token); + String name = jwtService.extractClaimGeneric("name", token); + String password = jwtService.extractClaimGeneric("password", token); + LocalDate birthday = jwtService.extractClaimGeneric("birthday", token); - // is converted from optional to user bc it always exists - User user = userRepository.findUserById(id).orElseThrow(() -> new IllegalStateException("User does not exist.")); + User user = userRepository.findUserByEmail(email).orElseThrow(() -> new EntityNotFoundException("User does not exist.")); - if (userRepository.findUserByEmail(email).isEmpty()) { + if (userRepository.findUserByEmail(email).isEmpty() + && email != null) { user.setEmail(email); } // throw error - user.setPassword(password); + if (password != null) user.setPassword(password); + if (name != null) user.setName(name); + if (birthday != null) user.setBirthday(birthday); } - public void deleteUser(Long userId) { + public void deleteUser(String token) { + String email = jwtService.extractClaimGeneric("email", token); + User user = userRepository.findUserByEmail(email) + .orElseThrow(() -> new EntityNotFoundException("User does not exist.")); + long userId = user.getId(); boolean exists = userRepository.existsById(userId); if (!exists) { throw new IllegalStateException("User does not exist");