Fix login flow: login stays on login, onboarding checks auth, homepage checks family
This commit is contained in:
parent
e0fdd432d3
commit
c97add7376
4 changed files with 51 additions and 10 deletions
|
|
@ -52,9 +52,8 @@ export function FamilyProvider({ children: providerChildren }: { children: React
|
|||
const sessionRes = await fetch("/api/auth/signin");
|
||||
const sessionData = await sessionRes.json();
|
||||
|
||||
// Not authenticated - redirect to login
|
||||
// Not authenticated - stay on current page, don't redirect
|
||||
if (!sessionData.authenticated) {
|
||||
router.push("/login");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
|
@ -66,8 +65,9 @@ export function FamilyProvider({ children: providerChildren }: { children: React
|
|||
return;
|
||||
}
|
||||
|
||||
// Fetch children for this family
|
||||
const res = await fetch(`/api/children?familyId=${sessionData.familyId}`);
|
||||
// Authenticated with family - check for children
|
||||
const familyId = sessionData.familyId;
|
||||
const res = await fetch(`/api/children?familyId=${familyId}`);
|
||||
const data = await res.json();
|
||||
|
||||
if (data.children?.length > 0) {
|
||||
|
|
@ -84,9 +84,11 @@ export function FamilyProvider({ children: providerChildren }: { children: React
|
|||
} else {
|
||||
// No children - go to onboarding
|
||||
router.push("/onboarding");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setFamilyId(sessionData.familyId);
|
||||
setFamilyId(familyId);
|
||||
setTier(sessionData.tier || "free");
|
||||
setMemberCount(2);
|
||||
} catch (err) {
|
||||
|
|
|
|||
|
|
@ -7,12 +7,16 @@ export default function LoginPage() {
|
|||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
// Check if already logged in via session cookie
|
||||
async function checkSession() {
|
||||
const res = await fetch("/api/auth/signin");
|
||||
const data = await res.json();
|
||||
|
||||
if (data.authenticated) {
|
||||
router.push("/");
|
||||
if (data.familyId) {
|
||||
router.push("/");
|
||||
} else {
|
||||
router.push("/onboarding");
|
||||
}
|
||||
}
|
||||
}
|
||||
checkSession();
|
||||
|
|
@ -34,7 +38,11 @@ export default function LoginPage() {
|
|||
const data = await res.json();
|
||||
|
||||
if (data.success) {
|
||||
router.push("/");
|
||||
if (data.familyId) {
|
||||
router.push("/");
|
||||
} else {
|
||||
router.push("/onboarding");
|
||||
}
|
||||
} else {
|
||||
alert(data.error || "Sign in failed");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
|
||||
export default function OnboardingPage() {
|
||||
const router = useRouter();
|
||||
const [step, setStep] = useState(1);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [checkingAuth, setCheckingAuth] = useState(true);
|
||||
const [form, setForm] = useState({
|
||||
familyName: "",
|
||||
memberName: "",
|
||||
|
|
@ -15,6 +16,20 @@ export default function OnboardingPage() {
|
|||
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 () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
|
|
@ -32,6 +47,14 @@ export default function OnboardingPage() {
|
|||
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 (
|
||||
<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">
|
||||
|
|
|
|||
|
|
@ -137,12 +137,20 @@ export default function HomePage() {
|
|||
const [pendingCount, setPendingCount] = useState(0);
|
||||
const [lastLogs, setLastLogs] = useState<any[]>([]);
|
||||
const { theme, toggle: toggleTheme } = useTheme();
|
||||
const { childId, child, loading } = useFamily();
|
||||
const { childId, child, familyId, loading } = useFamily();
|
||||
|
||||
if (loading) {
|
||||
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) {
|
||||
return <div className="min-h-screen flex items-center justify-center">No child found. Add a child in Family settings.</div>;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue