Skip to content
Snippets Groups Projects
Commit e4ac8828 authored by Laczkó Csongor Loránd's avatar Laczkó Csongor Loránd
Browse files

feat(backend): add CrossOrigin and modify validation

- Add CrossOrigin to allow requests from the frontend
- Modify validation logic (work in progress)
parent 7ad2fb3b
Branches
Tags
No related merge requests found
......@@ -17,7 +17,6 @@ import java.util.Optional;
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "https://vau-vau.web.app/")
public class Controller {
private final DogService dogService;
private final UserService userService;
......@@ -48,7 +47,7 @@ public class Controller {
@PostMapping("/newdog")
public ResponseEntity<?> addNewDog(@RequestBody DetailedDogDTO dto, @RequestHeader("Authorization") String token) {
if(isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
if(!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
dogService.addNewDog(dto);
return ResponseEntity.status(HttpStatus.CREATED).build();
......@@ -57,7 +56,7 @@ public class Controller {
@PostMapping("/dogs/{id}/edit")
public ResponseEntity<?> editDog(@PathVariable Integer id, @RequestBody DetailedDogDTO dto, @RequestHeader("Authorization") String token) {
if (isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
if (!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
// Retrieve the dog entity from the database
Optional<DetailedDogDTO> optionalDog = dogService.get(id);
if (optionalDog.isPresent()) {
......@@ -73,7 +72,7 @@ public class Controller {
@DeleteMapping("/dogs/{id}")
public ResponseEntity<?> deleteDog(@PathVariable Integer id, @RequestHeader("Authorization") String token) {
if (isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
if (!isValidToken(token)) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
dogService.deleteDog(id);
return ResponseEntity.ok().build();
......
package hu.pazmany.dto;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.Size;
public class UserDTO {
@Min(value = 5, message = "Username must be at least 5 characters long")
@Size(min = 5, message = "Username must be at least 5 characters long")
private String username;
@Min(value = 5, message = "Password must be at least 5 characters long")
@Size(min = 5, message = "Password must be at least 5 characters long")
private String password;
private String token;
......
package hu.pazmany.security;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("https://vau-vau.web.app/", "http://localhost:3000", "http://localhost:3001", "http://localhost:3002", "http://localhost:8080")
.allowedMethods("*");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment