Fix tenant_id not being saved to database on login
All checks were successful
Trigger Cloudarix Deploy / call-webhook (push) Successful in 1s
All checks were successful
Trigger Cloudarix Deploy / call-webhook (push) Successful in 1s
This commit is contained in:
parent
e0a746b29c
commit
c4600ba68c
42
debug-db.ts
Normal file
42
debug-db.ts
Normal file
@ -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();
|
||||||
@ -1,9 +1,11 @@
|
|||||||
import { db } from "@/lib/db/index";
|
import { db } from "@/lib/db/index";
|
||||||
|
import { users } from "@/lib/db/schema/auth";
|
||||||
import { DrizzleAdapter } from "@auth/drizzle-adapter";
|
import { DrizzleAdapter } from "@auth/drizzle-adapter";
|
||||||
import { getServerSession } from "next-auth/next";
|
import { getServerSession } from "next-auth/next";
|
||||||
import { type Adapter } from "next-auth/adapters";
|
import { type Adapter } from "next-auth/adapters";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
import { eq } from "drizzle-orm";
|
||||||
import AzureADProvider from "next-auth/providers/azure-ad";
|
import AzureADProvider from "next-auth/providers/azure-ad";
|
||||||
|
|
||||||
export type AuthSession = {
|
export type AuthSession = {
|
||||||
@ -38,6 +40,18 @@ export const authOptions = {
|
|||||||
// Extract tenantId from Azure AD tid claim
|
// Extract tenantId from Azure AD tid claim
|
||||||
if (profile && 'tid' in profile) {
|
if (profile && 'tid' in profile) {
|
||||||
token.tenantId = profile.tid as string;
|
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;
|
return token;
|
||||||
},
|
},
|
||||||
|
|||||||
@ -13,6 +13,7 @@
|
|||||||
"db:pull": "drizzle-kit introspect",
|
"db:pull": "drizzle-kit introspect",
|
||||||
"db:push": "drizzle-kit push",
|
"db:push": "drizzle-kit push",
|
||||||
"db:studio": "drizzle-kit studio",
|
"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",
|
"db:check": "drizzle-kit check",
|
||||||
"stripe:listen": "stripe listen --forward-to localhost:3000/api/webhooks/stripe"
|
"stripe:listen": "stripe listen --forward-to localhost:3000/api/webhooks/stripe"
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user