Next.js Discord

Discord Forum

MongoDB adapter with Nextjs

Unanswered
ashcode posted this in #help-forum
Open in Discord
I am using JS, and want to add adapter in my nextjs app,

I wanted a few steps that I can follow to add the adapter, as there are not a lot of resources in JS.
I tried, but also encountering this error
/node_modules/mongodb/lib/client-side-encryption/mongocryptd_manager.js:34:24
Module not found: Can't resolve 'child_process'

my code, (where do we add the dbname configuration??);
import { MongoClient } from "mongodb";

if (!process.env.DB_URI) {
  throw new Error('Invalid/Missing environment variable: "DB_URI"');
}
const uri = process.env.DB_URI;
const options = {};
let client;
let clientPromise;
if (process.env.NODE_ENV === "development") {
  if (!global._mongoClientPromise) {
    client = new MongoClient(uri, options);
    global._mongoClientPromise = client.connect();
  }
  clientPromise = global._mongoClientPromise;
} else {
  client = new MongoClient(uri, options);
  clientPromise = client.connect();
}
export default clientPromise;

my authOption file;
import { ConnectDB } from "../../../../lib/ConnectDB";
import NextAuth from "next-auth/next";
import { MongoDBAdapter } from "@auth/mongodb-adapter"
import clientPromise from "../../../../lib/adapter";

export const authOptions = {
  adapter: MongoDBAdapter(clientPromise),
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_SECRET_KEY,
    }),
  ],
  secret: process.env.NEXTAUTH_SECRET,
  session:{
    strategy:'jwt',
  },
  callbacks: {
    async signIn({ user, profile }) {
      try {
        // await ConnectDB();
        const userExits = await User.findOne({ email: profile.email });
        if (!userExits) {
          const res = await axios
            .post(`${process.env.NEXTAUTH_URL}/api/user`, {
              ....
            });
        }
      } catch (err) {
        console.log(err);
      }
      return user;
    }
  },
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };

0 Replies