Replies: 8 comments 5 replies
-
Nice work! We are absolutely open for it. 😉. But I think we could add it inside the mongodb adapter. I assume people would search for it there. (I recall this has happened before already) We can either expose it as a submodule under cc @ndom91 |
Beta Was this translation helpful? Give feedback.
-
I would love to have a "correct" way to implement Mongoose with Next-auth. I can't find the previous discussion but if I recall correctly previous attempts at this required you to open two Mongo connections each time, one for the next-auth mongo connection and one for your Mongoose connection. It also had the issue of not utilizing the Mongoose schema for auth so any default fields or requirements you setup for your User schema were not respected. Which basically made using Mongoose over the normal Mongo adapter useless in terms of auth. |
Beta Was this translation helpful? Give feedback.
-
Any advice on how to implement this? |
Beta Was this translation helpful? Give feedback.
-
Thats exactly what i was looking for. most of people use mongoose in their apps to make their life easier |
Beta Was this translation helpful? Give feedback.
-
Hey, is it implemented on next-auth?? if not how can I use it? |
Beta Was this translation helpful? Give feedback.
-
For all who's wondering on a way to do it, here's a comment: #3542 (comment) - in short, you make two connections and it works all right |
Beta Was this translation helpful? Give feedback.
-
still not resolved? |
Beta Was this translation helpful? Give feedback.
-
For anyone interested, I have found a working solution. Here you have your db connection file, like utils/db.js import { MongoClient, ServerApiVersion } from "mongodb";
import mongoose from "mongoose";
if (!process.env.MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}
const uri = process.env.MONGODB_URI;
const options = {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
};
let client;
let clientPromise;
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).
let globalWithMongo = global;
if (!globalWithMongo._mongoClient) {
// globalWithMongo._mongoClient = new MongoClient(uri, options);
globalWithMongo._mongoClient = new MongoClient(uri, options);
}
client = globalWithMongo._mongoClient;
// client.connect();
} else {
// In production mode, it's best to not use a global variable.
client = new MongoClient(uri, options);
// clientPromise = client.connect();
}
client.connect().then((c) => {
mongoose.createConnection().setClient(c);
});
// Add client to mongoose
// Export a module-scoped MongoClient. By doing this in a
// separate module, the client can be shared across functions.
export default client; Then you simply pass the client to the MongoDB Adaptater: import NextAuth from "next-auth";
// import AppleProvider from "next-auth/providers/apple";
// import FacebookProvider from "next-auth/providers/facebook";
import GoogleProvider from "next-auth/providers/google";
import { MongoDBAdapter } from "@auth/mongodb-adapter";
import client from "../../../utils/db";
// import EmailProvider from "next-auth/providers/email";
export const authOptions = {
adapter: MongoDBAdapter(client),
providers: [
// OAuth authentication providers...
// AppleProvider({
// clientId: process.env.APPLE_ID,
// clientSecret: process.env.APPLE_SECRET,
// }),
// FacebookProvider({
// clientId: process.env.FACEBOOK_ID,
// clientSecret: process.env.FACEBOOK_SECRET,
// }),
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
// Passwordless / email sign in
// EmailProvider({
// server: process.env.MAIL_SERVER,
// from: "NextAuth.js <no-reply@example.com>",
// }),
],
callbacks: {
session: async ({ session, user }) => {
console.log("user from callback", user);
if (session?.user) {
session.user.id = user.id;
}
return session;
},
},
};
export default NextAuth(authOptions); You can then use mongoose in your app without issues, and you have only created a single connection. |
Beta Was this translation helpful? Give feedback.
-
@balazsorban44 I am currently creating a custom adapter for Mongoose and was curious if Next-auth would be interested in adding it to their list of approved adapters. Didn't want to waste time if there was no interest.
Created public repo to check out the code -> https://github.com/msoyka/next-auth-mongoose-adapter.
Still needs refinement as I am still verifying all the scenarios work, but wanted to get it in front of the team in case they have thoughts to make it better as well.
Thanks!
Matt
Beta Was this translation helpful? Give feedback.
All reactions