From b84dba8df1cf51d7a83ede17f3d7ddccfffa5198 Mon Sep 17 00:00:00 2001 From: renatodellosso Date: Mon, 24 Feb 2025 16:49:35 -0500 Subject: [PATCH 1/4] Don't use cache in sign in flow --- lib/Auth.ts | 13 ++++++------- lib/MongoDB.ts | 14 +++++++++----- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/Auth.ts b/lib/Auth.ts index 4900d320..d7956ada 100644 --- a/lib/Auth.ts +++ b/lib/Auth.ts @@ -14,8 +14,6 @@ import CollectionId from "./client/CollectionId"; import { AdapterUser } from "next-auth/adapters"; import { wait } from "./client/ClientUtils"; -const db = getDatabase(); - const adapter = MongoDBAdapter(clientPromise, { databaseName: process.env.DB }); export const AuthenticationOptions: AuthOptions = { @@ -92,7 +90,7 @@ export const AuthenticationOptions: AuthOptions = { callbacks: { async session({ session, user }) { session.user = await ( - await db + await getDatabase() ).findObjectById(CollectionId.Users, new ObjectId(user.id)); return session; @@ -107,6 +105,7 @@ export const AuthenticationOptions: AuthOptions = { */ async signIn({ user }) { Analytics.signIn(user.name ?? "Unknown User"); + const db = await getDatabase(false); let typedUser = user as Partial; if (!typedUser.slug || typedUser._id?.toString() != typedUser.id) { @@ -116,9 +115,9 @@ export const AuthenticationOptions: AuthOptions = { ); let foundUser: User | undefined = undefined; while (!foundUser) { - foundUser = await ( - await db - ).findObject(CollectionId.Users, { email: typedUser.email }); + foundUser = await db.findObject(CollectionId.Users, { + email: typedUser.email, + }); if (!foundUser) await wait(50); } @@ -128,7 +127,7 @@ export const AuthenticationOptions: AuthOptions = { typedUser._id = foundUser._id; typedUser.lastSignInDateTime = new Date(); - typedUser = await repairUser(await db, typedUser); + typedUser = await repairUser(db, typedUser); console.log("User updated:", typedUser._id?.toString()); }; diff --git a/lib/MongoDB.ts b/lib/MongoDB.ts index f7343f11..f79f6362 100644 --- a/lib/MongoDB.ts +++ b/lib/MongoDB.ts @@ -30,13 +30,17 @@ clientPromise = global.clientPromise; export { clientPromise }; -export async function getDatabase(): Promise { +export async function getDatabase( + useCache: boolean = true, +): Promise { if (!global.interface) { await clientPromise; - const dbInterface = new CachedDbInterface( - new MongoDBInterface(clientPromise), - cacheOptions, - ); + + const mongo = new MongoDBInterface(clientPromise); + + const dbInterface = useCache + ? new CachedDbInterface(mongo, cacheOptions) + : mongo; await dbInterface.init(); global.interface = dbInterface; From 790097afb63e767044ccf666c41e5da702820451 Mon Sep 17 00:00:00 2001 From: renatodellosso Date: Mon, 24 Feb 2025 16:52:59 -0500 Subject: [PATCH 2/4] Reuse db in auth flow --- lib/Auth.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Auth.ts b/lib/Auth.ts index d7956ada..c08923cf 100644 --- a/lib/Auth.ts +++ b/lib/Auth.ts @@ -141,15 +141,13 @@ export const AuthenticationOptions: AuthOptions = { today.toDateString() ) { // We use user.id since user._id strangely doesn't exist on user. - await getDatabase().then((db) => db.updateObjectById( CollectionId.Users, new ObjectId(typedUser._id?.toString()), { lastSignInDateTime: today, }, - ), - ); + ); } new ResendUtils().createContact(typedUser as User); From b78ba521b3a5d7565e6c77c09f1dcf28a599e9df Mon Sep 17 00:00:00 2001 From: renatodellosso Date: Mon, 24 Feb 2025 16:54:48 -0500 Subject: [PATCH 3/4] Reduce getDatabase calls in Auth --- lib/Auth.ts | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/Auth.ts b/lib/Auth.ts index c08923cf..bee2f0b0 100644 --- a/lib/Auth.ts +++ b/lib/Auth.ts @@ -16,6 +16,8 @@ import { wait } from "./client/ClientUtils"; const adapter = MongoDBAdapter(clientPromise, { databaseName: process.env.DB }); +const cachedDb = getDatabase(); + export const AuthenticationOptions: AuthOptions = { secret: process.env.NEXTAUTH_SECRET, providers: [ @@ -28,11 +30,7 @@ export const AuthenticationOptions: AuthOptions = { profile.email, profile.picture, false, - await GenerateSlug( - await getDatabase(), - CollectionId.Users, - profile.name, - ), + await GenerateSlug(await cachedDb, CollectionId.Users, profile.name), [], [], ); @@ -60,11 +58,7 @@ export const AuthenticationOptions: AuthOptions = { profile.email, profile.picture, false, - await GenerateSlug( - await getDatabase(), - CollectionId.Users, - profile.name, - ), + await GenerateSlug(await cachedDb, CollectionId.Users, profile.name), [], [], profile.sub, @@ -90,7 +84,7 @@ export const AuthenticationOptions: AuthOptions = { callbacks: { async session({ session, user }) { session.user = await ( - await getDatabase() + await cachedDb ).findObjectById(CollectionId.Users, new ObjectId(user.id)); return session; @@ -141,13 +135,13 @@ export const AuthenticationOptions: AuthOptions = { today.toDateString() ) { // We use user.id since user._id strangely doesn't exist on user. - db.updateObjectById( - CollectionId.Users, - new ObjectId(typedUser._id?.toString()), - { - lastSignInDateTime: today, - }, - ); + db.updateObjectById( + CollectionId.Users, + new ObjectId(typedUser._id?.toString()), + { + lastSignInDateTime: today, + }, + ); } new ResendUtils().createContact(typedUser as User); From 7b9d17f51b4b765027b28fbe7678d907ce09ccdd Mon Sep 17 00:00:00 2001 From: renatodellosso Date: Mon, 24 Feb 2025 16:56:14 -0500 Subject: [PATCH 4/4] Add log for sign in callback --- lib/Auth.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Auth.ts b/lib/Auth.ts index bee2f0b0..4967d175 100644 --- a/lib/Auth.ts +++ b/lib/Auth.ts @@ -98,6 +98,8 @@ export const AuthenticationOptions: AuthOptions = { * For email sign in, runs when the "Sign In" button is clicked (before email is sent). */ async signIn({ user }) { + console.log(`User is signing in: ${user.name}, ${user.email}, ${user.id}`); + Analytics.signIn(user.name ?? "Unknown User"); const db = await getDatabase(false);