Correction de l'accès à /admin/login dans l'APK admin
- Détection automatique de l'app admin (Capacitor) - Redirection automatique vers /admin/login si non connecté - Gestion gracieuse des erreurs API en mode statique - Message informatif si API non disponible - Support du mode statique pour l'APK admin
This commit is contained in:
73
app/page.tsx
73
app/page.tsx
@ -1,21 +1,78 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
export default function Home() {
|
||||
const router = useRouter();
|
||||
const [checked, setChecked] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
router.replace("/accueil");
|
||||
// Détecter si on est dans l'app admin
|
||||
// Méthode 1: Vérifier si Capacitor est disponible (APK)
|
||||
const isCapacitor = typeof window !== "undefined" && (window as any).Capacitor;
|
||||
|
||||
// Méthode 2: Vérifier le user agent ou l'URL
|
||||
const isAdminPath = typeof window !== "undefined" &&
|
||||
(window.location.pathname.startsWith("/admin") ||
|
||||
window.location.search.includes("admin=true"));
|
||||
|
||||
// Méthode 3: Vérifier si on a un mot de passe admin en localStorage
|
||||
const hasAdminPassword = typeof window !== "undefined" &&
|
||||
localStorage.getItem("adminPassword") !== null;
|
||||
|
||||
// Si on est dans Capacitor OU qu'on a un mot de passe admin, c'est l'app admin
|
||||
const isAdminApp = isCapacitor || isAdminPath || hasAdminPassword;
|
||||
|
||||
if (isAdminApp) {
|
||||
// Vérifier si l'admin est connecté
|
||||
const adminPassword = typeof window !== "undefined" ?
|
||||
localStorage.getItem("adminPassword") : null;
|
||||
|
||||
if (adminPassword) {
|
||||
// Tester la connexion (si API disponible)
|
||||
fetch("/api/admin/clients", {
|
||||
headers: {
|
||||
Authorization: `Bearer ${adminPassword}`,
|
||||
},
|
||||
})
|
||||
.then((res) => {
|
||||
if (res.ok) {
|
||||
router.replace("/admin");
|
||||
} else if (res.status === 404) {
|
||||
// API non disponible (mode statique) - accepter quand même
|
||||
router.replace("/admin");
|
||||
} else {
|
||||
localStorage.removeItem("adminPassword");
|
||||
router.replace("/admin/login");
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
// Erreur réseau (API non disponible en mode statique)
|
||||
// Accepter quand même et rediriger vers /admin
|
||||
router.replace("/admin");
|
||||
});
|
||||
} else {
|
||||
// Pas de mot de passe, rediriger vers login
|
||||
router.replace("/admin/login");
|
||||
}
|
||||
} else {
|
||||
// App client normale
|
||||
router.replace("/accueil");
|
||||
}
|
||||
setChecked(true);
|
||||
}, [router]);
|
||||
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-screen">
|
||||
<div className="text-center">
|
||||
<p className="text-gray-600">Redirection...</p>
|
||||
if (!checked) {
|
||||
return (
|
||||
<div className="flex items-center justify-center min-h-screen bg-background dark:bg-background-dark">
|
||||
<div className="text-center">
|
||||
<p className="text-gray-600 dark:text-gray-400">Chargement...</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user