Skip to content

Commit

Permalink
Changed all Requests to use JWTs / Changed search parameters from ID …
Browse files Browse the repository at this point in the history
…to EMAIL
  • Loading branch information
Gugi-Games committed Jan 19, 2024
1 parent 4b5f802 commit 5b76921
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/MeetMate/security/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/com/MeetMate/user/UserController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand All @@ -35,10 +32,9 @@ public void registerNewUser(@RequestParam String token) {
userService.registerNewUser(token);
}

@PutMapping(path = "put")
public void updateUser(@RequestParam MultiValueMap<String, String> formData) {
System.out.println(formData);
userService.updateUser(formData);
@PutMapping(path = "update")
public void updateUser(@RequestParam String token) {
userService.updateUser(token);
}

@PostMapping(path = "auth")
Expand All @@ -47,8 +43,8 @@ public ResponseEntity<String> 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);
}

}
53 changes: 27 additions & 26 deletions src/main/java/com/MeetMate/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ public class UserService {
private final PasswordEncoder passwordEncoder;
private final AuthenticationManager authenticationManager;

public User getUserById(Long userId) {
Optional<User> userOptional = userRepository.findUserById(userId);
return userRepository.findUserById(userId).orElseThrow(() -> new EntityNotFoundException("User does not exist"));
}

public User getUserByEmail(String userEmail) {
Optional<User> 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<User> userOptional = userRepository.findUserByEmail(email);
return userRepository.findUserByEmail(email).orElseThrow(() -> new EntityNotFoundException("User does not exist"));
}

public List<User> getAllUsers() {
Expand All @@ -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<User> userOptional = userRepository.findUserByEmail(email);
userRepository.findUserByEmail(email);
Expand All @@ -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) {
Expand All @@ -71,28 +71,29 @@ public String authenticateUser(String token) {
return jwtService.generateToken(null, user);
}

//doesn't need repository methods
@Transactional
public void updateUser(MultiValueMap<String, String> 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");
Expand Down

0 comments on commit 5b76921

Please sign in to comment.