Fix onboarding to use custom session auth
Was using next-auth which wasn't working with custom sessions. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
parent
340cf4322e
commit
e7a5de3cc2
1 changed files with 33 additions and 39 deletions
|
|
@ -1,63 +1,57 @@
|
|||
import { NextResponse } from "next/server";
|
||||
import { dbUnscoped as db } from "@/db";
|
||||
import { auth } from "@/auth";
|
||||
import { families, familyMembers, children } from "@/db/schema/family";
|
||||
import { users } from "@/db/schema/auth";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { sql } from "@/db";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
// For onboarding - auth via custom session (not next-auth)
|
||||
export async function POST(request: Request) {
|
||||
const session = await auth();
|
||||
const sessionToken = (await cookies()).get("tia_session")?.value;
|
||||
|
||||
if (!session?.user?.email) {
|
||||
if (!sessionToken) {
|
||||
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Get user from session
|
||||
const sessions = await sql`
|
||||
SELECT user_id FROM sessions WHERE session_token = ${sessionToken} AND expires > NOW()
|
||||
`;
|
||||
|
||||
if (!sessions || sessions.length === 0) {
|
||||
return NextResponse.json({ error: "Invalid session" }, { status: 401 });
|
||||
}
|
||||
|
||||
const userId = sessions[0].user_id;
|
||||
|
||||
const body = await request.json();
|
||||
const { familyName, memberName, childName, birthDate, sex } = body;
|
||||
|
||||
// Get or create user
|
||||
let user = await db.query.users.findFirst({
|
||||
where: eq(users.email, session.user.email),
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Create family
|
||||
const [family] = await db
|
||||
.insert(families)
|
||||
.values({
|
||||
name: familyName || "The Family",
|
||||
})
|
||||
.returning();
|
||||
const familyId = crypto.randomUUID();
|
||||
await sql`
|
||||
INSERT INTO families (id, name, tier, max_children, max_members, created_at, updated_at)
|
||||
VALUES (${familyId}, ${familyName || "The Family"}, 'free', 3, 4, NOW(), NOW())
|
||||
`;
|
||||
|
||||
// Add member
|
||||
await db.insert(familyMembers).values({
|
||||
familyId: family.id,
|
||||
userId: user.id,
|
||||
role: "admin",
|
||||
displayName: memberName,
|
||||
});
|
||||
await sql`
|
||||
INSERT INTO family_members (id, family_id, user_id, role, display_name, created_at)
|
||||
VALUES (${crypto.randomUUID()}, ${familyId}, ${userId}, 'admin', ${memberName}, NOW())
|
||||
`;
|
||||
|
||||
// Calculate stage
|
||||
// Create child
|
||||
const childId = crypto.randomUUID();
|
||||
const birth = new Date(birthDate);
|
||||
const months = Math.floor((Date.now() - birth.getTime()) / (1000 * 60 * 60 * 24 * 30));
|
||||
let stage: "newborn" | "infant" | "solids_start" | "toddler_early" | "toddler_late" | "preschool" = "newborn";
|
||||
let stage = "newborn";
|
||||
if (months >= 36) stage = "preschool";
|
||||
else if (months >= 24) stage = "toddler_late";
|
||||
else if (months >= 12) stage = "toddler_early";
|
||||
else if (months >= 6) stage = "solids_start";
|
||||
else if (months >= 3) stage = "infant";
|
||||
|
||||
// Create child
|
||||
await db.insert(children).values({
|
||||
familyId: family.id,
|
||||
name: childName,
|
||||
birthDate: birth,
|
||||
sex,
|
||||
currentStage: stage,
|
||||
});
|
||||
await sql`
|
||||
INSERT INTO children (id, family_id, name, birth_date, sex, current_stage, created_at)
|
||||
VALUES (${childId}, ${familyId}, ${childName}, ${birth.toISOString()}, ${sex}, ${stage}, NOW())
|
||||
`;
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
return NextResponse.json({ success: true, childId, familyId });
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue