From 63ee2732571a5f15fa1cc36f263522e604b76996 Mon Sep 17 00:00:00 2001 From: Kshetez Vinayak <100942053+auspy@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:30:24 +0530 Subject: [PATCH 1/5] Update mongodb.config.ts --- src/utils/mongodb.config.ts | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/utils/mongodb.config.ts b/src/utils/mongodb.config.ts index 30f0ae1..37eab82 100644 --- a/src/utils/mongodb.config.ts +++ b/src/utils/mongodb.config.ts @@ -1,4 +1,3 @@ -// This approach is taken from https://github.com/vercel/next.js/tree/canary/examples/with-mongodb import { MongoClient } from "mongodb"; if (!process.env.MONGO_URL) { @@ -6,28 +5,36 @@ if (!process.env.MONGO_URL) { } const uri = process.env.MONGO_URL; -const options = {}; +const options = { + useUnifiedTopology: true, + useNewUrlParser: true, + connectTimeoutMS: 30000, + socketTimeoutMS: 30000, + serverSelectionTimeoutMS: 30000, + heartbeatFrequencyMS: 10000, +}; -let client; -let mongoClient: Promise; +let client: MongoClient; +let clientPromise: Promise; if (process.env.NODE_ENV === "development") { - // In development mode, use a global variable so that the value let globalWithMongo = global as typeof globalThis & { _mongoClientPromise?: Promise; }; - // is preserved across module reloads caused by HMR (Hot Module Replacement). if (!globalWithMongo._mongoClientPromise) { client = new MongoClient(uri, options); - globalWithMongo._mongoClientPromise = client.connect(); + globalWithMongo._mongoClientPromise = client.connect().catch((err) => { + console.error("Failed to connect to MongoDB:", err); + throw err; + }); } - mongoClient = globalWithMongo._mongoClientPromise; + clientPromise = globalWithMongo._mongoClientPromise; } else { - // In production mode, it's best to not use a global variable. client = new MongoClient(uri, options); - mongoClient = client.connect(); + clientPromise = client.connect().catch((err) => { + console.error("Failed to connect to MongoDB:", err); + throw err; + }); } -// Export a module-scoped MongoClient promise. By doing this in a -// separate module, the client can be shared across functions. -export default mongoClient; +export default clientPromise; From 30de1a97cdfe90181eecc9334dd7ba1bece0ae92 Mon Sep 17 00:00:00 2001 From: Kshetez Vinayak <100942053+auspy@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:32:04 +0530 Subject: [PATCH 2/5] Update adapterFunctions.ts --- src/utils/auth/adapterFunctions.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/utils/auth/adapterFunctions.ts b/src/utils/auth/adapterFunctions.ts index e8b38a5..61fb03a 100644 --- a/src/utils/auth/adapterFunctions.ts +++ b/src/utils/auth/adapterFunctions.ts @@ -1,7 +1,15 @@ import { MongoDBAdapter } from "@auth/mongodb-adapter"; -import mongoClient from "../mongodb.config"; +import clientPromise from "../mongodb.config"; import { Adapter } from "next-auth/adapters"; -const adapter = MongoDBAdapter(mongoClient) as Adapter; +let adapter: Adapter; + +try { + adapter = MongoDBAdapter(clientPromise) as Adapter; +} catch (error) { + console.error("Failed to create MongoDB adapter:", error); + // Provide a fallback or throw an error based on your requirements + throw new Error("Failed to initialize authentication adapter"); +} export { adapter }; From add2e35df56b39b06a7b0abe4aa0e239913b2f47 Mon Sep 17 00:00:00 2001 From: Kshetez Vinayak <100942053+auspy@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:39:08 +0530 Subject: [PATCH 3/5] Update adapterFunctions.ts --- src/utils/auth/adapterFunctions.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/auth/adapterFunctions.ts b/src/utils/auth/adapterFunctions.ts index 61fb03a..6ea9f03 100644 --- a/src/utils/auth/adapterFunctions.ts +++ b/src/utils/auth/adapterFunctions.ts @@ -9,7 +9,6 @@ try { } catch (error) { console.error("Failed to create MongoDB adapter:", error); // Provide a fallback or throw an error based on your requirements - throw new Error("Failed to initialize authentication adapter"); } export { adapter }; From 5ad4ae618b6bac17ada8714748b48700594c1fe8 Mon Sep 17 00:00:00 2001 From: Kshetez Vinayak <100942053+auspy@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:54:53 +0530 Subject: [PATCH 4/5] Update mongodb.config.ts --- src/utils/mongodb.config.ts | 47 ++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/src/utils/mongodb.config.ts b/src/utils/mongodb.config.ts index 37eab82..32138a0 100644 --- a/src/utils/mongodb.config.ts +++ b/src/utils/mongodb.config.ts @@ -1,40 +1,33 @@ import { MongoClient } from "mongodb"; -if (!process.env.MONGO_URL) { - throw new Error('Invalid/Missing environment variable: "MONGO_URL"'); +if (!process.env.MONGODB_URI) { + throw new Error('Invalid/Missing environment variable: "MONGODB_URI"'); } -const uri = process.env.MONGO_URL; -const options = { - useUnifiedTopology: true, - useNewUrlParser: true, - connectTimeoutMS: 30000, - socketTimeoutMS: 30000, - serverSelectionTimeoutMS: 30000, - heartbeatFrequencyMS: 10000, -}; +const uri = process.env.MONGODB_URI; +const options = {}; -let client: MongoClient; -let clientPromise: Promise; +let client; +let clientPromise; -if (process.env.NODE_ENV === "development") { +function getClientPromise() { let globalWithMongo = global as typeof globalThis & { _mongoClientPromise?: Promise; }; - if (!globalWithMongo._mongoClientPromise) { + if (process.env.NODE_ENV === "development") { + // In development mode, use a global variable so that the value + // is preserved across module reloads caused by HMR (Hot Module Replacement). + if (!globalWithMongo._mongoClientPromise) { + client = new MongoClient(uri, options); + globalWithMongo._mongoClientPromise = client.connect(); + } + clientPromise = globalWithMongo._mongoClientPromise; + } else { client = new MongoClient(uri, options); - globalWithMongo._mongoClientPromise = client.connect().catch((err) => { - console.error("Failed to connect to MongoDB:", err); - throw err; - }); + clientPromise = client.connect(); } - clientPromise = globalWithMongo._mongoClientPromise; -} else { - client = new MongoClient(uri, options); - clientPromise = client.connect().catch((err) => { - console.error("Failed to connect to MongoDB:", err); - throw err; - }); + + return clientPromise; } -export default clientPromise; +export default getClientPromise; From b7eb5c73410045c8fe91effe9a6d657389b95ee4 Mon Sep 17 00:00:00 2001 From: Kshetez Vinayak <100942053+auspy@users.noreply.github.com> Date: Tue, 24 Sep 2024 12:56:30 +0530 Subject: [PATCH 5/5] Update adapterFunctions.ts --- src/utils/auth/adapterFunctions.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/utils/auth/adapterFunctions.ts b/src/utils/auth/adapterFunctions.ts index 6ea9f03..88b624e 100644 --- a/src/utils/auth/adapterFunctions.ts +++ b/src/utils/auth/adapterFunctions.ts @@ -1,11 +1,12 @@ import { MongoDBAdapter } from "@auth/mongodb-adapter"; -import clientPromise from "../mongodb.config"; +import getClientPromise from "../mongodb.config"; import { Adapter } from "next-auth/adapters"; let adapter: Adapter; try { - adapter = MongoDBAdapter(clientPromise) as Adapter; + const client = getClientPromise(); + adapter = MongoDBAdapter(client) as Adapter; } catch (error) { console.error("Failed to create MongoDB adapter:", error); // Provide a fallback or throw an error based on your requirements