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:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
},
|
||||
}),
|
||||
|
||||
@@ -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";
|
||||
},
|
||||
|
||||
@@ -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 = "/";
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user