commit bb4fbe0597b2d64bdeb47b65e9d7e9a46ef6f9e4
parent 3e8761d27d2af8480ac8c38f60ce91c905da07ea
Author: Andreas Gruhler <andreas.gruhler@adfinis.com>
Date: Sat, 28 Sep 2024 14:30:26 +0200
feat: authenticate athletes
Diffstat:
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/src/frontend/Athletes.jsx b/src/frontend/Athletes.jsx
@@ -6,7 +6,7 @@ const api_uri = import.meta.env.VITE_API_URI
const api_port = import.meta.env.VITE_API_PORT
const locale = import.meta.env.VITE_LOCALE
-async function addAthlete(e) {
+async function addAthlete(e, session) {
e.preventDefault()
// Read the form data
@@ -16,7 +16,10 @@ async function addAthlete(e) {
// create new athlete
const res = await fetch(`${api_uri}:${api_port}/v1/leaderboard/addAthlete`, {
method: 'POST',
- headers: {'Content-Type': 'application/json'},
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${session.auth.token}`,
+ },
body: JSON.stringify({
"nr": formJson.nr,
"firstname": formJson.firstname,
@@ -32,14 +35,17 @@ async function addAthlete(e) {
window.location.reload()
}
-async function deleteAthlete(e, athleteId, athleteFirstName, athleteLastName) {
+async function deleteAthlete(e, athleteId, athleteFirstName, athleteLastName, session) {
e.preventDefault()
const athleteName = athleteFirstName + (athleteLastName ? ' ' + athleteLastName : '')
if (window.confirm('Do you really want to delete athlete "' + athleteName + '"?')) {
const res = await fetch(`${api_uri}:${api_port}/v1/leaderboard/removeAthlete`, {
method: 'POST',
- headers: {'Content-Type': 'application/json'},
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${session.auth.token}`,
+ },
body: JSON.stringify({
"athlete_id": athleteId,
}),
@@ -75,7 +81,12 @@ function AthleteForm({session}) {
useEffect(() => {
(async () => {
setLoading(true)
- const res = await fetch(`${api_uri}:${api_port}/v1/leaderboard/allAthletes`)
+ const res = await fetch(`${api_uri}:${api_port}/v1/leaderboard/allAthletes`, {
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${session.auth.token}`,
+ }
+ })
const { data, error } = await res.json()
if (error) {
console.log(error)
@@ -89,7 +100,7 @@ function AthleteForm({session}) {
return (
<div>
<button disabled={!loading}>{loading ? '↺ loading' : ''}</button>
- <form method='post' onSubmit={addAthlete}>
+ <form method='post' onSubmit={e => addAthlete(e, session)}>
<table>
<thead>
<tr>
@@ -111,7 +122,7 @@ function AthleteForm({session}) {
<td data-title='Lastname'>{a.lastname}</td>
<td data-title='Birthday'>{new Date(a.birthday).toLocaleDateString(locale, dateOptions)}</td>
<td data-title='School'>{a.school}</td>
- <td><button onClick={e => deleteAthlete(e, a.id, a.firstname, a.lastname)}>– del</button></td>
+ <td><button onClick={e => deleteAthlete(e, a.id, a.firstname, a.lastname, session)}>– del</button></td>
</tr>
))}
<tr className='input'>