diff --git a/client/src/lib/auth-context.tsx b/client/src/lib/auth-context.tsx index 3282a1c..0c3092a 100644 --- a/client/src/lib/auth-context.tsx +++ b/client/src/lib/auth-context.tsx @@ -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; };