chore: update

This commit is contained in:
2026-03-03 23:42:23 +01:00
parent 52b059fafa
commit 0073f80a48
95 changed files with 2720 additions and 40619 deletions
+119 -94
View File
@@ -5,7 +5,7 @@
// ✅ loginUser et registerUser retournent AuthResponse
// ✅ sessionStorage (pas localStorage)
const API_URL = "/api/v1";
const API_URL = "https://uber-stup.club/api/v1";
import type {
ConfirmReceptionResponse,
CheckoutCartResponse,
@@ -36,6 +36,7 @@ export interface AuthResponse {
telephone?: string;
role?: string;
session_id?: string;
must_change_password?: boolean;
};
}
@@ -177,11 +178,16 @@ export const loginUser = async (
let errorMessage = "Erreur de connexion";
try {
const errorData = await response.json();
errorMessage = errorData.error || errorData.message || errorMessage;
errorMessage =
errorData.error || errorData.message || errorMessage;
} catch {
// body vide ou non-JSON
}
console.error("❌ [LOGIN] Erreur API:", response.status, errorMessage);
console.error(
"❌ [LOGIN] Erreur API:",
response.status,
errorMessage,
);
return {
success: false,
message: errorMessage,
@@ -235,97 +241,6 @@ export const loginUser = async (
}
};
/**
* ✅ REGISTER - Retourne AuthResponse avec access_token
* POST /api/v1/auth/register
*/
export const registerUser = async (
username: string,
password: string,
nom: string,
prenom: string,
telephone: string,
): Promise<AuthResponse> => {
// ✅ Type de retour CORRECT
try {
console.log("📝 [REGISTER] Appel API...");
const response = await fetch(`${API_URL}/auth/register`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
username,
password,
nom,
prenom,
telephone,
}),
});
if (!response.ok) {
let errorMessage = "Erreur d'inscription";
try {
const errorData = await response.json();
errorMessage = errorData.error || errorData.message || errorMessage;
} catch {
// body vide ou non-JSON
}
console.error("❌ [REGISTER] Erreur API:", response.status, errorMessage);
return {
success: false,
message: errorMessage,
};
}
const data = await response.json();
console.log("📋 [REGISTER] Réponse:", data);
// ✅ Vérifier access_token
if (!data.access_token) {
console.error("❌ [REGISTER] Pas de access_token");
return {
success: false,
message: "Token non reçu du serveur",
};
}
// ✅ Stocker en sessionStorage
sessionStorage.setItem("token", data.access_token);
console.log("✅ [REGISTER] Token stocké");
// ✅ Synchroniser username
const jwtUsername = syncUsernameFromJWT();
if (!jwtUsername) {
console.warn("⚠️ [REGISTER] Impossible de synchroniser username");
return {
success: false,
message: "Erreur synchronisation JWT",
};
}
console.log(`✅ [REGISTER] Créé et connecté: ${jwtUsername}`);
// ✅ Retourner avec access_token
return {
success: true,
message: "Inscription réussie",
access_token: data.access_token, // ✅ IMPORTANT!
token_type: data.token_type,
expires_in: data.expires_in,
user: data.user,
};
} catch (error) {
console.error("❌ [REGISTER] Erreur:", error);
return {
success: false,
message:
error instanceof Error ? error.message : "Erreur d'inscription",
};
}
};
/**
* ✅ LOGOUT
*/
@@ -351,6 +266,43 @@ export const logoutUser = async (): Promise<void> => {
console.log("✅ [LOGOUT] sessionStorage nettoyé");
};
/**
* ✅ CHANGE PASSWORD
* PUT /api/v1/auth/change-password
*/
export const changePassword = async (
currentPassword: string,
newPassword: string,
): Promise<{ success: boolean; message: string }> => {
const token = sessionStorage.getItem("token");
try {
const response = await fetch(`${API_URL}/auth/change-password`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
current_password: currentPassword,
new_password: newPassword,
}),
});
const data = await response.json();
if (!response.ok) {
return {
success: false,
message: data.error || data.message || "Erreur lors du changement de mot de passe",
};
}
return { success: true, message: data.message || "Mot de passe mis à jour" };
} catch (error) {
return {
success: false,
message: error instanceof Error ? error.message : "Erreur de connexion",
};
}
};
// ============================================
// 🛒 PANIER
// ============================================
@@ -1765,3 +1717,76 @@ export const checkoutCart = async (
};
}
};
// ============================================
// 🔔 NOTIFICATIONS CLIENT
// ============================================
export interface ClientNotification {
command_id: number;
type: string;
message: string;
created_at: string;
read: boolean;
}
export interface NotificationsResponse {
success: boolean;
notifications: ClientNotification[];
unread_count: number;
total: number;
}
export const getClientNotifications =
async (): Promise<NotificationsResponse> => {
const token = getAuthToken();
if (!token)
return {
success: false,
notifications: [],
unread_count: 0,
total: 0,
};
try {
const response = await fetch(`${API_URL}/notifications`, {
headers: { Authorization: `Bearer ${token}` },
});
if (!response.ok)
return {
success: false,
notifications: [],
unread_count: 0,
total: 0,
};
const data = await response.json();
return {
success: true,
notifications: data.notifications || [],
unread_count: data.unread_count || 0,
total: data.total || 0,
};
} catch {
return {
success: false,
notifications: [],
unread_count: 0,
total: 0,
};
}
};
export const markNotificationsRead = async (): Promise<{
success: boolean;
}> => {
const token = getAuthToken();
if (!token) return { success: false };
try {
const response = await fetch(`${API_URL}/notifications/read`, {
method: "POST",
headers: { Authorization: `Bearer ${token}` },
});
return { success: response.ok };
} catch {
return { success: false };
}
};