lucia-auth / lucia

Authentication, simple and clean
https://lucia-auth.com
MIT License
8.96k stars 460 forks source link

[Bug]: TypeError: this.Session.aggregate(...).toArray is not a function at MongodbAdapter.getSessionAndUser #1381

Closed donsn closed 7 months ago

donsn commented 7 months ago

Package

​@lucia-auth/adapter-mongodb

Describe the bug

The default integration with Lucia v3 is broken when you try to connect using MongoDB and Mongoose

Migrated fully as explained on the documentation, this doesn't work at the moment

It breaks at getSessionAndUser when you try to validate sessions

pilcrowOnPaper commented 7 months ago

How are you initializing the adapter? The MongoDB adapter has been tested against both mongodb and mongoose so I doubt it's a bug

donsn commented 7 months ago

image @pilcrowOnPaper then I use the database connection anywhere it's needed

pilcrowOnPaper commented 7 months ago

Like what are you passing to the adapter?

donsn commented 7 months ago

Passing the mongoose collection like this

image

pilcrowOnPaper commented 7 months ago

What version of mongoose are you using?

donsn commented 7 months ago

Here's the version I'm using

"mongodb": "^6.3.0", "mongoose": "^8.1.0",

pilcrowOnPaper commented 7 months ago

Might be a v8 issue, will test it out

pilcrowOnPaper commented 7 months ago

I can't see to recreate the issue. Tested with MongoDB v6 and Mongoose v8. I'd need a basic reproduction.

donsn commented 7 months ago

I can't see to recreate the issue. Tested with MongoDB v6 and Mongoose v8. I'd need a basic reproduction.

@pilcrowOnPaper This happens when you call validateSession with the SessionID

pilcrowOnPaper commented 7 months ago

We have tests for all adapter methods, including getSessionAndUser(), and I'm not getting any error in both Mongoose v6 and v8.

cdy-peters commented 7 months ago

I can't see to recreate the issue. Tested with MongoDB v6 and Mongoose v8. I'd need a basic reproduction.

I'm able to reproduce this with the same package versions and with an example. It seems to happen on first load (with a session cookie set) after restarting the app and clears after reloading the page. I haven't come across this error in any other scenarios.

pilcrowOnPaper commented 7 months ago

Blank project and this works fine:

import mongoose from "mongoose";

await mongoose.connect(url);

const result = await mongoose.connection.collection("users").aggregate().toArray();
console.log(result);
donsn commented 7 months ago

I can't see to recreate the issue. Tested with MongoDB v6 and Mongoose v8. I'd need a basic reproduction.

I'm able to reproduce this with the same package versions and with an example. It seems to happen on first load (with a session cookie set) after restarting the app and clears after reloading the page. I haven't come across this error in any other scenarios.

Yes. It happens with a session cookie set

I'm trying to create an isolated scenario to find the root cause

pilcrowOnPaper commented 7 months ago

Can't reproduce it:

import { Lucia, generateId } from "lucia";
import { MongodbAdapter } from "@lucia-auth/adapter-mongodb";
import mongoose from "mongoose";

const adapter = new MongodbAdapter(
  mongoose.connection.collection("sessions"),
  mongoose.connection.collection("users")
);

const lucia = new Lucia(adapter);

await mongoose.connect(process.env.MONGODB_URL);

const userId = generateId(15)
await mongoose.connection.collection("users").insertOne({
    _id: userId as any,
    username: "pilcrow"
})

const session = await lucia.createSession(userId, {});
const validatedSession = await lucia.validateSession(session.id);
pilcrowOnPaper commented 7 months ago

What framework are you using?

cdy-peters commented 7 months ago

I found my issue... I wasn't awaiting the connection. I think I removed it as it was throwing an ES target error that I couldn't resolve. Didn't realise with Nuxt you have to define the ES target for the Nitro server separately, solution.

donsn commented 7 months ago

@pilcrowOnPaper I did a sample here: https://github.com/donsn/lucia-auth-test

pilcrowOnPaper commented 7 months ago

Yeah probably an issue with Mongoose. That said, I can't seem to recreate the issue in a simple project. I can't even use createConnection() for some reason

donsn commented 7 months ago

Yeah probably an issue with Mongoose. That said, I can't seem to recreate the issue in a simple project. I can't even use createConnection() for some reason

Thank you