Checkpoint: Implémentation authentification hybride (cookies + Authorization header) - Résolution du problème de connexion dans l'environnement de preview Manus

This commit is contained in:
Manus Sandbox
2025-11-19 06:40:14 -05:00
parent a5b78153a0
commit a1d67c7451
7 changed files with 54 additions and 8 deletions

View File

@@ -36,6 +36,8 @@ export function useAuth(options?: UseAuthOptions) {
}
throw error;
} finally {
// Supprimer le token de localStorage
localStorage.removeItem('auth_token');
utils.auth.me.setData(undefined, null);
await utils.auth.me.invalidate();
}

View File

@@ -43,9 +43,22 @@ const trpcClient = trpc.createClient({
url: "/api/trpc",
transformer: superjson,
fetch(input, init) {
// Récupérer le token depuis localStorage
const token = localStorage.getItem('auth_token');
const headers: HeadersInit = {
...(init?.headers || {}),
};
// Ajouter le header Authorization si un token existe
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
return globalThis.fetch(input, {
...(init ?? {}),
credentials: "include",
headers,
});
},
}),

View File

@@ -16,7 +16,11 @@ export default function Login() {
const [error, setError] = useState("");
const loginMutation = trpc.auth.login.useMutation({
onSuccess: () => {
onSuccess: (data) => {
// Stocker le token dans localStorage
if (data.token) {
localStorage.setItem('auth_token', data.token);
}
// Rediriger vers le tableau de bord admin
window.location.href = "/admin";
},

View File

@@ -18,7 +18,11 @@ export default function Register() {
const [error, setError] = useState("");
const registerMutation = trpc.auth.register.useMutation({
onSuccess: () => {
onSuccess: (data) => {
// Stocker le token dans localStorage
if (data.token) {
localStorage.setItem('auth_token', data.token);
}
// Rediriger vers le tableau de bord
window.location.href = "/";
},