From 3c1b8202c2e520dc384cde332a574c4364348415 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Mar 2026 10:03:56 +0000 Subject: [PATCH 1/2] Initial plan From 919d0ae3ac84e75b489a286bbac968e2ca7671b0 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Mar 2026 10:06:54 +0000 Subject: [PATCH 2/2] refactor: use Better Auth get-session as auth source of truth in AuthContext Co-authored-by: benhalverson <7907232+benhalverson@users.noreply.github.com> --- src/context/AuthContext.tsx | 16 +++++++++++++--- src/pages/Signin.tsx | 9 +++------ src/pages/Signup.tsx | 4 ++++ 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index edcf095..4baecda 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -28,11 +28,21 @@ export const AuthProvider = ({ children }: { children: ReactNode }) => { const fetchUser = async () => { try { - const res = await fetch(`${BASE_URL}/profile`, { + const sessionRes = await fetch(`${BASE_URL}/api/auth/get-session`, { credentials: "include", }); - if (res.ok) { - const data: any = await res.json(); + const sessionData = sessionRes.ok ? await sessionRes.json() : null; + + if (!sessionData?.session) { + setUser(null); + return; + } + + const profileRes = await fetch(`${BASE_URL}/profile`, { + credentials: "include", + }); + if (profileRes.ok) { + const data: any = await profileRes.json(); setUser(data); } else { setUser(null); diff --git a/src/pages/Signin.tsx b/src/pages/Signin.tsx index 48910ab..ab448d5 100644 --- a/src/pages/Signin.tsx +++ b/src/pages/Signin.tsx @@ -12,7 +12,7 @@ const Signin = () => { const navigate = useNavigate(); const [loading, setLoading] = useState(false); const [tab, setTab] = useState<"password" | "passkey">("password"); - const { setUser } = useAuth(); + const { fetchUser } = useAuth(); const schema = z.object({ email: z.string().email({ message: "Invalid email address" }), @@ -55,11 +55,7 @@ const Signin = () => { }); if (!res.ok) throw new Error("Invalid credentials"); - const profileRes = await fetch(`${BASE_URL}/profile`, { - credentials: "include", - }); - const profileData: any = await profileRes.json(); - setUser(profileData); + await fetchUser(); toast.success("Signed in!", { id: toastId }); navigate("/profile"); } catch (err: unknown) { @@ -149,6 +145,7 @@ const Signin = () => { throw new Error(result.error || "Passkey verification failed"); } + await fetchUser(); toast.success("Passkey login successful!", { id: toastId }); navigate("/profile"); } catch (err: unknown) { diff --git a/src/pages/Signup.tsx b/src/pages/Signup.tsx index 848ee37..d677dcf 100644 --- a/src/pages/Signup.tsx +++ b/src/pages/Signup.tsx @@ -5,6 +5,7 @@ import { toast } from "react-hot-toast"; import { useNavigate } from "react-router-dom"; import { z } from "zod"; import { BASE_URL } from "../config"; +import { useAuth } from "../context/AuthContext"; import { base64urlToUint8Array, bufferToBase64 } from "../utils/webauthn"; const schema = z.object({ @@ -17,6 +18,7 @@ type SignupFormData = z.infer; const Signup = () => { const resolver = zodResolver(schema); const navigate = useNavigate(); + const { fetchUser } = useAuth(); const { register, @@ -39,6 +41,7 @@ const Signup = () => { }); if (!res.ok) throw new Error("Signup failed"); + await fetchUser(); toast.success("Signup successful!", { id: toastId }); navigate("/profile"); } catch (err: unknown) { @@ -109,6 +112,7 @@ const Signup = () => { }); if (!finishRes.ok) throw new Error("Failed to finish WebAuthn"); + await fetchUser(); toast.success("Passkey signup complete!", { id: toastId }); navigate("/profile"); reset();