diff --git a/src/app/FamilyProvider.tsx b/src/app/FamilyProvider.tsx index 2f71af8..e849285 100644 --- a/src/app/FamilyProvider.tsx +++ b/src/app/FamilyProvider.tsx @@ -2,6 +2,7 @@ import { useState, useEffect, createContext, useContext } from "react"; import { ReactNode } from "react"; +import { useRouter } from "next/navigation"; interface Child { id: string; @@ -35,6 +36,7 @@ export function useFamily() { } export function FamilyProvider({ children: providerChildren }: { children: ReactNode }) { + const router = useRouter(); const [familyId, setFamilyId] = useState(null); const [childId, setChildId] = useState(null); const [child, setChild] = useState(null); @@ -50,17 +52,22 @@ export function FamilyProvider({ children: providerChildren }: { children: React const sessionRes = await fetch("/api/auth/signin"); const sessionData = await sessionRes.json(); + // Not authenticated - redirect to login if (!sessionData.authenticated) { - // Not logged in, use default - setFamilyId("default"); + router.push("/login"); setLoading(false); return; } - const sessionFamilyId = sessionData.familyId || "default"; + // Authenticated but no family - go to onboarding + if (!sessionData.familyId) { + router.push("/onboarding"); + setLoading(false); + return; + } // Fetch children for this family - const res = await fetch(`/api/children?familyId=${sessionFamilyId}`); + const res = await fetch(`/api/children?familyId=${sessionData.familyId}`); const data = await res.json(); if (data.children?.length > 0) { @@ -74,9 +81,12 @@ export function FamilyProvider({ children: providerChildren }: { children: React setChildren(childList); setChild(childList[0]); setChildId(childList[0].id); + } else { + // No children - go to onboarding + router.push("/onboarding"); } - setFamilyId(sessionFamilyId); + setFamilyId(sessionData.familyId); setTier(sessionData.tier || "free"); setMemberCount(2); } catch (err) { @@ -87,7 +97,7 @@ export function FamilyProvider({ children: providerChildren }: { children: React } fetchFamilyData(); - }, []); + }, [router]); return ( { + if (!confirm("Are you sure you want to sign out?")) return; + setSigningOut(true); + try { + await fetch("/api/auth/signout", { method: "POST" }); + router.push("/login"); + } catch (err) { + console.error("Sign out failed:", err); + } + setSigningOut(false); + }; + const themeOptions = [ { value: "light", label: "Light" }, { value: "dark", label: "Dark" }, @@ -317,6 +329,15 @@ export default function SettingsPage() {
App Version
Tia v1.0.0
+ + {/* Sign Out */} + );