create account marche
This commit is contained in:
@@ -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)))
|
@ApiResponse(responseCode = "200", description = "Récupère tous les athlètes", content = @Content(mediaType = "application/json", schema = @Schema(implementation = List.class)))
|
||||||
})
|
})
|
||||||
@GetMapping("/all")
|
@GetMapping("/all")
|
||||||
@PreAuthorize("hasRole('admin') or hasRole('coach') or hasRole('athlete')")
|
@PreAuthorize("hasRole('admin') or hasRole('coach')")
|
||||||
public ResponseEntity<List<AthleteDTO>> all() {
|
public ResponseEntity<List<AthleteDTO>> all() {
|
||||||
List<Athlete> athletes = athleteDAO.findAll();
|
List<Athlete> athletes = athleteDAO.findAll();
|
||||||
List<AthleteDTO> dtos = new ArrayList<>();
|
List<AthleteDTO> dtos = new ArrayList<>();
|
||||||
|
|||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,9 +8,18 @@ export const Login =() =>{
|
|||||||
const { keycloak } = useKeycloak();
|
const { keycloak } = useKeycloak();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const syncUser = async () => {
|
const syncAndLoadUser = async () => {
|
||||||
if (keycloak.authenticated && keycloak.token) {
|
if (keycloak.authenticated && keycloak.token) {
|
||||||
console.log("Attempting to sync user with backend...");
|
const tokenParsed = keycloak.tokenParsed;
|
||||||
|
setUser({
|
||||||
|
id: 0,
|
||||||
|
keycloakId: tokenParsed!.sub!,
|
||||||
|
email: tokenParsed?.email || "",
|
||||||
|
nom: tokenParsed?.family_name || "",
|
||||||
|
prenom: tokenParsed?.given_name || "",
|
||||||
|
role: "athlete",
|
||||||
|
sessions: []
|
||||||
|
});
|
||||||
try {
|
try {
|
||||||
const response = await fetch("http://localhost:8081/api/users/sync", {
|
const response = await fetch("http://localhost:8081/api/users/sync", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -26,36 +35,8 @@ export const Login =() =>{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
syncUser();
|
syncAndLoadUser();
|
||||||
}, [keycloak.authenticated, keycloak.token]);
|
}, [keycloak.authenticated, keycloak.token, setUser]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (keycloak.authenticated && keycloak.token) {
|
|
||||||
fetch("http://localhost:8081/api/users/sync", {
|
|
||||||
method: "POST",
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${keycloak.token}`,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then(res => console.log("Sync response status:", res.status))
|
|
||||||
.catch(err => console.error("Sync error:", err));;
|
|
||||||
}
|
|
||||||
}, [keycloak.authenticated]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (keycloak.authenticated) {
|
|
||||||
const tokenParsed = keycloak.tokenParsed;
|
|
||||||
setUser({
|
|
||||||
id: 0,
|
|
||||||
keycloakId: tokenParsed!.sub!,
|
|
||||||
email: tokenParsed?.email,
|
|
||||||
nom: tokenParsed?.family_name,
|
|
||||||
prenom: tokenParsed?.given_name,
|
|
||||||
role: "athlete",
|
|
||||||
sessions: []
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}, [keycloak.authenticated]);
|
|
||||||
|
|
||||||
|
|
||||||
function handleLogin(): void {
|
function handleLogin(): void {
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import { unescapeLeadingUnderscores } from "typescript";
|
|||||||
console.log(user.nom);
|
console.log(user.nom);
|
||||||
console.log(user.prenom);
|
console.log(user.prenom);
|
||||||
console.log(user.email);
|
console.log(user.email);
|
||||||
if (user.role === "Athlete") return null;
|
if (user.role === "athlete") return null;
|
||||||
|
|
||||||
|
|
||||||
const athleteMap: Map<number, Athlete> = new Map();
|
const athleteMap: Map<number, Athlete> = new Map();
|
||||||
@@ -82,7 +82,7 @@ import { unescapeLeadingUnderscores } from "typescript";
|
|||||||
}}>
|
}}>
|
||||||
<option value="athletes">Athlètes</option>
|
<option value="athletes">Athlètes</option>
|
||||||
<option value="activites">Activités</option>
|
<option value="activites">Activités</option>
|
||||||
{user.role === "Admin" && <option value="coachs"> Coachs</option>}
|
{user.role === "admin" && <option value="coachs"> Coachs</option>}
|
||||||
<option value="sessions"> Sessions</option>
|
<option value="sessions"> Sessions</option>
|
||||||
<option value="lignes"> Lignes</option>
|
<option value="lignes"> Lignes</option>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user