124 lines
3.9 KiB
JavaScript
124 lines
3.9 KiB
JavaScript
const API_URL = import.meta.env.VITE_API_URL || ''
|
|
const BASE = `${API_URL}/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',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password }),
|
|
})
|
|
const data = await res.json()
|
|
if (!res.ok) throw apiError(data.message || 'Erreur de connexion.', res.status)
|
|
return data
|
|
}
|
|
|
|
export async function registerApi({
|
|
email,
|
|
firstname,
|
|
lastname,
|
|
password,
|
|
birthdate,
|
|
gender,
|
|
marketingOptIn,
|
|
}) {
|
|
const res = await fetch(`${BASE}/register`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
// birthdate et gender sont facultatifs : on omet la clé plutôt que
|
|
// d'envoyer une chaîne vide, que l'API stockerait telle quelle.
|
|
body: JSON.stringify({
|
|
email,
|
|
firstname,
|
|
lastname,
|
|
password,
|
|
birthdate: birthdate || undefined,
|
|
gender: gender || undefined,
|
|
marketingOptIn: marketingOptIn ?? undefined,
|
|
}),
|
|
})
|
|
const data = await res.json()
|
|
if (!res.ok) {
|
|
// Le backend renvoie un message générique ("Données invalides") avec le
|
|
// détail par champ dans `errors` : on préfère afficher ce détail quand il
|
|
// existe (ex. l'âge minimum requis pour la date de naissance).
|
|
const fieldMessage = Object.values(data.errors || {})[0]?.[0]
|
|
throw apiError(fieldMessage || data.message || "Erreur lors de l'inscription.", res.status)
|
|
}
|
|
return data
|
|
}
|
|
|
|
export async function verifyCodeApi({ email, code }) {
|
|
const res = await fetch(`${BASE}/verify-code`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, code }),
|
|
})
|
|
const data = await res.json()
|
|
if (!res.ok) throw apiError(data.message || 'Code invalide.', res.status)
|
|
return data
|
|
}
|
|
|
|
export async function forgotPasswordApi(email) {
|
|
const res = await fetch(`${BASE}/forgot-password`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email }),
|
|
})
|
|
const data = await res.json()
|
|
if (!res.ok) throw apiError(data.message || "Erreur lors de l'envoi du lien.", res.status)
|
|
return data
|
|
}
|
|
|
|
export async function resetPasswordApi({ token, password, confirmPassword }) {
|
|
const res = await fetch(`${BASE}/reset-password/${token}`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ password, confirmPassword }),
|
|
})
|
|
const data = await res.json()
|
|
if (!res.ok) throw apiError(data.message || 'Erreur lors de la définition du mot de passe.', res.status)
|
|
return data
|
|
}
|
|
|
|
export async function meApi() {
|
|
const token = localStorage.getItem('token')
|
|
const res = await fetch(`${BASE}/me`, {
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
})
|
|
const data = await res.json()
|
|
if (!res.ok) throw apiError(data.message || 'Erreur lors de la récupération du profil.', res.status)
|
|
return data
|
|
}
|
|
|
|
export async function deleteAccountApi() {
|
|
const token = localStorage.getItem('token')
|
|
const res = await fetch(`${BASE}/account`, {
|
|
method: 'DELETE',
|
|
headers: { Authorization: `Bearer ${token}` },
|
|
})
|
|
const data = await res.json()
|
|
if (!res.ok) throw apiError(data.message || 'Erreur lors de la suppression.', 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
|
|
}
|