create account marche

This commit is contained in:
tuanvu
2026-01-08 13:32:53 +01:00
parent a3cf9b821a
commit 85daf4647a
4 changed files with 64 additions and 35 deletions

View File

@@ -60,7 +60,7 @@ public class AthleteResource {
@ApiResponse(responseCode = "200", description = "Récupère tous les athlètes", content = @Content(mediaType = "application/json", schema = @Schema(implementation = List.class)))
})
@GetMapping("/all")
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
@PreAuthorize("hasRole('admin') or hasRole('coach')")
public ResponseEntity<List<AthleteDTO>> all() {
List<Athlete> athletes = athleteDAO.findAll();
List<AthleteDTO> dtos = new ArrayList<>();

View File

@@ -0,0 +1,48 @@
package hackathon.FrisbYEE.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.core.context.SecurityContextHolder;
import hackathon.FrisbYEE.jpa.metier.Athlete;
import hackathon.FrisbYEE.jpa.service.AthleteDAO;
import jakarta.transaction.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/users")
@CrossOrigin(origins = "http://localhost:3000")
public class UserSyncResource {
@Autowired
private AthleteDAO athleteDAO;
@PostMapping("/sync")
@Transactional
public ResponseEntity<Void> sync() {
Jwt jwt = (Jwt) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
String keycloakId = jwt.getSubject();
String firstName = jwt.getClaimAsString("given_name");
String lastName = jwt.getClaimAsString("family_name");
if (!athleteDAO.existsByKeycloakId(keycloakId)) {
System.out.println("New user detected from Keycloak. Syncing: " + firstName + " " + lastName);
Athlete athlete = new Athlete();
athlete.setKeycloakId(keycloakId);
athlete.setName(lastName);
athlete.setPrenom(firstName);
athlete.setRole(hackathon.FrisbYEE.jpa.metier.Role.athlete);
athleteDAO.save(athlete);
}
return ResponseEntity.ok().build();
}
}