Fix login flow to store and use real family_id
This commit is contained in:
parent
1d4acd9c2a
commit
1932d2ae6b
4 changed files with 66 additions and 32 deletions
|
|
@ -46,7 +46,11 @@ export function FamilyProvider({ children: providerChildren }: { children: React
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function fetchFamilyData() {
|
async function fetchFamilyData() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/api/children?familyId=default");
|
// Get family_id from localStorage (set during login)
|
||||||
|
const storedFamilyId = localStorage.getItem("family_id");
|
||||||
|
const familyIdToUse = storedFamilyId || "default";
|
||||||
|
|
||||||
|
const res = await fetch(`/api/children?familyId=${familyIdToUse}`);
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
if (data.children?.length > 0) {
|
if (data.children?.length > 0) {
|
||||||
|
|
@ -62,9 +66,16 @@ export function FamilyProvider({ children: providerChildren }: { children: React
|
||||||
setChildId(childList[0].id);
|
setChildId(childList[0].id);
|
||||||
}
|
}
|
||||||
|
|
||||||
setFamilyId("default");
|
setFamilyId(familyIdToUse);
|
||||||
setTier("free");
|
|
||||||
setMemberCount(2);
|
// Get tier and limits from family
|
||||||
|
const familyRes = await fetch(`/api/family?familyId=${familyIdToUse}`);
|
||||||
|
const familyData = await familyRes.json();
|
||||||
|
|
||||||
|
if (familyData.family) {
|
||||||
|
setTier(familyData.family.tier || "free");
|
||||||
|
setMemberCount(familyData.family.max_members || 2);
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to fetch family:", err);
|
console.error("Failed to fetch family:", err);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
@ -94,13 +105,4 @@ export function FamilyProvider({ children: providerChildren }: { children: React
|
||||||
{providerChildren}
|
{providerChildren}
|
||||||
</FamilyContext.Provider>
|
</FamilyContext.Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: Call useFamily() in any page to get:
|
|
||||||
// - familyId: The current family ID
|
|
||||||
// - childId: The selected child ID
|
|
||||||
// - child: The selected child object
|
|
||||||
// - children: List of all children
|
|
||||||
// - loading: Whether data is loading
|
|
||||||
// - tier: "free" or "pro"
|
|
||||||
// - memberCount: Number of family members
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { db } from "@/db";
|
import { sql } from "@/db";
|
||||||
import { users } from "@/db/schema/auth";
|
|
||||||
import { eq } from "drizzle-orm";
|
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const { email } = await request.json();
|
const { email } = await request.json();
|
||||||
|
|
@ -11,23 +9,44 @@ export async function POST(request: Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Find or create user
|
// Find user
|
||||||
let user = await db.query.users.findFirst({
|
const users = await sql`
|
||||||
where: eq(users.email, email),
|
SELECT u.id, u.email, fm.family_id as family_id
|
||||||
});
|
FROM users u
|
||||||
|
LEFT JOIN family_members fm ON fm.user_id = u.id
|
||||||
|
WHERE u.email = ${email}
|
||||||
|
LIMIT 1
|
||||||
|
`;
|
||||||
|
|
||||||
if (!user) {
|
if (!users || users.length === 0) {
|
||||||
const [newUser] = await db
|
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||||||
.insert(users)
|
|
||||||
.values({ email })
|
|
||||||
.returning();
|
|
||||||
user = newUser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// For demo, just return success - in real app, send magic link via email
|
const user = users[0];
|
||||||
return NextResponse.json({ success: true, userId: user.id });
|
const familyId = user.family_id;
|
||||||
|
|
||||||
|
// Get family info
|
||||||
|
let family = null;
|
||||||
|
if (familyId) {
|
||||||
|
const families = await sql`
|
||||||
|
SELECT id, name, tier, max_children, max_members
|
||||||
|
FROM families WHERE id = ${familyId}
|
||||||
|
`;
|
||||||
|
if (families.length > 0) {
|
||||||
|
family = families[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return user and family info
|
||||||
|
return NextResponse.json({
|
||||||
|
success: true,
|
||||||
|
userId: user.id,
|
||||||
|
email: user.email,
|
||||||
|
familyId: familyId,
|
||||||
|
family: family,
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error("Signin error:", error);
|
||||||
return NextResponse.json({ error: String(error) }, { status: 500 });
|
return NextResponse.json({ error: String(error) }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -19,8 +19,21 @@ export default function LoginPage() {
|
||||||
body: JSON.stringify({ email }),
|
body: JSON.stringify({ email }),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.ok) {
|
const data = await res.json();
|
||||||
|
|
||||||
|
if (data.success) {
|
||||||
|
// Store user and family info
|
||||||
|
localStorage.setItem("user_id", data.userId);
|
||||||
|
localStorage.setItem("user_email", data.email);
|
||||||
|
if (data.familyId) {
|
||||||
|
localStorage.setItem("family_id", data.familyId);
|
||||||
|
}
|
||||||
|
if (data.family) {
|
||||||
|
localStorage.setItem("family", JSON.stringify(data.family));
|
||||||
|
}
|
||||||
router.push("/");
|
router.push("/");
|
||||||
|
} else {
|
||||||
|
alert(data.error || "Sign in failed");
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|
@ -55,4 +68,4 @@ export default function LoginPage() {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue