|
@@ -0,0 +1,72 @@
|
|
|
|
|
+package pl.sudra.securityController;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import jakarta.validation.Valid;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.http.MediaType;
|
|
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
|
|
+import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
+import pl.sudra.domain.User;
|
|
|
|
|
+import pl.sudra.service.UserService;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+//import pl.sudra.configuration.SpringConfiguration.PasswordEncoder;
|
|
|
|
|
+import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
|
|
|
+
|
|
|
|
|
+@RestController
|
|
|
|
|
+@CrossOrigin(origins = "http://localhost:1410")
|
|
|
|
|
+public class SecurityController {
|
|
|
|
|
+ private UserService userService;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private PasswordEncoder passwordEncoder;
|
|
|
|
|
+
|
|
|
|
|
+ public SecurityController(UserService userService) {
|
|
|
|
|
+ this.userService = userService;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(
|
|
|
|
|
+ value = "/register",
|
|
|
|
|
+ method = RequestMethod.POST,
|
|
|
|
|
+ produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
+ public ResponseEntity<?> Register(@Valid @RequestBody User user, BindingResult bindingResult) {
|
|
|
|
|
+ if (bindingResult.hasErrors()) {
|
|
|
|
|
+ List<String> errors = bindingResult.getAllErrors()
|
|
|
|
|
+ .stream()
|
|
|
|
|
+ .map(DefaultMessageSourceResolvable::getDefaultMessage)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ ValidationErrorResponse errorResponse = new ValidationErrorResponse();
|
|
|
|
|
+ errorResponse.setMessage("Validation Error");
|
|
|
|
|
+ errorResponse.setErrors(errors);
|
|
|
|
|
+
|
|
|
|
|
+ return ResponseEntity.badRequest().body(errorResponse);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ boolean isUsernameNotUnique = this.userService.isUsernameNotUnique(user.getUsername());
|
|
|
|
|
+ boolean isEmailNotUnique = this.userService.isEmailNotUnique(user.getEmail());
|
|
|
|
|
+
|
|
|
|
|
+ if (isUsernameNotUnique || isEmailNotUnique) {
|
|
|
|
|
+ ValidationErrorResponse errorResponse = new ValidationErrorResponse();
|
|
|
|
|
+ errorResponse.setMessage("Duplication Error");
|
|
|
|
|
+ List<String> errors = new ArrayList<>();
|
|
|
|
|
+ if(isUsernameNotUnique) errors.add("Username already exist in database.");
|
|
|
|
|
+ if(isEmailNotUnique) errors.add("Email already exist in database.");
|
|
|
|
|
+ errorResponse.setErrors(errors);
|
|
|
|
|
+
|
|
|
|
|
+ return ResponseEntity.badRequest().body(errorResponse);
|
|
|
|
|
+ }
|
|
|
|
|
+ System.out.println("Creating reservation");
|
|
|
|
|
+
|
|
|
|
|
+ String hashedPassword = passwordEncoder.encode(user.getPassword());
|
|
|
|
|
+ user.setPassword(hashedPassword);
|
|
|
|
|
+
|
|
|
|
|
+ this.userService.registerUser(user);
|
|
|
|
|
+
|
|
|
|
|
+ return ResponseEntity.ok("Request is valid");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|