@auth/drizzle-adapter 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/lib/sqlite.ts CHANGED
@@ -13,6 +13,7 @@ import type {
13
13
  Adapter,
14
14
  AdapterAccount,
15
15
  AdapterAccountType,
16
+ AdapterAuthenticator,
16
17
  AdapterSession,
17
18
  AdapterUser,
18
19
  VerificationToken,
@@ -78,16 +79,44 @@ export function defineTables(
78
79
  token: text("token").notNull(),
79
80
  expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
80
81
  },
81
- (vt) => ({
82
- compositePk: primaryKey({ columns: [vt.identifier, vt.token] }),
82
+ (verficationToken) => ({
83
+ compositePk: primaryKey({
84
+ columns: [verficationToken.identifier, verficationToken.token],
85
+ }),
83
86
  })
84
87
  ) satisfies DefaultSQLiteVerificationTokenTable)
85
88
 
89
+ const authenticatorsTable =
90
+ schema.authenticatorsTable ??
91
+ (sqliteTable(
92
+ "authenticator",
93
+ {
94
+ credentialID: text("credentialID").notNull().unique(),
95
+ userId: text("userId")
96
+ .notNull()
97
+ .references(() => usersTable.id, { onDelete: "cascade" }),
98
+ providerAccountId: text("providerAccountId").notNull(),
99
+ credentialPublicKey: text("credentialPublicKey").notNull(),
100
+ counter: integer("counter").notNull(),
101
+ credentialDeviceType: text("credentialDeviceType").notNull(),
102
+ credentialBackedUp: integer("credentialBackedUp", {
103
+ mode: "boolean",
104
+ }).notNull(),
105
+ transports: text("transports"),
106
+ },
107
+ (authenticator) => ({
108
+ compositePK: primaryKey({
109
+ columns: [authenticator.userId, authenticator.credentialID],
110
+ }),
111
+ })
112
+ ) satisfies DefaultSQLiteAuthenticatorTable)
113
+
86
114
  return {
87
115
  usersTable,
88
116
  accountsTable,
89
117
  sessionsTable,
90
118
  verificationTokensTable,
119
+ authenticatorsTable,
91
120
  }
92
121
  }
93
122
 
