From c4600ba68c988b47ae77dc2d313c0fb33d8bc7bd Mon Sep 17 00:00:00 2001 From: Ahmed Darrazi Date: Sat, 6 Dec 2025 12:29:00 +0100 Subject: [PATCH] Fix tenant_id not being saved to database on login --- debug-db.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/auth/utils.ts | 14 ++++++++++++++ package.json | 1 + 3 files changed, 57 insertions(+) create mode 100644 debug-db.ts diff --git a/debug-db.ts b/debug-db.ts new file mode 100644 index 0000000..c9a49b4 --- /dev/null +++ b/debug-db.ts @@ -0,0 +1,42 @@ +import { Client } from 'pg'; + +const client = new Client({ + connectionString: 'postgresql://postgres:JsdPCZiC1C56Sz@localhost:5433/postgres', + ssl: false +}); + +async function test() { + try { + console.log('Connecting to database...'); + await client.connect(); + console.log('Connected successfully!'); + + console.log('Listing tables in public schema...'); + const res = await client.query(` + SELECT table_name + FROM information_schema.tables + WHERE table_schema = 'public' + ORDER BY table_name; + `); + + if (res.rows.length === 0) { + console.log('No tables found in public schema!'); + } else { + console.log('Tables found:'); + res.rows.forEach(row => console.log(`- ${row.table_name}`)); + } + + // Check user count if user table exists + if (res.rows.find(r => r.table_name === 'user')) { + const userCount = await client.query('SELECT count(*) FROM "user"'); + console.log(`User count: ${userCount.rows[0].count}`); + } + + } catch (err) { + console.error('Connection error:', err); + } finally { + await client.end(); + } +} + +test(); diff --git a/lib/auth/utils.ts b/lib/auth/utils.ts index fdd7931..8bf07dc 100644 --- a/lib/auth/utils.ts +++ b/lib/auth/utils.ts @@ -1,9 +1,11 @@ import { db } from "@/lib/db/index"; +import { users } from "@/lib/db/schema/auth"; import { DrizzleAdapter } from "@auth/drizzle-adapter"; import { getServerSession } from "next-auth/next"; import { type Adapter } from "next-auth/adapters"; import { redirect } from "next/navigation"; import { z } from "zod"; +import { eq } from "drizzle-orm"; import AzureADProvider from "next-auth/providers/azure-ad"; export type AuthSession = { @@ -38,6 +40,18 @@ export const authOptions = { // Extract tenantId from Azure AD tid claim if (profile && 'tid' in profile) { token.tenantId = profile.tid as string; + + // Update tenant_id in database if we have a user id + if (token.sub) { + try { + await db + .update(users) + .set({ tenantId: profile.tid as string }) + .where(eq(users.id, token.sub)); + } catch (error) { + console.error('Failed to update tenant_id:', error); + } + } } return token; }, diff --git a/package.json b/package.json index ed7ebd2..60e50be 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "db:pull": "drizzle-kit introspect", "db:push": "drizzle-kit push", "db:studio": "drizzle-kit studio", + "db:studio:prod": "lsof -ti:5433 | xargs kill -9 2>/dev/null; ssh cloudarix \"docker rm -f db-proxy 2>/dev/null; IP=\\$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' tenantpilot-db-tav83h.1.8ijze7mxpcg69pvdlu0g4j4et); docker run --rm -d --name db-proxy --network dokploy-network -p 127.0.0.1:5433:5432 alpine/socat TCP-LISTEN:5432,fork TCP:\\$IP:5432\"; ssh -L 5433:127.0.0.1:5433 cloudarix -N & sleep 3 && DATABASE_URL='postgresql://postgres:JsdPCZiC1C56Sz@localhost:5433/postgres' drizzle-kit studio", "db:check": "drizzle-kit check", "stripe:listen": "stripe listen --forward-to localhost:3000/api/webhooks/stripe" },