diff --git a/docs/pages/database/prisma.md b/docs/pages/database/prisma.md index e80516192..c3b581827 100644 --- a/docs/pages/database/prisma.md +++ b/docs/pages/database/prisma.md @@ -12,6 +12,8 @@ npm install @lucia-auth/adapter-prisma ## Schema +While Lucia does not enforce model names, the relation name (`user`) in the session model must be the camel-case version of the user model name. For example, if the user model was named `AuthUser`, the relation must be named `Session.authUser`. + ```prisma model User { id String @id diff --git a/packages/adapter-prisma/package.json b/packages/adapter-prisma/package.json index 5e6c53797..6ddf9abdf 100644 --- a/packages/adapter-prisma/package.json +++ b/packages/adapter-prisma/package.json @@ -13,7 +13,7 @@ "scripts": { "build": "shx rm -rf ./dist/* && tsc", "test": "tsx tests/prisma.ts", - "test-setup": "prisma db push", + "test-setup": "prisma db push && prisma generate", "auri.build": "pnpm build" }, "keywords": [ diff --git a/packages/adapter-prisma/src/index.ts b/packages/adapter-prisma/src/index.ts index 2030527fa..896dc0fee 100644 --- a/packages/adapter-prisma/src/index.ts +++ b/packages/adapter-prisma/src/index.ts @@ -39,10 +39,7 @@ export class PrismaAdapter<_PrismaClient extends PrismaClient> implements Adapte sessionId: string ): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> { const userModelKey = this.userModel.name[0].toLowerCase() + this.userModel.name.slice(1); - const result = await this.sessionModel.findUnique<{ - // this is a lie to make TS shut up - user: UserSchema; - }>({ + const result = await this.sessionModel.findUnique({ where: { id: sessionId }, @@ -51,7 +48,9 @@ export class PrismaAdapter<_PrismaClient extends PrismaClient> implements Adapte } }); if (!result) return [null, null]; - const userResult: UserSchema = result[userModelKey as "user"]; + const userResult: UserSchema = result[ + userModelKey as keyof typeof result + ] as any as UserSchema; delete result[userModelKey as keyof typeof result]; return [transformIntoDatabaseSession(result), transformIntoDatabaseUser(userResult)]; } diff --git a/packages/adapter-prisma/tests/prisma.ts b/packages/adapter-prisma/tests/prisma.ts index 948977a7a..d8c2e70ca 100644 --- a/packages/adapter-prisma/tests/prisma.ts +++ b/packages/adapter-prisma/tests/prisma.ts @@ -20,3 +20,11 @@ await client.session.deleteMany(); await client.user.deleteMany(); process.exit(0); + +declare module "lucia" { + interface Register { + DatabaseUserAttributes: { + username: string; + }; + } +}