mise a jour
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
const BASE = '/api/auth'
|
||||
|
||||
function apiError(message, status) {
|
||||
const err = new Error(message)
|
||||
err.status = status
|
||||
return err
|
||||
}
|
||||
|
||||
export async function loginApi({ email, password }) {
|
||||
const res = await fetch(`${BASE}/login`, {
|
||||
method: 'POST',
|
||||
@@ -7,7 +13,7 @@ export async function loginApi({ email, password }) {
|
||||
body: JSON.stringify({ email, password }),
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data.message || 'Erreur de connexion.')
|
||||
if (!res.ok) throw apiError(data.message || 'Erreur de connexion.', res.status)
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -18,6 +24,21 @@ export async function registerApi({ email, firstname, lastname, password }) {
|
||||
body: JSON.stringify({ email, firstname, lastname, password }),
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw new Error(data.message || "Erreur lors de l'inscription.")
|
||||
if (!res.ok) throw apiError(data.message || "Erreur lors de l'inscription.", res.status)
|
||||
return data
|
||||
}
|
||||
|
||||
export async function changePasswordApi({ currentPassword, newPassword }) {
|
||||
const token = localStorage.getItem('token')
|
||||
const res = await fetch(`${BASE}/change-password`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: JSON.stringify({ currentPassword, newPassword }),
|
||||
})
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw apiError(data.message || 'Erreur lors du changement de mot de passe.', res.status)
|
||||
return data
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user