@@ -95,8 +124,13 @@ export function SQLiteDrizzleAdapter(
95
124
  client: BaseSQLiteDatabase<"sync" | "async", any, any>,
96
125
  schema?: DefaultSQLiteSchema
97
126
  ): Adapter {
98
- const { usersTable, accountsTable, sessionsTable, verificationTokensTable } =
99
- defineTables(schema)
127
+ const {
128
+ usersTable,
129
+ accountsTable,
130
+ sessionsTable,
131
+ verificationTokensTable,
132
+ authenticatorsTable,
133
+ } = defineTables(schema)
100
134
 
101
135
  return {
102
136
  async createUser(data: AdapterUser) {
@@ -132,7 +166,7 @@ export function SQLiteDrizzleAdapter(
132
166
  userId: string
133
167
  expires: Date
134
168
  }) {
135
- return await client.insert(sessionsTable).values(data).returning().get()
169
+ return client.insert(sessionsTable).values(data).returning().get()
136
170
  },
137
171
  async getSessionAndUser(sessionToken: string) {
138
172
  const result = await client
@@ -207,7 +241,7 @@ export function SQLiteDrizzleAdapter(
207
241
  .run()
208
242
  },
209
243
  async createVerificationToken(data: VerificationToken) {
210
- return await client
244
+ return client
211
245
  .insert(verificationTokensTable)
212
246
  .values(data)
213
247
  .returning()
@@ -243,21 +277,70 @@ export function SQLiteDrizzleAdapter(
243
277
  )
244
278
  .run()
245
279
  },
280
+ async getAccount(providerAccountId: string, provider: string) {
281
+ return client
282
+ .select()
283
+ .from(accountsTable)
284
+ .where(
285
+ and(
286
+ eq(accountsTable.provider, provider),
287
+ eq(accountsTable.providerAccountId, providerAccountId)
288
+ )
289
+ )
290
+ .then((res) => res[0] ?? null) as Promise<AdapterAccount | null>
291
+ },
292
+ async createAuthenticator(data: AdapterAuthenticator) {
293
+ return client
294
+ .insert(authenticatorsTable)
295
+ .values(data)
296
+ .returning()
297
+ .then((res) => res[0] ?? null)
298
+ },
299
+ async getAuthenticator(credentialID: string) {
300
+ return client
301
+ .select()
302
+ .from(authenticatorsTable)
303
+ .where(eq(authenticatorsTable.credentialID, credentialID))
304
+ .then((res) => res[0] ?? null)
305
+ },
306
+ async listAuthenticatorsByUserId(userId: string) {
307
+ return client
308
+ .select()
309
+ .from(authenticatorsTable)
310
+ .where(eq(authenticatorsTable.userId, userId))
311
+ .then((res) => res)
312
+ },
313
+ async updateAuthenticatorCounter(credentialID: string, newCounter: number) {
314
+ const authenticator = await client
315
+ .update(authenticatorsTable)
316
+ .set({ counter: newCounter })
317
+ .where(eq(authenticatorsTable.credentialID, credentialID))
318
+ .returning()
319
+ .then((res) => res[0])
320
+
321
+ if (!authenticator) throw new Error("Authenticator not found.")
322
+
323
+ return authenticator
324
+ },
246
325
  }
247
326
  }
248
327
 
249
328
  type DefaultSQLiteColumn<
250
329
  T extends {
251
- data: string | number | Date
252
- dataType: "string" | "number" | "date"
330
+ data: string | boolean | number | Date
331
+ dataType: "string" | "boolean" | "number" | "date"
253
332
  notNull: boolean
254
- columnType: "SQLiteText" | "SQLiteTimestamp" | "SQLiteInteger"
333
+ columnType:
334
+ | "SQLiteText"
335
+ | "SQLiteBoolean"
336
+ | "SQLiteTimestamp"
337
+ | "SQLiteInteger"
255
338
  },
256
339
  > = SQLiteColumn<{
257
340
  name: string
258
341
  columnType: T["columnType"]
259
342
  data: T["data"]
260
- driverParam: string | number
343
+ driverParam: string | number | boolean
261
344
  notNull: T["notNull"]
262
345
  hasDefault: boolean
263
346
  enumValues: any
@@ -429,9 +512,66 @@ export type DefaultSQLiteVerificationTokenTable = SQLiteTableWithColumns<{
429
512
  schema: string | undefined
430
513
  }>
431
514
 
515
+ export type DefaultSQLiteAuthenticatorTable = SQLiteTableWithColumns<{
516
+ name: string
517
+ columns: {
518
+ credentialID: DefaultSQLiteColumn<{
519
+ columnType: "SQLiteText"
520
+ data: string
521
+ notNull: true
522
+ dataType: "string"
523
+ }>
524
+ userId: DefaultSQLiteColumn<{
525
+ columnType: "SQLiteText"
526
+ data: string
527
+ notNull: true
528
+ dataType: "string"
529
+ }>
530
+ providerAccountId: DefaultSQLiteColumn<{
531
+ columnType: "SQLiteText"
532
+ data: string
533
+ notNull: true
534
+ dataType: "string"
535
+ }>
536
+ credentialPublicKey: DefaultSQLiteColumn<{
537
+ columnType: "SQLiteText"
538
+ data: string
539
+ notNull: true
540
+ dataType: "string"
541
+ }>
542
+ counter: DefaultSQLiteColumn<{
543
+ columnType: "SQLiteInteger"
544
+ data: number
545
+ notNull: true
546
+ dataType: "number"
547
+ }>
548
+ credentialDeviceType: DefaultSQLiteColumn<{
549
+ columnType: "SQLiteText"
550
+ data: string
551
+ notNull: true
552
+ dataType: "string"
553
+ }>
554
+ credentialBackedUp: DefaultSQLiteColumn<{
555
+ columnType: "SQLiteBoolean"
556
+ data: boolean
557
+ notNull: true
558
+ dataType: "boolean"
559
+ }>
560
+ transports: DefaultSQLiteColumn<{
561
+ columnType: "SQLiteText"
562
+ data: string
563
+ notNull: false
564
+ dataType: "string"
565
+ }>
566
+ }
567
+ dialect: "sqlite"
568
+ schema: string | undefined
569
+ }>
570
+
432
571
  export type DefaultSQLiteSchema = {
433
572
  usersTable: DefaultSQLiteUsersTable
434
573
  accountsTable: DefaultSQLiteAccountsTable
435
574
  sessionsTable?: DefaultSQLiteSessionsTable
436
575
  verificationTokensTable?: DefaultSQLiteVerificationTokenTable
576
+ authenticatorsTable?: DefaultSQLiteAuthenticatorTable
437
577
  }