Fix: Connexion des pages de détails aux APIs backend avec authentification cookies

- Mise à jour de services/api.ts pour supporter l'authentification par cookies HttpOnly
  * Ajout de withCredentials: true dans l'intercepteur de requêtes
  * Modification de l'intercepteur de réponse pour gérer les 401 sans localStorage
  * Utilisation de sessionStorage pour returnUrl au lieu de localStorage
  * Suppression des tentatives de nettoyage de tokens localStorage (gérés par cookies)

- Connexion des pages de détails à apiService au lieu de fetch direct:
  * app/(main)/chantiers/[id]/page.tsx → apiService.chantiers.getById()
  * app/(main)/chantiers/[id]/budget/page.tsx → apiService.budgets.getByChantier()
  * app/(main)/clients/[id]/page.tsx → apiService.clients.getById()
  * app/(main)/materiels/[id]/page.tsx → apiService.materiels.getById()

Avantages:
- Gestion automatique de l'authentification via cookies HttpOnly (plus sécurisé)
- Redirection automatique vers /api/auth/login en cas de 401
- Code plus propre et maintenable
- Gestion d'erreurs cohérente dans toute l'application

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
DahoudG
2025-10-31 12:04:35 +00:00
parent a91a34dbf8
commit be04ef16d9
5 changed files with 32 additions and 55 deletions

View File

@@ -9,6 +9,7 @@ import { Column } from 'primereact/column';
import { ProgressBar } from 'primereact/progressbar';
import { Chart } from 'primereact/chart';
import { Tag } from 'primereact/tag';
import { apiService } from '@/services/api';
interface BudgetChantier {
id: number;
@@ -45,19 +46,11 @@ export default function ChantierBudgetPage() {
const loadBudget = async () => {
try {
setLoading(true);
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'https://api.lions.dev/btpxpress';
// Charger le budget du chantier
const response = await fetch(`${API_URL}/api/v1/budgets/chantier/${id}`);
if (!response.ok) {
throw new Error('Erreur lors du chargement du budget');
}
const data = await response.json();
const data = await apiService.budgets.getByChantier(Number(id));
setBudget(data);
} catch (error) {
console.error('Erreur:', error);
console.error('Erreur lors du chargement du budget:', error);
// L'intercepteur API gérera automatiquement la redirection si 401
} finally {
setLoading(false);
}

View File

@@ -9,6 +9,7 @@ import { Tag } from 'primereact/tag';
import { ProgressBar } from 'primereact/progressbar';
import { Divider } from 'primereact/divider';
import { Skeleton } from 'primereact/skeleton';
import { apiService } from '@/services/api';
interface Chantier {
id: number;
@@ -54,18 +55,12 @@ export default function ChantierDetailsPage() {
const loadChantier = async () => {
try {
setLoading(true);
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'https://api.lions.dev/btpxpress';
const response = await fetch(`${API_URL}/api/v1/chantiers/${id}`);
if (!response.ok) {
throw new Error('Erreur lors du chargement du chantier');
}
const data = await response.json();
const data = await apiService.chantiers.getById(Number(id));
setChantier(data);
} catch (error) {
console.error('Erreur:', error);
// TODO: Afficher un toast d'erreur
console.error('Erreur lors du chargement du chantier:', error);
// L'intercepteur API gérera automatiquement la redirection si 401
// TODO: Afficher un toast d'erreur pour les autres erreurs
} finally {
setLoading(false);
}