Remove signIn callback — enrollment now happens in getCurrentSession
Release / build-and-push (push) Has been cancelled
Release / build-and-push (push) Has been cancelled
Auth.js v5 calls signIn before the new user's row is fully committed, so inserting into household_members immediately caused a FK violation for any new user (e.g. cam on first sign-in). Enrollment is now handled lazily in getCurrentSession's auto-enroll fallback, which runs after the session is established and the user row is guaranteed to exist. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
2129f9718f
commit
e130cda6c5
@@ -3,15 +3,10 @@ import NextAuth, { type DefaultSession } from "next-auth";
|
|||||||
import { db } from "@/lib/db";
|
import { db } from "@/lib/db";
|
||||||
import {
|
import {
|
||||||
accounts,
|
accounts,
|
||||||
households,
|
|
||||||
householdMembers,
|
|
||||||
sessions,
|
sessions,
|
||||||
users,
|
users,
|
||||||
verificationTokens,
|
verificationTokens,
|
||||||
} from "@/modules/_core/schema";
|
} from "@/modules/_core/schema";
|
||||||
import { eq } from "drizzle-orm";
|
|
||||||
import { ensureDefaultCalendarsForMembership } from "@/modules/calendar/server/defaults";
|
|
||||||
import { ensureDefaultListsForHousehold } from "@/modules/lists/server/defaults";
|
|
||||||
|
|
||||||
declare module "next-auth" {
|
declare module "next-auth" {
|
||||||
interface Session {
|
interface Session {
|
||||||
@@ -41,35 +36,6 @@ export const { handlers, auth, signIn, signOut } = NextAuth({
|
|||||||
signIn: "/login",
|
signIn: "/login",
|
||||||
},
|
},
|
||||||
callbacks: {
|
callbacks: {
|
||||||
async signIn({ user }) {
|
|
||||||
if (!user.id) return true;
|
|
||||||
const existing = await db
|
|
||||||
.select()
|
|
||||||
.from(householdMembers)
|
|
||||||
.where(eq(householdMembers.userId, user.id))
|
|
||||||
.limit(1);
|
|
||||||
if (existing.length === 0) {
|
|
||||||
const [household] = await db.select().from(households).limit(1);
|
|
||||||
if (household) {
|
|
||||||
const [anyMember] = await db
|
|
||||||
.select()
|
|
||||||
.from(householdMembers)
|
|
||||||
.where(eq(householdMembers.householdId, household.id))
|
|
||||||
.limit(1);
|
|
||||||
const role = anyMember ? "member" : "owner";
|
|
||||||
await db
|
|
||||||
.insert(householdMembers)
|
|
||||||
.values({ householdId: household.id, userId: user.id, role })
|
|
||||||
.onConflictDoNothing();
|
|
||||||
await ensureDefaultCalendarsForMembership({
|
|
||||||
householdId: household.id,
|
|
||||||
userId: user.id,
|
|
||||||
});
|
|
||||||
await ensureDefaultListsForHousehold(household.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
},
|
|
||||||
session({ session, user }) {
|
session({ session, user }) {
|
||||||
session.user.id = user.id;
|
session.user.id = user.id;
|
||||||
return session;
|
return session;
|
||||||
|
|||||||
Reference in New Issue
Block a user