@holeauth/2fa-drizzle 0.0.1-alpha.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/LICENSE +21 -0
- package/README.md +11 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/mysql/index.cjs +72 -0
- package/dist/mysql/index.cjs.map +1 -0
- package/dist/mysql/index.d.cts +138 -0
- package/dist/mysql/index.d.ts +138 -0
- package/dist/mysql/index.js +69 -0
- package/dist/mysql/index.js.map +1 -0
- package/dist/pg/index.cjs +71 -0
- package/dist/pg/index.cjs.map +1 -0
- package/dist/pg/index.d.cts +154 -0
- package/dist/pg/index.d.ts +154 -0
- package/dist/pg/index.js +68 -0
- package/dist/pg/index.js.map +1 -0
- package/dist/sqlite/index.cjs +71 -0
- package/dist/sqlite/index.cjs.map +1 -0
- package/dist/sqlite/index.d.cts +138 -0
- package/dist/sqlite/index.d.ts +138 -0
- package/dist/sqlite/index.js +68 -0
- package/dist/sqlite/index.js.map +1 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robert Kratz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @holeauth/2fa-drizzle
|
|
2
|
+
|
|
3
|
+
Drizzle adapter for `@holeauth/plugin-2fa`. Subpaths: `/pg`, `/mysql`, `/sqlite`.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { createTwoFactorTables, createTwoFactorAdapter } from '@holeauth/2fa-drizzle/pg';
|
|
7
|
+
import { users } from '@/db/schema';
|
|
8
|
+
|
|
9
|
+
export const twofa = createTwoFactorTables({ usersTable: users });
|
|
10
|
+
const twoFactorAdapter = createTwoFactorAdapter({ db, tables: twofa.tables });
|
|
11
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var mysqlCore = require('drizzle-orm/mysql-core');
|
|
4
|
+
var drizzleOrm = require('drizzle-orm');
|
|
5
|
+
|
|
6
|
+
// src/mysql/index.ts
|
|
7
|
+
function createTwoFactorTables(opts) {
|
|
8
|
+
const { usersTable, prefix = "holeauth_2fa_" } = opts;
|
|
9
|
+
const p = (s) => `${prefix}${s}`;
|
|
10
|
+
const twoFactor = mysqlCore.mysqlTable(p("credential"), {
|
|
11
|
+
userId: mysqlCore.varchar("user_id", { length: 191 }).primaryKey().references(() => usersTable.id, { onDelete: "cascade" }),
|
|
12
|
+
secret: mysqlCore.varchar("secret", { length: 191 }).notNull(),
|
|
13
|
+
enabled: mysqlCore.boolean("enabled").notNull().default(false),
|
|
14
|
+
recoveryCodes: mysqlCore.json("recovery_codes").$type().notNull(),
|
|
15
|
+
createdAt: mysqlCore.timestamp("created_at", { fsp: 3 }).notNull().defaultNow(),
|
|
16
|
+
updatedAt: mysqlCore.timestamp("updated_at", { fsp: 3 }).notNull().defaultNow()
|
|
17
|
+
});
|
|
18
|
+
const twoFactorRelations = drizzleOrm.relations(twoFactor, ({ one }) => ({
|
|
19
|
+
user: one(usersTable, { fields: [twoFactor.userId], references: [usersTable.id] })
|
|
20
|
+
}));
|
|
21
|
+
return { tables: { twoFactor }, relations: { twoFactorRelations } };
|
|
22
|
+
}
|
|
23
|
+
var rowToRecord = (r) => ({
|
|
24
|
+
userId: String(r.userId),
|
|
25
|
+
secret: String(r.secret),
|
|
26
|
+
enabled: Boolean(r.enabled),
|
|
27
|
+
recoveryCodes: r.recoveryCodes ?? [],
|
|
28
|
+
createdAt: r.createdAt,
|
|
29
|
+
updatedAt: r.updatedAt
|
|
30
|
+
});
|
|
31
|
+
function createTwoFactorAdapter(opts) {
|
|
32
|
+
const { db, tables } = opts;
|
|
33
|
+
const { twoFactor } = tables;
|
|
34
|
+
return {
|
|
35
|
+
async getByUserId(userId) {
|
|
36
|
+
const rows = await db.select().from(twoFactor).where(drizzleOrm.eq(twoFactor.userId, userId)).limit(1);
|
|
37
|
+
return rows[0] ? rowToRecord(rows[0]) : null;
|
|
38
|
+
},
|
|
39
|
+
async upsert(record) {
|
|
40
|
+
await db.insert(twoFactor).values({
|
|
41
|
+
userId: record.userId,
|
|
42
|
+
secret: record.secret,
|
|
43
|
+
enabled: record.enabled,
|
|
44
|
+
recoveryCodes: record.recoveryCodes
|
|
45
|
+
}).onDuplicateKeyUpdate({
|
|
46
|
+
set: {
|
|
47
|
+
secret: record.secret,
|
|
48
|
+
enabled: record.enabled,
|
|
49
|
+
recoveryCodes: record.recoveryCodes,
|
|
50
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
const rows = await db.select().from(twoFactor).where(drizzleOrm.eq(twoFactor.userId, record.userId)).limit(1);
|
|
54
|
+
return rowToRecord(rows[0]);
|
|
55
|
+
},
|
|
56
|
+
async update(userId, patch) {
|
|
57
|
+
const toSet = { ...patch, updatedAt: /* @__PURE__ */ new Date() };
|
|
58
|
+
delete toSet.userId;
|
|
59
|
+
await db.update(twoFactor).set(toSet).where(drizzleOrm.eq(twoFactor.userId, userId));
|
|
60
|
+
const rows = await db.select().from(twoFactor).where(drizzleOrm.eq(twoFactor.userId, userId)).limit(1);
|
|
61
|
+
return rows[0] ? rowToRecord(rows[0]) : null;
|
|
62
|
+
},
|
|
63
|
+
async delete(userId) {
|
|
64
|
+
await db.delete(twoFactor).where(drizzleOrm.eq(twoFactor.userId, userId));
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
exports.createTwoFactorAdapter = createTwoFactorAdapter;
|
|
70
|
+
exports.createTwoFactorTables = createTwoFactorTables;
|
|
71
|
+
//# sourceMappingURL=index.cjs.map
|
|
72
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/mysql/index.ts"],"names":["mysqlTable","varchar","boolean","json","timestamp","relations","eq"],"mappings":";;;;;;AAmBO,SAAS,sBAAiD,IAAA,EAAuC;AACtG,EAAA,MAAM,EAAE,UAAA,EAAY,MAAA,GAAS,eAA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CAAA;AACH,MAAA,MAAM,OAAO,MAAM,EAAA,CAAG,MAAA,EAAO,CAAE,KAAK,SAAS,CAAA,CAAE,KAAA,CAAMA,aAAA,CAAG,UAAU,MAAA,EAAQ,MAAA,CAAO,MAAM,CAAC,CAAA,CAAE,MAAM,CAAC,CAAA;AACjG,MAAA,OAAO,WAAA,CAAY,IAAA,CAAK,CAAC,CAA4B,CAAA;AAAA,IACvD,CAAA;AAAA,IACA,MAAM,MAAA,CAAO,MAAA,EAAQ,KAAA,EAAO;AAC1B,MAAA,MAAM,QAAiC,EAAE,GAAG,OAAO,SAAA,kBAAW,IAAI,MAAK,EAAE;AACzE,MAAA,OAAO,KAAA,CAAM,MAAA;AACb,MAAA,MAAM,EAAA,CAAG,MAAA,CAAO,SAAS,CAAA,CAAE,GAAA,CAAI,KAAK,CAAA,CAAE,KAAA,CAAMA,aAAA,CAAG,SAAA,CAAU,MAAA,EAAQ,MAAM,CAAC,CAAA;AACxE,MAAA,MAAM,OAAO,MAAM,EAAA,CAAG,MAAA,EAAO,CAAE,KAAK,SAAS,CAAA,CAAE,KAAA,CAAMA,aAAA,CAAG,UAAU,MAAA,EAAQ,MAAM,CAAC,CAAA,CAAE,MAAM,CAAC,CAAA;AAC1F,MAAA,OAAO,KAAK,CAAC,CAAA,GAAI,YAAY,IAAA,CAAK,CAAC,CAA4B,CAAA,GAAI,IAAA;AAAA,IACrE,CAAA;AAAA,IACA,MAAM,OAAO,MAAA,EAAQ;AACnB,MAAA,MAAM,EAAA,CAAG,OAAO,SAAS,CAAA,CAAE,MAAMA,aAAA,CAAG,SAAA,CAAU,MAAA,EAAQ,MAAM,CAAC,CAAA;AAAA,IAC/D;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["import {\n mysqlTable,\n varchar,\n timestamp,\n boolean,\n json,\n type MySqlTableWithColumns,\n} from 'drizzle-orm/mysql-core';\nimport { relations, eq } from 'drizzle-orm';\nimport type { TwoFactorAdapter, TwoFactorRecord } from '@holeauth/plugin-2fa';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type MysqlUsersTable = MySqlTableWithColumns<any> & { id: any };\n\nexport interface CreateTwoFactorTablesOptions<U extends MysqlUsersTable> {\n usersTable: U;\n prefix?: string;\n}\n\nexport function createTwoFactorTables<U extends MysqlUsersTable>(opts: CreateTwoFactorTablesOptions<U>) {\n const { usersTable, prefix = 'holeauth_2fa_' } = opts;\n const p = (s: string) => `${prefix}${s}`;\n const twoFactor = mysqlTable(p('credential'), {\n userId: varchar('user_id', { length: 191 })\n .primaryKey()\n .references(() => usersTable.id, { onDelete: 'cascade' }),\n secret: varchar('secret', { length: 191 }).notNull(),\n enabled: boolean('enabled').notNull().default(false),\n recoveryCodes: json('recovery_codes').$type<string[]>().notNull(),\n createdAt: timestamp('created_at', { fsp: 3 }).notNull().defaultNow(),\n updatedAt: timestamp('updated_at', { fsp: 3 }).notNull().defaultNow(),\n });\n const twoFactorRelations = relations(twoFactor, ({ one }) => ({\n user: one(usersTable, { fields: [twoFactor.userId], references: [usersTable.id] }),\n }));\n return { tables: { twoFactor }, relations: { twoFactorRelations } };\n}\n\ntype Tables = ReturnType<typeof createTwoFactorTables>['tables'];\n\nexport interface CreateTwoFactorAdapterOptions {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n db: any;\n tables: Tables;\n}\n\nconst rowToRecord = (r: Record<string, unknown>): TwoFactorRecord => ({\n userId: String(r.userId),\n secret: String(r.secret),\n enabled: Boolean(r.enabled),\n recoveryCodes: (r.recoveryCodes as string[] | null | undefined) ?? [],\n createdAt: r.createdAt as Date | undefined,\n updatedAt: r.updatedAt as Date | undefined,\n});\n\nexport function createTwoFactorAdapter(opts: CreateTwoFactorAdapterOptions): TwoFactorAdapter {\n const { db, tables } = opts;\n const { twoFactor } = tables;\n return {\n async getByUserId(userId) {\n const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, userId)).limit(1);\n return rows[0] ? rowToRecord(rows[0] as Record<string, unknown>) : null;\n },\n async upsert(record) {\n await db\n .insert(twoFactor)\n .values({\n userId: record.userId,\n secret: record.secret,\n enabled: record.enabled,\n recoveryCodes: record.recoveryCodes,\n })\n .onDuplicateKeyUpdate({\n set: {\n secret: record.secret,\n enabled: record.enabled,\n recoveryCodes: record.recoveryCodes,\n updatedAt: new Date(),\n },\n });\n const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, record.userId)).limit(1);\n return rowToRecord(rows[0] as Record<string, unknown>);\n },\n async update(userId, patch) {\n const toSet: Record<string, unknown> = { ...patch, updatedAt: new Date() };\n delete toSet.userId;\n await db.update(twoFactor).set(toSet).where(eq(twoFactor.userId, userId));\n const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, userId)).limit(1);\n return rows[0] ? rowToRecord(rows[0] as Record<string, unknown>) : null;\n },\n async delete(userId) {\n await db.delete(twoFactor).where(eq(twoFactor.userId, userId));\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import * as drizzle_orm from 'drizzle-orm';
|
|
2
|
+
import * as drizzle_orm_mysql_core from 'drizzle-orm/mysql-core';
|
|
3
|
+
import { MySqlTableWithColumns } from 'drizzle-orm/mysql-core';
|
|
4
|
+
import { TwoFactorAdapter } from '@holeauth/plugin-2fa';
|
|
5
|
+
|
|
6
|
+
type MysqlUsersTable = MySqlTableWithColumns<any> & {
|
|
7
|
+
id: any;
|
|
8
|
+
};
|
|
9
|
+
interface CreateTwoFactorTablesOptions<U extends MysqlUsersTable> {
|
|
10
|
+
usersTable: U;
|
|
11
|
+
prefix?: string;
|
|
12
|
+
}
|
|
13
|
+
declare function createTwoFactorTables<U extends MysqlUsersTable>(opts: CreateTwoFactorTablesOptions<U>): {
|
|
14
|
+
tables: {
|
|
15
|
+
twoFactor: MySqlTableWithColumns<{
|
|
16
|
+
name: string;
|
|
17
|
+
schema: undefined;
|
|
18
|
+
columns: {
|
|
19
|
+
userId: drizzle_orm_mysql_core.MySqlColumn<{
|
|
20
|
+
name: "user_id";
|
|
21
|
+
tableName: string;
|
|
22
|
+
dataType: "string";
|
|
23
|
+
columnType: "MySqlVarChar";
|
|
24
|
+
data: string;
|
|
25
|
+
driverParam: string | number;
|
|
26
|
+
notNull: true;
|
|
27
|
+
hasDefault: false;
|
|
28
|
+
isPrimaryKey: true;
|
|
29
|
+
isAutoincrement: false;
|
|
30
|
+
hasRuntimeDefault: false;
|
|
31
|
+
enumValues: [string, ...string[]];
|
|
32
|
+
baseColumn: never;
|
|
33
|
+
identity: undefined;
|
|
34
|
+
generated: undefined;
|
|
35
|
+
}, object>;
|
|
36
|
+
secret: drizzle_orm_mysql_core.MySqlColumn<{
|
|
37
|
+
name: "secret";
|
|
38
|
+
tableName: string;
|
|
39
|
+
dataType: "string";
|
|
40
|
+
columnType: "MySqlVarChar";
|
|
41
|
+
data: string;
|
|
42
|
+
driverParam: string | number;
|
|
43
|
+
notNull: true;
|
|
44
|
+
hasDefault: false;
|
|
45
|
+
isPrimaryKey: false;
|
|
46
|
+
isAutoincrement: false;
|
|
47
|
+
hasRuntimeDefault: false;
|
|
48
|
+
enumValues: [string, ...string[]];
|
|
49
|
+
baseColumn: never;
|
|
50
|
+
identity: undefined;
|
|
51
|
+
generated: undefined;
|
|
52
|
+
}, object>;
|
|
53
|
+
enabled: drizzle_orm_mysql_core.MySqlColumn<{
|
|
54
|
+
name: "enabled";
|
|
55
|
+
tableName: string;
|
|
56
|
+
dataType: "boolean";
|
|
57
|
+
columnType: "MySqlBoolean";
|
|
58
|
+
data: boolean;
|
|
59
|
+
driverParam: number | boolean;
|
|
60
|
+
notNull: true;
|
|
61
|
+
hasDefault: true;
|
|
62
|
+
isPrimaryKey: false;
|
|
63
|
+
isAutoincrement: false;
|
|
64
|
+
hasRuntimeDefault: false;
|
|
65
|
+
enumValues: undefined;
|
|
66
|
+
baseColumn: never;
|
|
67
|
+
identity: undefined;
|
|
68
|
+
generated: undefined;
|
|
69
|
+
}, object>;
|
|
70
|
+
recoveryCodes: drizzle_orm_mysql_core.MySqlColumn<{
|
|
71
|
+
name: "recovery_codes";
|
|
72
|
+
tableName: string;
|
|
73
|
+
dataType: "json";
|
|
74
|
+
columnType: "MySqlJson";
|
|
75
|
+
data: string[];
|
|
76
|
+
driverParam: string;
|
|
77
|
+
notNull: true;
|
|
78
|
+
hasDefault: false;
|
|
79
|
+
isPrimaryKey: false;
|
|
80
|
+
isAutoincrement: false;
|
|
81
|
+
hasRuntimeDefault: false;
|
|
82
|
+
enumValues: undefined;
|
|
83
|
+
baseColumn: never;
|
|
84
|
+
identity: undefined;
|
|
85
|
+
generated: undefined;
|
|
86
|
+
}, object>;
|
|
87
|
+
createdAt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
88
|
+
name: "created_at";
|
|
89
|
+
tableName: string;
|
|
90
|
+
dataType: "date";
|
|
91
|
+
columnType: "MySqlTimestamp";
|
|
92
|
+
data: Date;
|
|
93
|
+
driverParam: string | number;
|
|
94
|
+
notNull: true;
|
|
95
|
+
hasDefault: true;
|
|
96
|
+
isPrimaryKey: false;
|
|
97
|
+
isAutoincrement: false;
|
|
98
|
+
hasRuntimeDefault: false;
|
|
99
|
+
enumValues: undefined;
|
|
100
|
+
baseColumn: never;
|
|
101
|
+
identity: undefined;
|
|
102
|
+
generated: undefined;
|
|
103
|
+
}, object>;
|
|
104
|
+
updatedAt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
105
|
+
name: "updated_at";
|
|
106
|
+
tableName: string;
|
|
107
|
+
dataType: "date";
|
|
108
|
+
columnType: "MySqlTimestamp";
|
|
109
|
+
data: Date;
|
|
110
|
+
driverParam: string | number;
|
|
111
|
+
notNull: true;
|
|
112
|
+
hasDefault: true;
|
|
113
|
+
isPrimaryKey: false;
|
|
114
|
+
isAutoincrement: false;
|
|
115
|
+
hasRuntimeDefault: false;
|
|
116
|
+
enumValues: undefined;
|
|
117
|
+
baseColumn: never;
|
|
118
|
+
identity: undefined;
|
|
119
|
+
generated: undefined;
|
|
120
|
+
}, object>;
|
|
121
|
+
};
|
|
122
|
+
dialect: "mysql";
|
|
123
|
+
}>;
|
|
124
|
+
};
|
|
125
|
+
relations: {
|
|
126
|
+
twoFactorRelations: drizzle_orm.Relations<string, {
|
|
127
|
+
user: drizzle_orm.One<U["_"]["name"], true>;
|
|
128
|
+
}>;
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
type Tables = ReturnType<typeof createTwoFactorTables>['tables'];
|
|
132
|
+
interface CreateTwoFactorAdapterOptions {
|
|
133
|
+
db: any;
|
|
134
|
+
tables: Tables;
|
|
135
|
+
}
|
|
136
|
+
declare function createTwoFactorAdapter(opts: CreateTwoFactorAdapterOptions): TwoFactorAdapter;
|
|
137
|
+
|
|
138
|
+
export { type CreateTwoFactorAdapterOptions, type CreateTwoFactorTablesOptions, type MysqlUsersTable, createTwoFactorAdapter, createTwoFactorTables };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import * as drizzle_orm from 'drizzle-orm';
|
|
2
|
+
import * as drizzle_orm_mysql_core from 'drizzle-orm/mysql-core';
|
|
3
|
+
import { MySqlTableWithColumns } from 'drizzle-orm/mysql-core';
|
|
4
|
+
import { TwoFactorAdapter } from '@holeauth/plugin-2fa';
|
|
5
|
+
|
|
6
|
+
type MysqlUsersTable = MySqlTableWithColumns<any> & {
|
|
7
|
+
id: any;
|
|
8
|
+
};
|
|
9
|
+
interface CreateTwoFactorTablesOptions<U extends MysqlUsersTable> {
|
|
10
|
+
usersTable: U;
|
|
11
|
+
prefix?: string;
|
|
12
|
+
}
|
|
13
|
+
declare function createTwoFactorTables<U extends MysqlUsersTable>(opts: CreateTwoFactorTablesOptions<U>): {
|
|
14
|
+
tables: {
|
|
15
|
+
twoFactor: MySqlTableWithColumns<{
|
|
16
|
+
name: string;
|
|
17
|
+
schema: undefined;
|
|
18
|
+
columns: {
|
|
19
|
+
userId: drizzle_orm_mysql_core.MySqlColumn<{
|
|
20
|
+
name: "user_id";
|
|
21
|
+
tableName: string;
|
|
22
|
+
dataType: "string";
|
|
23
|
+
columnType: "MySqlVarChar";
|
|
24
|
+
data: string;
|
|
25
|
+
driverParam: string | number;
|
|
26
|
+
notNull: true;
|
|
27
|
+
hasDefault: false;
|
|
28
|
+
isPrimaryKey: true;
|
|
29
|
+
isAutoincrement: false;
|
|
30
|
+
hasRuntimeDefault: false;
|
|
31
|
+
enumValues: [string, ...string[]];
|
|
32
|
+
baseColumn: never;
|
|
33
|
+
identity: undefined;
|
|
34
|
+
generated: undefined;
|
|
35
|
+
}, object>;
|
|
36
|
+
secret: drizzle_orm_mysql_core.MySqlColumn<{
|
|
37
|
+
name: "secret";
|
|
38
|
+
tableName: string;
|
|
39
|
+
dataType: "string";
|
|
40
|
+
columnType: "MySqlVarChar";
|
|
41
|
+
data: string;
|
|
42
|
+
driverParam: string | number;
|
|
43
|
+
notNull: true;
|
|
44
|
+
hasDefault: false;
|
|
45
|
+
isPrimaryKey: false;
|
|
46
|
+
isAutoincrement: false;
|
|
47
|
+
hasRuntimeDefault: false;
|
|
48
|
+
enumValues: [string, ...string[]];
|
|
49
|
+
baseColumn: never;
|
|
50
|
+
identity: undefined;
|
|
51
|
+
generated: undefined;
|
|
52
|
+
}, object>;
|
|
53
|
+
enabled: drizzle_orm_mysql_core.MySqlColumn<{
|
|
54
|
+
name: "enabled";
|
|
55
|
+
tableName: string;
|
|
56
|
+
dataType: "boolean";
|
|
57
|
+
columnType: "MySqlBoolean";
|
|
58
|
+
data: boolean;
|
|
59
|
+
driverParam: number | boolean;
|
|
60
|
+
notNull: true;
|
|
61
|
+
hasDefault: true;
|
|
62
|
+
isPrimaryKey: false;
|
|
63
|
+
isAutoincrement: false;
|
|
64
|
+
hasRuntimeDefault: false;
|
|
65
|
+
enumValues: undefined;
|
|
66
|
+
baseColumn: never;
|
|
67
|
+
identity: undefined;
|
|
68
|
+
generated: undefined;
|
|
69
|
+
}, object>;
|
|
70
|
+
recoveryCodes: drizzle_orm_mysql_core.MySqlColumn<{
|
|
71
|
+
name: "recovery_codes";
|
|
72
|
+
tableName: string;
|
|
73
|
+
dataType: "json";
|
|
74
|
+
columnType: "MySqlJson";
|
|
75
|
+
data: string[];
|
|
76
|
+
driverParam: string;
|
|
77
|
+
notNull: true;
|
|
78
|
+
hasDefault: false;
|
|
79
|
+
isPrimaryKey: false;
|
|
80
|
+
isAutoincrement: false;
|
|
81
|
+
hasRuntimeDefault: false;
|
|
82
|
+
enumValues: undefined;
|
|
83
|
+
baseColumn: never;
|
|
84
|
+
identity: undefined;
|
|
85
|
+
generated: undefined;
|
|
86
|
+
}, object>;
|
|
87
|
+
createdAt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
88
|
+
name: "created_at";
|
|
89
|
+
tableName: string;
|
|
90
|
+
dataType: "date";
|
|
91
|
+
columnType: "MySqlTimestamp";
|
|
92
|
+
data: Date;
|
|
93
|
+
driverParam: string | number;
|
|
94
|
+
notNull: true;
|
|
95
|
+
hasDefault: true;
|
|
96
|
+
isPrimaryKey: false;
|
|
97
|
+
isAutoincrement: false;
|
|
98
|
+
hasRuntimeDefault: false;
|
|
99
|
+
enumValues: undefined;
|
|
100
|
+
baseColumn: never;
|
|
101
|
+
identity: undefined;
|
|
102
|
+
generated: undefined;
|
|
103
|
+
}, object>;
|
|
104
|
+
updatedAt: drizzle_orm_mysql_core.MySqlColumn<{
|
|
105
|
+
name: "updated_at";
|
|
106
|
+
tableName: string;
|
|
107
|
+
dataType: "date";
|
|
108
|
+
columnType: "MySqlTimestamp";
|
|
109
|
+
data: Date;
|
|
110
|
+
driverParam: string | number;
|
|
111
|
+
notNull: true;
|
|
112
|
+
hasDefault: true;
|
|
113
|
+
isPrimaryKey: false;
|
|
114
|
+
isAutoincrement: false;
|
|
115
|
+
hasRuntimeDefault: false;
|
|
116
|
+
enumValues: undefined;
|
|
117
|
+
baseColumn: never;
|
|
118
|
+
identity: undefined;
|
|
119
|
+
generated: undefined;
|
|
120
|
+
}, object>;
|
|
121
|
+
};
|
|
122
|
+
dialect: "mysql";
|
|
123
|
+
}>;
|
|
124
|
+
};
|
|
125
|
+
relations: {
|
|
126
|
+
twoFactorRelations: drizzle_orm.Relations<string, {
|
|
127
|
+
user: drizzle_orm.One<U["_"]["name"], true>;
|
|
128
|
+
}>;
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
type Tables = ReturnType<typeof createTwoFactorTables>['tables'];
|
|
132
|
+
interface CreateTwoFactorAdapterOptions {
|
|
133
|
+
db: any;
|
|
134
|
+
tables: Tables;
|
|
135
|
+
}
|
|
136
|
+
declare function createTwoFactorAdapter(opts: CreateTwoFactorAdapterOptions): TwoFactorAdapter;
|
|
137
|
+
|
|
138
|
+
export { type CreateTwoFactorAdapterOptions, type CreateTwoFactorTablesOptions, type MysqlUsersTable, createTwoFactorAdapter, createTwoFactorTables };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { mysqlTable, timestamp, json, boolean, varchar } from 'drizzle-orm/mysql-core';
|
|
2
|
+
import { relations, eq } from 'drizzle-orm';
|
|
3
|
+
|
|
4
|
+
// src/mysql/index.ts
|
|
5
|
+
function createTwoFactorTables(opts) {
|
|
6
|
+
const { usersTable, prefix = "holeauth_2fa_" } = opts;
|
|
7
|
+
const p = (s) => `${prefix}${s}`;
|
|
8
|
+
const twoFactor = mysqlTable(p("credential"), {
|
|
9
|
+
userId: varchar("user_id", { length: 191 }).primaryKey().references(() => usersTable.id, { onDelete: "cascade" }),
|
|
10
|
+
secret: varchar("secret", { length: 191 }).notNull(),
|
|
11
|
+
enabled: boolean("enabled").notNull().default(false),
|
|
12
|
+
recoveryCodes: json("recovery_codes").$type().notNull(),
|
|
13
|
+
createdAt: timestamp("created_at", { fsp: 3 }).notNull().defaultNow(),
|
|
14
|
+
updatedAt: timestamp("updated_at", { fsp: 3 }).notNull().defaultNow()
|
|
15
|
+
});
|
|
16
|
+
const twoFactorRelations = relations(twoFactor, ({ one }) => ({
|
|
17
|
+
user: one(usersTable, { fields: [twoFactor.userId], references: [usersTable.id] })
|
|
18
|
+
}));
|
|
19
|
+
return { tables: { twoFactor }, relations: { twoFactorRelations } };
|
|
20
|
+
}
|
|
21
|
+
var rowToRecord = (r) => ({
|
|
22
|
+
userId: String(r.userId),
|
|
23
|
+
secret: String(r.secret),
|
|
24
|
+
enabled: Boolean(r.enabled),
|
|
25
|
+
recoveryCodes: r.recoveryCodes ?? [],
|
|
26
|
+
createdAt: r.createdAt,
|
|
27
|
+
updatedAt: r.updatedAt
|
|
28
|
+
});
|
|
29
|
+
function createTwoFactorAdapter(opts) {
|
|
30
|
+
const { db, tables } = opts;
|
|
31
|
+
const { twoFactor } = tables;
|
|
32
|
+
return {
|
|
33
|
+
async getByUserId(userId) {
|
|
34
|
+
const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, userId)).limit(1);
|
|
35
|
+
return rows[0] ? rowToRecord(rows[0]) : null;
|
|
36
|
+
},
|
|
37
|
+
async upsert(record) {
|
|
38
|
+
await db.insert(twoFactor).values({
|
|
39
|
+
userId: record.userId,
|
|
40
|
+
secret: record.secret,
|
|
41
|
+
enabled: record.enabled,
|
|
42
|
+
recoveryCodes: record.recoveryCodes
|
|
43
|
+
}).onDuplicateKeyUpdate({
|
|
44
|
+
set: {
|
|
45
|
+
secret: record.secret,
|
|
46
|
+
enabled: record.enabled,
|
|
47
|
+
recoveryCodes: record.recoveryCodes,
|
|
48
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, record.userId)).limit(1);
|
|
52
|
+
return rowToRecord(rows[0]);
|
|
53
|
+
},
|
|
54
|
+
async update(userId, patch) {
|
|
55
|
+
const toSet = { ...patch, updatedAt: /* @__PURE__ */ new Date() };
|
|
56
|
+
delete toSet.userId;
|
|
57
|
+
await db.update(twoFactor).set(toSet).where(eq(twoFactor.userId, userId));
|
|
58
|
+
const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, userId)).limit(1);
|
|
59
|
+
return rows[0] ? rowToRecord(rows[0]) : null;
|
|
60
|
+
},
|
|
61
|
+
async delete(userId) {
|
|
62
|
+
await db.delete(twoFactor).where(eq(twoFactor.userId, userId));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export { createTwoFactorAdapter, createTwoFactorTables };
|
|
68
|
+
//# sourceMappingURL=index.js.map
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/mysql/index.ts"],"names":[],"mappings":";;;;AAmBO,SAAS,sBAAiD,IAAA,EAAuC;AACtG,EAAA,MAAM,EAAE,UAAA,EAAY,MAAA,GAAS,eAAA,EAAgB,GAAI,IAAA;AACjD,EAAA,MAAM,IAAI,CAAC,CAAA,KAAc,CAAA,EAAG,MAAM,GAAG,CAAC,CAAA,CAAA;AACtC,EAAA,MAAM,SAAA,GAAY,UAAA,CAAW,CAAA,CAAE,YAAY,CAAA,EAAG;AAAA,IAC5C,QAAQ,OAAA,CAAQ,SAAA,EAAW,EAAE,MAAA,EAAQ,KAAK,CAAA,CACvC,UAAA,EAAW,CACX,WAAW,MAAM,UAAA,CAAW,IAAI,EAAE,QAAA,EAAU,WAAW,CAAA;AAAA,IAC1D,MAAA,EAAQ,QAAQ,QAAA,EAAU,EAAE,QAAQ,GAAA,EAAK,EAAE,OAAA,EAAQ;AAAA,IACnD,SAAS,OAAA,CAAQ,SAAS,EAAE,OAAA,EAAQ,CAAE,QAAQ,KAAK,CAAA;AAAA,IACnD,eAAe,IAAA,CAAK,gBAAgB,CAAA,CAAE,KAAA,GAAkB,OAAA,EAAQ;AAAA,IAChE,SAAA,EAAW,SAAA,CAAU,YAAA,EAAc,EAAE,GAAA,EAAK,GAAG,CAAA,CAAE,OAAA,EAAQ,CAAE,UAAA,EAAW;AAAA,IACpE,SAAA,EAAW,SAAA,CAAU,YAAA,EAAc,EAAE,GAAA,EAAK,GAAG,CAAA,CAAE,OAA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,IAAA,CAAK,CAAC,CAA4B,CAAA,GAAI,IAAA;AAAA,IACrE,CAAA;AAAA,IACA,MAAM,OAAO,MAAA,EAAQ;AACnB,MAAA,MAAM,EAAA,CAAG,OAAO,SAAS,CAAA,CAAE,MAAM,EAAA,CAAG,SAAA,CAAU,MAAA,EAAQ,MAAM,CAAC,CAAA;AAAA,IAC/D;AAAA,GACF;AACF","file":"index.js","sourcesContent":["import {\n mysqlTable,\n varchar,\n timestamp,\n boolean,\n json,\n type MySqlTableWithColumns,\n} from 'drizzle-orm/mysql-core';\nimport { relations, eq } from 'drizzle-orm';\nimport type { TwoFactorAdapter, TwoFactorRecord } from '@holeauth/plugin-2fa';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type MysqlUsersTable = MySqlTableWithColumns<any> & { id: any };\n\nexport interface CreateTwoFactorTablesOptions<U extends MysqlUsersTable> {\n usersTable: U;\n prefix?: string;\n}\n\nexport function createTwoFactorTables<U extends MysqlUsersTable>(opts: CreateTwoFactorTablesOptions<U>) {\n const { usersTable, prefix = 'holeauth_2fa_' } = opts;\n const p = (s: string) => `${prefix}${s}`;\n const twoFactor = mysqlTable(p('credential'), {\n userId: varchar('user_id', { length: 191 })\n .primaryKey()\n .references(() => usersTable.id, { onDelete: 'cascade' }),\n secret: varchar('secret', { length: 191 }).notNull(),\n enabled: boolean('enabled').notNull().default(false),\n recoveryCodes: json('recovery_codes').$type<string[]>().notNull(),\n createdAt: timestamp('created_at', { fsp: 3 }).notNull().defaultNow(),\n updatedAt: timestamp('updated_at', { fsp: 3 }).notNull().defaultNow(),\n });\n const twoFactorRelations = relations(twoFactor, ({ one }) => ({\n user: one(usersTable, { fields: [twoFactor.userId], references: [usersTable.id] }),\n }));\n return { tables: { twoFactor }, relations: { twoFactorRelations } };\n}\n\ntype Tables = ReturnType<typeof createTwoFactorTables>['tables'];\n\nexport interface CreateTwoFactorAdapterOptions {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n db: any;\n tables: Tables;\n}\n\nconst rowToRecord = (r: Record<string, unknown>): TwoFactorRecord => ({\n userId: String(r.userId),\n secret: String(r.secret),\n enabled: Boolean(r.enabled),\n recoveryCodes: (r.recoveryCodes as string[] | null | undefined) ?? [],\n createdAt: r.createdAt as Date | undefined,\n updatedAt: r.updatedAt as Date | undefined,\n});\n\nexport function createTwoFactorAdapter(opts: CreateTwoFactorAdapterOptions): TwoFactorAdapter {\n const { db, tables } = opts;\n const { twoFactor } = tables;\n return {\n async getByUserId(userId) {\n const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, userId)).limit(1);\n return rows[0] ? rowToRecord(rows[0] as Record<string, unknown>) : null;\n },\n async upsert(record) {\n await db\n .insert(twoFactor)\n .values({\n userId: record.userId,\n secret: record.secret,\n enabled: record.enabled,\n recoveryCodes: record.recoveryCodes,\n })\n .onDuplicateKeyUpdate({\n set: {\n secret: record.secret,\n enabled: record.enabled,\n recoveryCodes: record.recoveryCodes,\n updatedAt: new Date(),\n },\n });\n const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, record.userId)).limit(1);\n return rowToRecord(rows[0] as Record<string, unknown>);\n },\n async update(userId, patch) {\n const toSet: Record<string, unknown> = { ...patch, updatedAt: new Date() };\n delete toSet.userId;\n await db.update(twoFactor).set(toSet).where(eq(twoFactor.userId, userId));\n const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, userId)).limit(1);\n return rows[0] ? rowToRecord(rows[0] as Record<string, unknown>) : null;\n },\n async delete(userId) {\n await db.delete(twoFactor).where(eq(twoFactor.userId, userId));\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var pgCore = require('drizzle-orm/pg-core');
|
|
4
|
+
var drizzleOrm = require('drizzle-orm');
|
|
5
|
+
|
|
6
|
+
// src/pg/index.ts
|
|
7
|
+
function createTwoFactorTables(opts) {
|
|
8
|
+
const { usersTable, prefix = "holeauth_2fa_" } = opts;
|
|
9
|
+
const p = (s) => `${prefix}${s}`;
|
|
10
|
+
const twoFactor = pgCore.pgTable(p("credential"), {
|
|
11
|
+
userId: pgCore.text("user_id").primaryKey().references(() => usersTable.id, { onDelete: "cascade" }),
|
|
12
|
+
secret: pgCore.text("secret").notNull(),
|
|
13
|
+
enabled: pgCore.boolean("enabled").notNull().default(false),
|
|
14
|
+
recoveryCodes: pgCore.text("recovery_codes").array().notNull().default([]),
|
|
15
|
+
createdAt: pgCore.timestamp("created_at", { withTimezone: true, mode: "date" }).notNull().defaultNow(),
|
|
16
|
+
updatedAt: pgCore.timestamp("updated_at", { withTimezone: true, mode: "date" }).notNull().defaultNow()
|
|
17
|
+
});
|
|
18
|
+
const twoFactorRelations = drizzleOrm.relations(twoFactor, ({ one }) => ({
|
|
19
|
+
user: one(usersTable, { fields: [twoFactor.userId], references: [usersTable.id] })
|
|
20
|
+
}));
|
|
21
|
+
return { tables: { twoFactor }, relations: { twoFactorRelations } };
|
|
22
|
+
}
|
|
23
|
+
var rowToRecord = (r) => ({
|
|
24
|
+
userId: String(r.userId),
|
|
25
|
+
secret: String(r.secret),
|
|
26
|
+
enabled: Boolean(r.enabled),
|
|
27
|
+
recoveryCodes: r.recoveryCodes ?? [],
|
|
28
|
+
createdAt: r.createdAt,
|
|
29
|
+
updatedAt: r.updatedAt
|
|
30
|
+
});
|
|
31
|
+
function createTwoFactorAdapter(opts) {
|
|
32
|
+
const { db, tables } = opts;
|
|
33
|
+
const { twoFactor } = tables;
|
|
34
|
+
return {
|
|
35
|
+
async getByUserId(userId) {
|
|
36
|
+
const rows = await db.select().from(twoFactor).where(drizzleOrm.eq(twoFactor.userId, userId)).limit(1);
|
|
37
|
+
return rows[0] ? rowToRecord(rows[0]) : null;
|
|
38
|
+
},
|
|
39
|
+
async upsert(record) {
|
|
40
|
+
const [row] = await db.insert(twoFactor).values({
|
|
41
|
+
userId: record.userId,
|
|
42
|
+
secret: record.secret,
|
|
43
|
+
enabled: record.enabled,
|
|
44
|
+
recoveryCodes: record.recoveryCodes
|
|
45
|
+
}).onConflictDoUpdate({
|
|
46
|
+
target: twoFactor.userId,
|
|
47
|
+
set: {
|
|
48
|
+
secret: record.secret,
|
|
49
|
+
enabled: record.enabled,
|
|
50
|
+
recoveryCodes: record.recoveryCodes,
|
|
51
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
52
|
+
}
|
|
53
|
+
}).returning();
|
|
54
|
+
return rowToRecord(row);
|
|
55
|
+
},
|
|
56
|
+
async update(userId, patch) {
|
|
57
|
+
const toSet = { ...patch, updatedAt: /* @__PURE__ */ new Date() };
|
|
58
|
+
delete toSet.userId;
|
|
59
|
+
const [row] = await db.update(twoFactor).set(toSet).where(drizzleOrm.eq(twoFactor.userId, userId)).returning();
|
|
60
|
+
return row ? rowToRecord(row) : null;
|
|
61
|
+
},
|
|
62
|
+
async delete(userId) {
|
|
63
|
+
await db.delete(twoFactor).where(drizzleOrm.eq(twoFactor.userId, userId));
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
exports.createTwoFactorAdapter = createTwoFactorAdapter;
|
|
69
|
+
exports.createTwoFactorTables = createTwoFactorTables;
|
|
70
|
+
//# sourceMappingURL=index.cjs.map
|
|
71
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/pg/index.ts"],"names":["pgTable","text","boolean","timestamp","relations","eq"],"mappings":";;;;;;AAYO,SAAS,sBAA8C,IAAA,EAAuC;AACnG,EAAA,MAAM,EAAE,UAAA,EAAY,MAAA,GAAS,eAAA,EAAgB,GAAI,IAAA;AACjD,EAAA,MAAM,IAAI,CAAC,CAAA,KAAc,CAAA,EAAG,MAAM,GAAG,CAAC,CAAA,CAAA;AAEtC,EAAA,MAAM,SAAA,GAAYA,cAAA,CAAQ,CAAA,CAAE,YAAY,CAAA,EAAG;AAAA,IACzC,MAAA,EAAQC,WAAA,CAAK,SAAS,CAAA,C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Q,KAAA,EAAO;AAC1B,MAAA,MAAM,QAAiC,EAAE,GAAG,OAAO,SAAA,kBAAW,IAAI,MAAK,EAAE;AACzE,MAAA,OAAO,KAAA,CAAM,MAAA;AACb,MAAA,MAAM,CAAC,GAAG,CAAA,GAAI,MAAM,EAAA,CAAG,MAAA,CAAO,SAAS,CAAA,CAAE,GAAA,CAAI,KAAK,CAAA,CAAE,MAAMA,aAAA,CAAG,SAAA,CAAU,QAAQ,MAAM,CAAC,EAAE,SAAA,EAAU;AAClG,MAAA,OAAO,GAAA,GAAM,WAAA,CAAY,GAAG,CAAA,GAAI,IAAA;AAAA,IAClC,CAAA;AAAA,IACA,MAAM,OAAO,MAAA,EAAQ;AACnB,MAAA,MAAM,EAAA,CAAG,OAAO,SAAS,CAAA,CAAE,MAAMA,aAAA,CAAG,SAAA,CAAU,MAAA,EAAQ,MAAM,CAAC,CAAA;AAAA,IAC/D;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["import { pgTable, text, timestamp, boolean, type PgTableWithColumns } from 'drizzle-orm/pg-core';\nimport { relations, eq } from 'drizzle-orm';\nimport type { TwoFactorAdapter, TwoFactorRecord } from '@holeauth/plugin-2fa';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport type PgUsersTable = PgTableWithColumns<any> & { id: any };\n\nexport interface CreateTwoFactorTablesOptions<U extends PgUsersTable> {\n usersTable: U;\n prefix?: string;\n}\n\nexport function createTwoFactorTables<U extends PgUsersTable>(opts: CreateTwoFactorTablesOptions<U>) {\n const { usersTable, prefix = 'holeauth_2fa_' } = opts;\n const p = (s: string) => `${prefix}${s}`;\n\n const twoFactor = pgTable(p('credential'), {\n userId: text('user_id')\n .primaryKey()\n .references(() => usersTable.id, { onDelete: 'cascade' }),\n secret: text('secret').notNull(),\n enabled: boolean('enabled').notNull().default(false),\n recoveryCodes: text('recovery_codes').array().notNull().default([]),\n createdAt: timestamp('created_at', { withTimezone: true, mode: 'date' }).notNull().defaultNow(),\n updatedAt: timestamp('updated_at', { withTimezone: true, mode: 'date' }).notNull().defaultNow(),\n });\n\n const twoFactorRelations = relations(twoFactor, ({ one }) => ({\n user: one(usersTable, { fields: [twoFactor.userId], references: [usersTable.id] }),\n }));\n\n return { tables: { twoFactor }, relations: { twoFactorRelations } };\n}\n\ntype Tables = ReturnType<typeof createTwoFactorTables>['tables'];\n\nexport interface CreateTwoFactorAdapterOptions {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n db: any;\n tables: Tables;\n}\n\nconst rowToRecord = (r: Record<string, unknown>): TwoFactorRecord => ({\n userId: String(r.userId),\n secret: String(r.secret),\n enabled: Boolean(r.enabled),\n recoveryCodes: (r.recoveryCodes as string[] | null | undefined) ?? [],\n createdAt: r.createdAt as Date | undefined,\n updatedAt: r.updatedAt as Date | undefined,\n});\n\nexport function createTwoFactorAdapter(opts: CreateTwoFactorAdapterOptions): TwoFactorAdapter {\n const { db, tables } = opts;\n const { twoFactor } = tables;\n return {\n async getByUserId(userId) {\n const rows = await db.select().from(twoFactor).where(eq(twoFactor.userId, userId)).limit(1);\n return rows[0] ? rowToRecord(rows[0]) : null;\n },\n async upsert(record) {\n const [row] = await db\n .insert(twoFactor)\n .values({\n userId: record.userId,\n secret: record.secret,\n enabled: record.enabled,\n recoveryCodes: record.recoveryCodes,\n })\n .onConflictDoUpdate({\n target: twoFactor.userId,\n set: {\n secret: record.secret,\n enabled: record.enabled,\n recoveryCodes: record.recoveryCodes,\n updatedAt: new Date(),\n },\n })\n .returning();\n return rowToRecord(row);\n },\n async update(userId, patch) {\n const toSet: Record<string, unknown> = { ...patch, updatedAt: new Date() };\n delete toSet.userId;\n const [row] = await db.update(twoFactor).set(toSet).where(eq(twoFactor.userId, userId)).returning();\n return row ? rowToRecord(row) : null;\n },\n async delete(userId) {\n await db.delete(twoFactor).where(eq(twoFactor.userId, userId));\n },\n };\n}\n"]}
|