Fix auth: hash routing bugs, proper error parsing from JSON responses

master
isnowglobal-admin 2026-05-21 16:24:39 -04:00
parent 5d0b8736fc
commit 87da0d19b3
1 changed files with 26 additions and 6 deletions

View File

@ -59,7 +59,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password }),
});
if (!res.ok) { setError(await res.text()); return; }
if (!res.ok) {
const err = await res.json().catch(() => ({ error: "Authentication failed" }));
setError(err.error || "Authentication failed");
return;
}
const data: User = await res.json();
setUser(data);
setLocation("/");
@ -72,7 +76,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, email, password }),
});
if (!res.ok) { setError(await res.text()); return; }
if (!res.ok) {
const err = await res.json().catch(() => ({ error: "Registration failed" }));
setError(err.error || "Registration failed");
return;
}
const data: User = await res.json();
setUser(data);
setLocation("/");
@ -81,7 +89,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const logout = async () => {
await fetch("/api/auth/logout", { method: "POST" });
setUser(null);
setLocation("/#/login");
setLocation("/login");
};
const updateProfile = async (data: { name?: string; email?: string }) => {
@ -91,7 +99,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (!res.ok) { setError(await res.text()); return false; }
if (!res.ok) {
const err = await res.json().catch(() => ({ error: "Update failed" }));
setError(err.error || "Update failed");
return false;
}
const updated: User = await res.json();
setUser(updated);
return true;
@ -104,7 +116,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ currentPassword: current, newPassword: newPass }),
});
if (!res.ok) { setError(await res.text()); return false; }
if (!res.ok) {
const err = await res.json().catch(() => ({ error: "Password change failed" }));
setError(err.error || "Password change failed");
return false;
}
await res.json();
return true;
};
@ -116,7 +132,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email }),
});
if (!res.ok) { setError(await res.text()); return false; }
if (!res.ok) {
const err = await res.json().catch(() => ({ error: "Request failed" }));
setError(err.error || "Request failed");
return false;
}
await res.json();
return true;
};