diff --git a/packages/adapter-drizzle/tests/mysql.ts b/packages/adapter-drizzle/tests/mysql.ts index ac86f0024..6ca8585b3 100644 --- a/packages/adapter-drizzle/tests/mysql.ts +++ b/packages/adapter-drizzle/tests/mysql.ts @@ -76,12 +76,3 @@ await connection.execute("DROP TABLE IF EXISTS user_session"); await connection.execute("DROP TABLE IF EXISTS test_user"); process.exit(); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-drizzle/tests/postgresql.ts b/packages/adapter-drizzle/tests/postgresql.ts index 65607ff05..7afc99c13 100644 --- a/packages/adapter-drizzle/tests/postgresql.ts +++ b/packages/adapter-drizzle/tests/postgresql.ts @@ -62,12 +62,3 @@ await pool.query("DROP TABLE public.session"); await pool.query("DROP TABLE public.user"); process.exit(); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-drizzle/tests/sqlite.ts b/packages/adapter-drizzle/tests/sqlite.ts index 78cb752fc..7567f964d 100644 --- a/packages/adapter-drizzle/tests/sqlite.ts +++ b/packages/adapter-drizzle/tests/sqlite.ts @@ -42,12 +42,3 @@ const db = drizzle(sqliteDB); const adapter = new DrizzleSQLiteAdapter(db, sessionTable, userTable); await testAdapter(adapter); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-mongodb/src/index.ts b/packages/adapter-mongodb/src/index.ts index 01abadf4d..801fa6b40 100644 --- a/packages/adapter-mongodb/src/index.ts +++ b/packages/adapter-mongodb/src/index.ts @@ -1,18 +1,18 @@ import type { Adapter, DatabaseSession, - DatabaseSessionAttributes, + RegisteredDatabaseSessionAttributes, DatabaseUser, - DatabaseUserAttributes + RegisteredDatabaseUserAttributes } from "lucia"; import { Collection } from "mongodb"; -interface UserDoc extends DatabaseUserAttributes { +interface UserDoc extends RegisteredDatabaseUserAttributes { _id: string; __v?: any; } -interface SessionDoc extends DatabaseSessionAttributes { +interface SessionDoc extends RegisteredDatabaseSessionAttributes { _id: string; __v?: any; user_id: string; diff --git a/packages/adapter-mysql/src/base.ts b/packages/adapter-mysql/src/base.ts index 05a30eb4f..07f876eea 100644 --- a/packages/adapter-mysql/src/base.ts +++ b/packages/adapter-mysql/src/base.ts @@ -1,9 +1,9 @@ import type { Adapter, DatabaseSession, - DatabaseSessionAttributes, + RegisteredDatabaseSessionAttributes, DatabaseUser, - DatabaseUserAttributes + RegisteredDatabaseUserAttributes } from "lucia"; export class MySQLAdapter implements Adapter { @@ -106,13 +106,13 @@ export interface Controller { getAll(sql: string, args: any[]): Promise; } -interface SessionSchema extends DatabaseSessionAttributes { +interface SessionSchema extends RegisteredDatabaseSessionAttributes { id: string; user_id: string; expires_at: Date | string; } -interface UserSchema extends DatabaseUserAttributes { +interface UserSchema extends RegisteredDatabaseUserAttributes { id: string; } diff --git a/packages/adapter-mysql/tests/mysql2.ts b/packages/adapter-mysql/tests/mysql2.ts index 27a0d1a55..164cdb106 100644 --- a/packages/adapter-mysql/tests/mysql2.ts +++ b/packages/adapter-mysql/tests/mysql2.ts @@ -48,12 +48,3 @@ await connection.execute("DROP TABLE IF EXISTS user_session"); await connection.execute("DROP TABLE IF EXISTS test_user"); process.exit(); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-mysql/tests/planetscale.ts b/packages/adapter-mysql/tests/planetscale.ts index 6f68949eb..4574e4fd5 100644 --- a/packages/adapter-mysql/tests/planetscale.ts +++ b/packages/adapter-mysql/tests/planetscale.ts @@ -46,12 +46,3 @@ await connection.execute("DROP TABLE IF EXISTS user_session"); await connection.execute("DROP TABLE IF EXISTS test_user"); process.exit(); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-postgresql/src/base.ts b/packages/adapter-postgresql/src/base.ts index 2d1f63eef..12fde199c 100644 --- a/packages/adapter-postgresql/src/base.ts +++ b/packages/adapter-postgresql/src/base.ts @@ -1,9 +1,9 @@ import type { Adapter, DatabaseSession, - DatabaseSessionAttributes, + RegisteredDatabaseSessionAttributes, DatabaseUser, - DatabaseUserAttributes + RegisteredDatabaseUserAttributes } from "lucia"; export class PostgreSQLAdapter implements Adapter { @@ -109,13 +109,13 @@ export interface Controller { getAll(sql: string, args: any[]): Promise; } -interface SessionSchema extends DatabaseSessionAttributes { +interface SessionSchema extends RegisteredDatabaseSessionAttributes { id: string; user_id: string; expires_at: Date; } -interface UserSchema extends DatabaseUserAttributes { +interface UserSchema extends RegisteredDatabaseUserAttributes { id: string; } diff --git a/packages/adapter-postgresql/tests/node-postgres.ts b/packages/adapter-postgresql/tests/node-postgres.ts index 17c6f9914..9e692c74f 100644 --- a/packages/adapter-postgresql/tests/node-postgres.ts +++ b/packages/adapter-postgresql/tests/node-postgres.ts @@ -45,12 +45,3 @@ await pool.query("DROP TABLE public.session"); await pool.query("DROP TABLE public.user"); process.exit(); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-postgresql/tests/postgresjs.ts b/packages/adapter-postgresql/tests/postgresjs.ts index 0b5e17d62..3fd435324 100644 --- a/packages/adapter-postgresql/tests/postgresjs.ts +++ b/packages/adapter-postgresql/tests/postgresjs.ts @@ -38,12 +38,3 @@ await sql`DROP TABLE public.session`; await sql`DROP TABLE public.user`; process.exit(); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-prisma/src/index.ts b/packages/adapter-prisma/src/index.ts index 2c7e456e1..d6983c4fb 100644 --- a/packages/adapter-prisma/src/index.ts +++ b/packages/adapter-prisma/src/index.ts @@ -1,9 +1,9 @@ import type { Adapter, DatabaseSession, - DatabaseSessionAttributes, + RegisteredDatabaseSessionAttributes, DatabaseUser, - DatabaseUserAttributes + RegisteredDatabaseUserAttributes } from "lucia"; export class PrismaAdapter<_PrismaClient extends PrismaClient> implements Adapter { @@ -112,11 +112,11 @@ interface PrismaClient { $transaction: any; } -interface UserSchema extends DatabaseUserAttributes { +interface UserSchema extends RegisteredDatabaseUserAttributes { id: string; } -interface SessionSchema extends DatabaseSessionAttributes { +interface SessionSchema extends RegisteredDatabaseSessionAttributes { id: string; userId: string; expiresAt: Date; diff --git a/packages/adapter-sqlite/src/base.ts b/packages/adapter-sqlite/src/base.ts index 6f37db8bd..77fdb53dd 100644 --- a/packages/adapter-sqlite/src/base.ts +++ b/packages/adapter-sqlite/src/base.ts @@ -1,9 +1,9 @@ import type { Adapter, DatabaseSession, - DatabaseSessionAttributes, + RegisteredDatabaseSessionAttributes, DatabaseUser, - DatabaseUserAttributes + RegisteredDatabaseUserAttributes } from "lucia"; export class SQLiteAdapter implements Adapter { @@ -106,13 +106,13 @@ export interface Controller { getAll(sql: string, args: any[]): Promise; } -interface SessionSchema extends DatabaseSessionAttributes { +interface SessionSchema extends RegisteredDatabaseSessionAttributes { id: string; user_id: string; expires_at: number; } -interface UserSchema extends DatabaseUserAttributes { +interface UserSchema extends RegisteredDatabaseUserAttributes { id: string; } diff --git a/packages/adapter-sqlite/tests/better-sqlite3.ts b/packages/adapter-sqlite/tests/better-sqlite3.ts index 71642d924..35731ad0d 100644 --- a/packages/adapter-sqlite/tests/better-sqlite3.ts +++ b/packages/adapter-sqlite/tests/better-sqlite3.ts @@ -28,12 +28,3 @@ const adapter = new BetterSqlite3Adapter(db, { }); await testAdapter(adapter); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-sqlite/tests/bun-sqlite.ts b/packages/adapter-sqlite/tests/bun-sqlite.ts index 4417cacf6..167e81517 100644 --- a/packages/adapter-sqlite/tests/bun-sqlite.ts +++ b/packages/adapter-sqlite/tests/bun-sqlite.ts @@ -30,12 +30,3 @@ const adapter = new BunSQLiteAdapter(db, { }); await testAdapter(adapter); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-sqlite/tests/d1.ts b/packages/adapter-sqlite/tests/d1.ts index 2025c06f2..228109fcd 100644 --- a/packages/adapter-sqlite/tests/d1.ts +++ b/packages/adapter-sqlite/tests/d1.ts @@ -22,12 +22,3 @@ const adapter = new D1Adapter(d1, { }); await testAdapter(adapter); - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-sqlite/tests/libsql.ts b/packages/adapter-sqlite/tests/libsql.ts index 2f36d45e7..f8863c848 100644 --- a/packages/adapter-sqlite/tests/libsql.ts +++ b/packages/adapter-sqlite/tests/libsql.ts @@ -38,12 +38,3 @@ try { } finally { await fs.rm("test/libsql/test.db"); } - -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} diff --git a/packages/adapter-test/src/index.ts b/packages/adapter-test/src/index.ts index ba6d58c8a..25539eec9 100644 --- a/packages/adapter-test/src/index.ts +++ b/packages/adapter-test/src/index.ts @@ -2,15 +2,6 @@ import { Adapter, DatabaseSession, DatabaseUser } from "lucia"; import { generateRandomString, alphabet } from "oslo/random"; import assert from "node:assert/strict"; -declare module "lucia" { - interface DatabaseUserAttributes { - username: string; - } - interface DatabaseSessionAttributes { - country: string; - } -} - export const databaseUser: DatabaseUser = { id: generateRandomString(15, alphabet("0-9", "a-z")), attributes: { diff --git a/packages/lucia/src/core.ts b/packages/lucia/src/core.ts index 24d917ec6..b138c6b80 100644 --- a/packages/lucia/src/core.ts +++ b/packages/lucia/src/core.ts @@ -5,8 +5,8 @@ import { CookieController } from "oslo/cookie"; import type { Cookie } from "oslo/cookie"; import type { Adapter } from "./database.js"; import type { - DatabaseSessionAttributes, - DatabaseUserAttributes, + RegisteredDatabaseSessionAttributes, + RegisteredDatabaseUserAttributes, RegisteredLucia } from "./index.js"; import { CookieAttributes } from "oslo/cookie"; @@ -39,10 +39,12 @@ export class Lucia< private sessionCookieController: CookieController; private getSessionAttributes: ( - databaseSessionAttributes: DatabaseSessionAttributes + databaseSessionAttributes: RegisteredDatabaseSessionAttributes ) => _SessionAttributes; - private getUserAttributes: (databaseUserAttributes: DatabaseUserAttributes) => _UserAttributes; + private getUserAttributes: ( + databaseUserAttributes: RegisteredDatabaseUserAttributes + ) => _UserAttributes; public readonly sessionCookieName: string; @@ -52,9 +54,11 @@ export class Lucia< sessionExpiresIn?: TimeSpan; sessionCookie?: SessionCookieOptions; getSessionAttributes?: ( - databaseSessionAttributes: DatabaseSessionAttributes + databaseSessionAttributes: RegisteredDatabaseSessionAttributes ) => _SessionAttributes; - getUserAttributes?: (databaseUserAttributes: DatabaseUserAttributes) => _UserAttributes; + getUserAttributes?: ( + databaseUserAttributes: RegisteredDatabaseUserAttributes + ) => _UserAttributes; } ) { this.adapter = adapter; @@ -151,7 +155,7 @@ export class Lucia< public async createSession( userId: string, - attributes: DatabaseSessionAttributes, + attributes: RegisteredDatabaseSessionAttributes, options?: { sessionId?: string; } diff --git a/packages/lucia/src/database.ts b/packages/lucia/src/database.ts index 5ad414592..d01fa04a7 100644 --- a/packages/lucia/src/database.ts +++ b/packages/lucia/src/database.ts @@ -1,4 +1,7 @@ -import type { DatabaseSessionAttributes, DatabaseUserAttributes } from "./index.js"; +import type { + RegisteredDatabaseSessionAttributes, + RegisteredDatabaseUserAttributes +} from "./index.js"; export interface Adapter { getSessionAndUser( @@ -13,12 +16,12 @@ export interface Adapter { export interface DatabaseUser { id: string; - attributes: DatabaseUserAttributes; + attributes: RegisteredDatabaseUserAttributes; } export interface DatabaseSession { userId: string; expiresAt: Date; id: string; - attributes: DatabaseSessionAttributes; + attributes: RegisteredDatabaseSessionAttributes; } diff --git a/packages/lucia/src/index.ts b/packages/lucia/src/index.ts index 2d51ce72b..7fddb567a 100644 --- a/packages/lucia/src/index.ts +++ b/packages/lucia/src/index.ts @@ -16,16 +16,24 @@ export type { CookieAttributes } from "oslo/cookie"; import type { Lucia } from "./core.js"; -export interface DatabaseUserAttributes {} -export interface DatabaseSessionAttributes {} - export interface Register {} export type RegisteredLucia = Register extends { - // need to infer to "copy" the generics of Lucia Lucia: infer _Lucia; } ? _Lucia extends Lucia ? _Lucia : Lucia : Lucia; + +export type RegisteredDatabaseUserAttributes = Register extends { + DatabaseUserAttributes: infer _DatabaseUserAttributes; +} + ? _DatabaseUserAttributes + : {}; + +export type RegisteredDatabaseSessionAttributes = Register extends { + DatabaseSessionAttributes: infer _DatabaseSessionAttributes; +} + ? _DatabaseSessionAttributes + : {};