Fix login flow: login stays on login, onboarding checks auth, homepage checks family

This commit is contained in:
Manohar Gupta 2026-05-11 00:06:23 +05:30
parent e0fdd432d3
commit c97add7376
4 changed files with 51 additions and 10 deletions

View file

@ -52,9 +52,8 @@ export function FamilyProvider({ children: providerChildren }: { children: React
const sessionRes = await fetch("/api/auth/signin"); const sessionRes = await fetch("/api/auth/signin");
const sessionData = await sessionRes.json(); const sessionData = await sessionRes.json();
// Not authenticated - redirect to login // Not authenticated - stay on current page, don't redirect
if (!sessionData.authenticated) { if (!sessionData.authenticated) {
router.push("/login");
setLoading(false); setLoading(false);
return; return;
} }
@ -66,8 +65,9 @@ export function FamilyProvider({ children: providerChildren }: { children: React
return; return;
} }
// Fetch children for this family // Authenticated with family - check for children
const res = await fetch(`/api/children?familyId=${sessionData.familyId}`); const familyId = sessionData.familyId;
const res = await fetch(`/api/children?familyId=${familyId}`);
const data = await res.json(); const data = await res.json();
if (data.children?.length > 0) { if (data.children?.length > 0) {
@ -84,9 +84,11 @@ export function FamilyProvider({ children: providerChildren }: { children: React
} else { } else {
// No children - go to onboarding // No children - go to onboarding
router.push("/onboarding"); router.push("/onboarding");
setLoading(false);
return;
} }
setFamilyId(sessionData.familyId); setFamilyId(familyId);
setTier(sessionData.tier || "free"); setTier(sessionData.tier || "free");
setMemberCount(2); setMemberCount(2);
} catch (err) { } catch (err) {

View file

@ -7,12 +7,16 @@ export default function LoginPage() {
const router = useRouter(); const router = useRouter();
useEffect(() => { useEffect(() => {
// Check if already logged in via session cookie
async function checkSession() { async function checkSession() {
const res = await fetch("/api/auth/signin"); const res = await fetch("/api/auth/signin");
const data = await res.json(); const data = await res.json();
if (data.authenticated) { if (data.authenticated) {
router.push("/"); if (data.familyId) {
router.push("/");
} else {
router.push("/onboarding");
}
} }
} }
checkSession(); checkSession();
@ -34,7 +38,11 @@ export default function LoginPage() {
const data = await res.json(); const data = await res.json();
if (data.success) { if (data.success) {
router.push("/"); if (data.familyId) {
router.push("/");
} else {
router.push("/onboarding");
}
} else { } else {
alert(data.error || "Sign in failed"); alert(data.error || "Sign in failed");
} }

View file

@ -1,12 +1,13 @@
"use client"; "use client";
import { useState } from "react"; import { useState, useEffect } from "react";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
export default function OnboardingPage() { export default function OnboardingPage() {
const router = useRouter(); const router = useRouter();
const [step, setStep] = useState(1); const [step, setStep] = useState(1);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [checkingAuth, setCheckingAuth] = useState(true);
const [form, setForm] = useState({ const [form, setForm] = useState({
familyName: "", familyName: "",
memberName: "", memberName: "",
@ -15,6 +16,20 @@ export default function OnboardingPage() {
sex: "" as "male" | "female" | "other", sex: "" as "male" | "female" | "other",
}); });
useEffect(() => {
async function checkAuth() {
const res = await fetch("/api/auth/signin");
const data = await res.json();
if (!data.authenticated) {
router.push("/login");
} else if (data.familyId) {
router.push("/");
}
setCheckingAuth(false);
}
checkAuth();
}, [router]);
const handleSubmit = async () => { const handleSubmit = async () => {
setLoading(true); setLoading(true);
try { try {
@ -32,6 +47,14 @@ export default function OnboardingPage() {
setLoading(false); setLoading(false);
}; };
if (checkingAuth) {
return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-rose-50 to-amber-50">
<div className="text-gray-500">Loading...</div>
</div>
);
}
return ( return (
<div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-rose-50 to-amber-50 p-4"> <div className="min-h-screen flex items-center justify-center bg-gradient-to-br from-rose-50 to-amber-50 p-4">
<div className="w-full max-w-md"> <div className="w-full max-w-md">

View file

@ -137,12 +137,20 @@ export default function HomePage() {
const [pendingCount, setPendingCount] = useState(0); const [pendingCount, setPendingCount] = useState(0);
const [lastLogs, setLastLogs] = useState<any[]>([]); const [lastLogs, setLastLogs] = useState<any[]>([]);
const { theme, toggle: toggleTheme } = useTheme(); const { theme, toggle: toggleTheme } = useTheme();
const { childId, child, loading } = useFamily(); const { childId, child, familyId, loading } = useFamily();
if (loading) { if (loading) {
return <div className="min-h-screen flex items-center justify-center">Loading...</div>; return <div className="min-h-screen flex items-center justify-center">Loading...</div>;
} }
// Not logged in - redirect to login
if (!familyId) {
if (typeof window !== "undefined") {
window.location.href = "/login";
}
return <div className="min-h-screen flex items-center justify-center">Redirecting...</div>;
}
if (!childId) { if (!childId) {
return <div className="min-h-screen flex items-center justify-center">No child found. Add a child in Family settings.</div>; return <div className="min-h-screen flex items-center justify-center">No child found. Add a child in Family settings.</div>;
} }