Closed ArthurOnTime closed 5 months ago
How are you initializing the MongoDB adapter?
import { MongodbAdapter } from '@lucia-auth/adapter-mongodb';
import User from '../mongodb/models/users.js'; // mongoose model import Session from '../mongodb/models/sessions.js'; // mongoose model
const adapter = new MongodbAdapter( Session, User );
export const lucia = new Lucia( adapter, { sessionCookie: { attributes: { secure: process.env.NODE_ENV === "production" } }, getUserAttributes: (attributes) => { return { discordId: attributes.discord_id, username: attributes.username, }; } });
What's Session
and User
?
Session
import mongodb from 'mongoose';
const Session = mongodb.models.Session || mongodb.model("Session", new mongodb.Schema( { _id: { type: String, required: true }, user_id: { type: mongodb.Schema.Types.ObjectId, ref: 'User', required: true }, expires_at: { type: Date, required: true } }, { _id: false } ));
export default Session;
User
import mongodb from 'mongoose';
const User = mongodb.models.User || mongodb.model("User", new mongodb.Schema( { _id: { type: mongodb.Schema.Types.ObjectId, required: true }, discordId: { type: String, required: false }, createdAtTimestamp: { type: Date, required: false }, username: { type: String, required: true }, globalName: { type: String, required: false }, discriminator: { type: String, required: false }, isBot: { type: Boolean, required: false }, isSystem: { type: Boolean, required: false }, avatar: { type: String, required: false }, banner: { type: String, required: false }, permissions: { type: [String], // This specifies that "roles" is an array of strings required: false }, roles: { type: [String], // This specifies that "roles" is an array of strings required: false }, }, { _id: false } ));
export default User;
This is expected since the adapter doesn't accept a Mongoose schema and instead accepts a MongoDB collection
Mongoose example: https://lucia-auth.com/database/mongoose
Indeed. I confirm everything works by passing a MongoDB collection. Thanks a lot.
Package
@lucia-auth/adapter-mongodb
Describe the bug
At the moment, using mongoose with the mongodb adapter doesn't work.
insertOne() doesn't exist in mongoose. mongoose uses create() instead.
Can be fixed by - line 58 - in setSession() : replacing
await this.Session.insertOne(value); // MUST be replaced for Mongoose
byawait this.Session.create(value); // WORKS for Mongoose
this.User.collectionName doesn't exist in mongoose. mongoose uses this.User.collection.name
Can be fixed by - line 20 - in getSessionAndUser() : replacing
from: this.User.collectionName,
byfrom: this.User.collection.name,
TypeError: cursor.toArray is not a function (I suppose it has to do with mongoose)
Can be fixed by - line 28 & 29 - in getSessionAndUser() : replacing
const sessionUsers = await cursor.toArray();
ANDconst sessionUser = sessionUsers?.at(0) ?? null;
byconst sessionUser = cursor?.at(0) ?? null;
PS: I love you work and I'm looking fwd to the fix so I can push to prod :)