lucia-auth / lucia

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

[Bug]: validateSession fails in V3 w. Prisma adapter if user model is not named like the attribute referencing it #1411

Closed ptrxyz closed 7 months ago

ptrxyz commented 8 months ago

Package

​@lucia-auth/adapter-prisma

Describe the bug

From the Prisma adapter, getSessionAndUser:

const userModelKey = this.userModel.name[0].toLowerCase() + this.userModel.name.slice(1);
const result = await this.sessionModel.findUnique({
    where: {
        id: sessionId
    },
    include: {
        [userModelKey]: true
    }
});

To determine the name of the field referencing the foreign key of the user in the user model, userModelKey is used. However userModelKey is simply based on the name of the model, the field name has nothing to do with the model name. This causes an error.

ptrxyz commented 8 months ago

A possible workaround would be to make this convention:

The session attribute referencing the user model should always be the pascalCase version of the user model it references.

I guess this needs to be added to the docs then. Alternatively one could consider adding an optional parameter to the Prisma adapter's constructor.

lowvisiondave commented 8 months ago

I came across this today. The code you reference was not the issue in my case, but rather the relationship field userId is what needed to be the same as the documentation. I was able to use a new model names as long as the relation was named userId.

pilcrowOnPaper commented 7 months ago

Yeah unfortunately this is a limitation of the adapter and I'd rather not add an additional config just for it. I've updated the docs though to prevent future issues

https://github.com/lucia-auth/lucia/pull/1438