@factiii/auth 0.5.5 → 0.5.6
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/dist/{chunk-KUYH4DBN.mjs → chunk-EHI4P63M.mjs} +0 -8
- package/dist/database-CqnmD1HM.d.mts +148 -0
- package/dist/database-CqnmD1HM.d.ts +148 -0
- package/dist/drizzle.d.mts +60 -0
- package/dist/drizzle.d.ts +60 -0
- package/dist/drizzle.js +308 -0
- package/dist/drizzle.mjs +281 -0
- package/dist/index.d.mts +6 -208
- package/dist/index.d.ts +6 -208
- package/dist/index.js +4 -294
- package/dist/index.mjs +5 -295
- package/dist/validators.mjs +1 -1
- package/package.json +6 -1
package/dist/drizzle.js
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/drizzle.ts
|
|
21
|
+
var drizzle_exports = {};
|
|
22
|
+
__export(drizzle_exports, {
|
|
23
|
+
createDrizzleAdapter: () => createDrizzleAdapter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(drizzle_exports);
|
|
26
|
+
|
|
27
|
+
// src/adapters/drizzleAdapter.ts
|
|
28
|
+
var import_drizzle_orm = require("drizzle-orm");
|
|
29
|
+
function createDrizzleAdapter(db, tables) {
|
|
30
|
+
const { users, sessions, otps, passwordResets, devices, admins } = tables;
|
|
31
|
+
return {
|
|
32
|
+
user: {
|
|
33
|
+
async findByEmailInsensitive(email) {
|
|
34
|
+
const rows = await db.select().from(users).where(import_drizzle_orm.sql`lower(${users.email}) = lower(${email})`).limit(1);
|
|
35
|
+
return rows[0] ?? null;
|
|
36
|
+
},
|
|
37
|
+
async findByUsernameInsensitive(username) {
|
|
38
|
+
const rows = await db.select().from(users).where(import_drizzle_orm.sql`lower(${users.username}) = lower(${username})`).limit(1);
|
|
39
|
+
return rows[0] ?? null;
|
|
40
|
+
},
|
|
41
|
+
async findByEmailOrUsernameInsensitive(identifier) {
|
|
42
|
+
const rows = await db.select().from(users).where(
|
|
43
|
+
(0, import_drizzle_orm.or)(
|
|
44
|
+
import_drizzle_orm.sql`lower(${users.email}) = lower(${identifier})`,
|
|
45
|
+
import_drizzle_orm.sql`lower(${users.username}) = lower(${identifier})`
|
|
46
|
+
)
|
|
47
|
+
).limit(1);
|
|
48
|
+
return rows[0] ?? null;
|
|
49
|
+
},
|
|
50
|
+
async findByEmailOrOAuthId(email, oauthId) {
|
|
51
|
+
const rows = await db.select().from(users).where(
|
|
52
|
+
(0, import_drizzle_orm.or)(
|
|
53
|
+
import_drizzle_orm.sql`lower(${users.email}) = lower(${email})`,
|
|
54
|
+
(0, import_drizzle_orm.eq)(users.oauthId, oauthId)
|
|
55
|
+
)
|
|
56
|
+
).limit(1);
|
|
57
|
+
return rows[0] ?? null;
|
|
58
|
+
},
|
|
59
|
+
async findById(id) {
|
|
60
|
+
const rows = await db.select().from(users).where((0, import_drizzle_orm.eq)(users.id, id)).limit(1);
|
|
61
|
+
return rows[0] ?? null;
|
|
62
|
+
},
|
|
63
|
+
async findActiveById(id) {
|
|
64
|
+
const rows = await db.select().from(users).where((0, import_drizzle_orm.and)((0, import_drizzle_orm.eq)(users.id, id), (0, import_drizzle_orm.eq)(users.status, "ACTIVE"))).limit(1);
|
|
65
|
+
return rows[0] ?? null;
|
|
66
|
+
},
|
|
67
|
+
async create(data) {
|
|
68
|
+
const rows = await db.insert(users).values(data).returning();
|
|
69
|
+
return rows[0];
|
|
70
|
+
},
|
|
71
|
+
async update(id, data) {
|
|
72
|
+
const rows = await db.update(users).set(data).where((0, import_drizzle_orm.eq)(users.id, id)).returning();
|
|
73
|
+
return rows[0];
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
session: {
|
|
77
|
+
async findById(id) {
|
|
78
|
+
const rows = await db.select({
|
|
79
|
+
id: sessions.id,
|
|
80
|
+
userId: sessions.userId,
|
|
81
|
+
socketId: sessions.socketId,
|
|
82
|
+
twoFaSecret: sessions.twoFaSecret,
|
|
83
|
+
browserName: sessions.browserName,
|
|
84
|
+
issuedAt: sessions.issuedAt,
|
|
85
|
+
lastUsed: sessions.lastUsed,
|
|
86
|
+
revokedAt: sessions.revokedAt,
|
|
87
|
+
deviceId: sessions.deviceId,
|
|
88
|
+
user: {
|
|
89
|
+
status: users.status,
|
|
90
|
+
verifiedHumanAt: users.verifiedHumanAt
|
|
91
|
+
}
|
|
92
|
+
}).from(sessions).innerJoin(users, (0, import_drizzle_orm.eq)(sessions.userId, users.id)).where((0, import_drizzle_orm.eq)(sessions.id, id)).limit(1);
|
|
93
|
+
return rows[0] ?? null;
|
|
94
|
+
},
|
|
95
|
+
async create(data) {
|
|
96
|
+
const rows = await db.insert(sessions).values(data).returning();
|
|
97
|
+
return rows[0];
|
|
98
|
+
},
|
|
99
|
+
async update(id, data) {
|
|
100
|
+
const rows = await db.update(sessions).set(data).where((0, import_drizzle_orm.eq)(sessions.id, id)).returning();
|
|
101
|
+
return rows[0];
|
|
102
|
+
},
|
|
103
|
+
async updateLastUsed(id) {
|
|
104
|
+
await db.update(sessions).set({ lastUsed: /* @__PURE__ */ new Date() }).where((0, import_drizzle_orm.eq)(sessions.id, id));
|
|
105
|
+
const rows = await db.select({
|
|
106
|
+
id: sessions.id,
|
|
107
|
+
userId: sessions.userId,
|
|
108
|
+
socketId: sessions.socketId,
|
|
109
|
+
twoFaSecret: sessions.twoFaSecret,
|
|
110
|
+
browserName: sessions.browserName,
|
|
111
|
+
issuedAt: sessions.issuedAt,
|
|
112
|
+
lastUsed: sessions.lastUsed,
|
|
113
|
+
revokedAt: sessions.revokedAt,
|
|
114
|
+
deviceId: sessions.deviceId,
|
|
115
|
+
user: {
|
|
116
|
+
verifiedHumanAt: users.verifiedHumanAt
|
|
117
|
+
}
|
|
118
|
+
}).from(sessions).innerJoin(users, (0, import_drizzle_orm.eq)(sessions.userId, users.id)).where((0, import_drizzle_orm.eq)(sessions.id, id)).limit(1);
|
|
119
|
+
return rows[0];
|
|
120
|
+
},
|
|
121
|
+
async revoke(id) {
|
|
122
|
+
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where((0, import_drizzle_orm.eq)(sessions.id, id));
|
|
123
|
+
},
|
|
124
|
+
async findActiveByUserId(userId, excludeSessionId) {
|
|
125
|
+
const conditions = [(0, import_drizzle_orm.eq)(sessions.userId, userId), (0, import_drizzle_orm.isNull)(sessions.revokedAt)];
|
|
126
|
+
if (excludeSessionId !== void 0) {
|
|
127
|
+
conditions.push((0, import_drizzle_orm.ne)(sessions.id, excludeSessionId));
|
|
128
|
+
}
|
|
129
|
+
const activeRows = await db.select({
|
|
130
|
+
id: sessions.id,
|
|
131
|
+
socketId: sessions.socketId,
|
|
132
|
+
userId: sessions.userId
|
|
133
|
+
}).from(sessions).where((0, import_drizzle_orm.and)(...conditions));
|
|
134
|
+
return activeRows;
|
|
135
|
+
},
|
|
136
|
+
async revokeAllByUserId(userId, excludeSessionId) {
|
|
137
|
+
const conditions = [(0, import_drizzle_orm.eq)(sessions.userId, userId), (0, import_drizzle_orm.isNull)(sessions.revokedAt)];
|
|
138
|
+
if (excludeSessionId !== void 0) {
|
|
139
|
+
conditions.push((0, import_drizzle_orm.ne)(sessions.id, excludeSessionId));
|
|
140
|
+
}
|
|
141
|
+
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where((0, import_drizzle_orm.and)(...conditions));
|
|
142
|
+
},
|
|
143
|
+
async findTwoFaSecretsByUserId(userId) {
|
|
144
|
+
const secretRows = await db.select({ twoFaSecret: sessions.twoFaSecret }).from(sessions).where((0, import_drizzle_orm.and)((0, import_drizzle_orm.eq)(sessions.userId, userId), (0, import_drizzle_orm.isNotNull)(sessions.twoFaSecret)));
|
|
145
|
+
return secretRows;
|
|
146
|
+
},
|
|
147
|
+
async clearTwoFaSecrets(userId, excludeSessionId) {
|
|
148
|
+
const conditions = [(0, import_drizzle_orm.eq)(sessions.userId, userId)];
|
|
149
|
+
if (excludeSessionId !== void 0) {
|
|
150
|
+
conditions.push((0, import_drizzle_orm.ne)(sessions.id, excludeSessionId));
|
|
151
|
+
}
|
|
152
|
+
await db.update(sessions).set({ twoFaSecret: null }).where((0, import_drizzle_orm.and)(...conditions));
|
|
153
|
+
},
|
|
154
|
+
async findByIdWithDevice(id, userId) {
|
|
155
|
+
const rows = await db.select({
|
|
156
|
+
twoFaSecret: sessions.twoFaSecret,
|
|
157
|
+
deviceId: sessions.deviceId,
|
|
158
|
+
device: {
|
|
159
|
+
pushToken: devices.pushToken
|
|
160
|
+
}
|
|
161
|
+
}).from(sessions).leftJoin(devices, (0, import_drizzle_orm.eq)(sessions.deviceId, devices.id)).where((0, import_drizzle_orm.and)((0, import_drizzle_orm.eq)(sessions.id, id), (0, import_drizzle_orm.eq)(sessions.userId, userId))).limit(1);
|
|
162
|
+
if (!rows[0]) return null;
|
|
163
|
+
const row = rows[0];
|
|
164
|
+
const device = row.device;
|
|
165
|
+
return {
|
|
166
|
+
twoFaSecret: row.twoFaSecret,
|
|
167
|
+
deviceId: row.deviceId,
|
|
168
|
+
device: device?.pushToken ? { pushToken: device.pushToken } : null
|
|
169
|
+
};
|
|
170
|
+
},
|
|
171
|
+
async revokeByDevicePushToken(userId, pushToken, excludeSessionId) {
|
|
172
|
+
const deviceRows = await db.select({ id: devices.id }).from(devices).where((0, import_drizzle_orm.eq)(devices.pushToken, pushToken)).limit(1);
|
|
173
|
+
if (!deviceRows[0]) return;
|
|
174
|
+
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where(
|
|
175
|
+
(0, import_drizzle_orm.and)(
|
|
176
|
+
(0, import_drizzle_orm.eq)(sessions.userId, userId),
|
|
177
|
+
(0, import_drizzle_orm.ne)(sessions.id, excludeSessionId),
|
|
178
|
+
(0, import_drizzle_orm.isNull)(sessions.revokedAt),
|
|
179
|
+
(0, import_drizzle_orm.eq)(sessions.deviceId, deviceRows[0].id)
|
|
180
|
+
)
|
|
181
|
+
);
|
|
182
|
+
},
|
|
183
|
+
async clearDeviceId(userId, deviceId) {
|
|
184
|
+
await db.update(sessions).set({ deviceId: null }).where((0, import_drizzle_orm.and)((0, import_drizzle_orm.eq)(sessions.userId, userId), (0, import_drizzle_orm.eq)(sessions.deviceId, deviceId)));
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
otp: {
|
|
188
|
+
async findValidByUserAndCode(userId, code) {
|
|
189
|
+
const rows = await db.select().from(otps).where(
|
|
190
|
+
(0, import_drizzle_orm.and)((0, import_drizzle_orm.eq)(otps.userId, userId), (0, import_drizzle_orm.eq)(otps.code, code), (0, import_drizzle_orm.gte)(otps.expiresAt, /* @__PURE__ */ new Date()))
|
|
191
|
+
).limit(1);
|
|
192
|
+
return rows[0] ?? null;
|
|
193
|
+
},
|
|
194
|
+
async create(data) {
|
|
195
|
+
const rows = await db.insert(otps).values(data).returning();
|
|
196
|
+
return rows[0];
|
|
197
|
+
},
|
|
198
|
+
async delete(id) {
|
|
199
|
+
await db.delete(otps).where((0, import_drizzle_orm.eq)(otps.id, id));
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
passwordReset: {
|
|
203
|
+
async findById(id) {
|
|
204
|
+
const rows = await db.select({
|
|
205
|
+
id: passwordResets.id,
|
|
206
|
+
createdAt: passwordResets.createdAt,
|
|
207
|
+
userId: passwordResets.userId
|
|
208
|
+
}).from(passwordResets).where((0, import_drizzle_orm.eq)(passwordResets.id, id)).limit(1);
|
|
209
|
+
return rows[0] ?? null;
|
|
210
|
+
},
|
|
211
|
+
async create(userId) {
|
|
212
|
+
const rows = await db.insert(passwordResets).values({ userId }).returning();
|
|
213
|
+
return rows[0];
|
|
214
|
+
},
|
|
215
|
+
async delete(id) {
|
|
216
|
+
await db.delete(passwordResets).where((0, import_drizzle_orm.eq)(passwordResets.id, id));
|
|
217
|
+
},
|
|
218
|
+
async deleteAllByUserId(userId) {
|
|
219
|
+
await db.delete(passwordResets).where((0, import_drizzle_orm.eq)(passwordResets.userId, userId));
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
device: {
|
|
223
|
+
async findByTokenSessionAndUser(pushToken, sessionId, userId) {
|
|
224
|
+
const rows = await db.select({ id: devices.id }).from(devices).where((0, import_drizzle_orm.eq)(devices.pushToken, pushToken)).limit(1);
|
|
225
|
+
if (!rows[0]) return null;
|
|
226
|
+
if (tables.devicesToSessions && tables.devicesToUsers) {
|
|
227
|
+
const sessionLink = await db.select().from(tables.devicesToSessions).where(
|
|
228
|
+
(0, import_drizzle_orm.and)(
|
|
229
|
+
(0, import_drizzle_orm.eq)(tables.devicesToSessions.deviceId, rows[0].id),
|
|
230
|
+
(0, import_drizzle_orm.eq)(tables.devicesToSessions.sessionId, sessionId)
|
|
231
|
+
)
|
|
232
|
+
).limit(1);
|
|
233
|
+
const userLink = await db.select().from(tables.devicesToUsers).where(
|
|
234
|
+
(0, import_drizzle_orm.and)(
|
|
235
|
+
(0, import_drizzle_orm.eq)(tables.devicesToUsers.deviceId, rows[0].id),
|
|
236
|
+
(0, import_drizzle_orm.eq)(tables.devicesToUsers.userId, userId)
|
|
237
|
+
)
|
|
238
|
+
).limit(1);
|
|
239
|
+
if (!sessionLink[0] || !userLink[0]) return null;
|
|
240
|
+
}
|
|
241
|
+
return { id: rows[0].id };
|
|
242
|
+
},
|
|
243
|
+
async upsertByPushToken(pushToken, sessionId, userId) {
|
|
244
|
+
const existing = await db.select({ id: devices.id }).from(devices).where((0, import_drizzle_orm.eq)(devices.pushToken, pushToken)).limit(1);
|
|
245
|
+
let deviceId;
|
|
246
|
+
if (existing[0]) {
|
|
247
|
+
deviceId = existing[0].id;
|
|
248
|
+
} else {
|
|
249
|
+
const insertedRows = await db.insert(devices).values({ pushToken }).returning({ id: devices.id });
|
|
250
|
+
deviceId = insertedRows[0].id;
|
|
251
|
+
}
|
|
252
|
+
if (tables.devicesToSessions) {
|
|
253
|
+
await db.insert(tables.devicesToSessions).values({ deviceId, sessionId }).onConflictDoNothing();
|
|
254
|
+
}
|
|
255
|
+
if (tables.devicesToUsers) {
|
|
256
|
+
await db.insert(tables.devicesToUsers).values({ deviceId, userId }).onConflictDoNothing();
|
|
257
|
+
}
|
|
258
|
+
await db.update(sessions).set({ deviceId }).where((0, import_drizzle_orm.eq)(sessions.id, sessionId));
|
|
259
|
+
},
|
|
260
|
+
async findByUserAndToken(userId, pushToken) {
|
|
261
|
+
if (tables.devicesToUsers) {
|
|
262
|
+
const joinRows = await db.select({ id: devices.id }).from(devices).innerJoin(
|
|
263
|
+
tables.devicesToUsers,
|
|
264
|
+
(0, import_drizzle_orm.eq)(devices.id, tables.devicesToUsers.deviceId)
|
|
265
|
+
).where(
|
|
266
|
+
(0, import_drizzle_orm.and)(
|
|
267
|
+
(0, import_drizzle_orm.eq)(devices.pushToken, pushToken),
|
|
268
|
+
(0, import_drizzle_orm.eq)(tables.devicesToUsers.userId, userId)
|
|
269
|
+
)
|
|
270
|
+
).limit(1);
|
|
271
|
+
return joinRows[0] ? { id: joinRows[0].id } : null;
|
|
272
|
+
}
|
|
273
|
+
const rows = await db.select({ id: devices.id }).from(devices).where((0, import_drizzle_orm.eq)(devices.pushToken, pushToken)).limit(1);
|
|
274
|
+
return rows[0] ? { id: rows[0].id } : null;
|
|
275
|
+
},
|
|
276
|
+
async disconnectUser(deviceId, userId) {
|
|
277
|
+
if (tables.devicesToUsers) {
|
|
278
|
+
await db.delete(tables.devicesToUsers).where(
|
|
279
|
+
(0, import_drizzle_orm.and)(
|
|
280
|
+
(0, import_drizzle_orm.eq)(tables.devicesToUsers.deviceId, deviceId),
|
|
281
|
+
(0, import_drizzle_orm.eq)(tables.devicesToUsers.userId, userId)
|
|
282
|
+
)
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
async hasRemainingUsers(deviceId) {
|
|
287
|
+
if (tables.devicesToUsers) {
|
|
288
|
+
const remainingRows = await db.select({ userId: tables.devicesToUsers.userId }).from(tables.devicesToUsers).where((0, import_drizzle_orm.eq)(tables.devicesToUsers.deviceId, deviceId)).limit(1);
|
|
289
|
+
return remainingRows.length > 0;
|
|
290
|
+
}
|
|
291
|
+
return false;
|
|
292
|
+
},
|
|
293
|
+
async delete(id) {
|
|
294
|
+
await db.delete(devices).where((0, import_drizzle_orm.eq)(devices.id, id));
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
admin: {
|
|
298
|
+
async findByUserId(userId) {
|
|
299
|
+
const rows = await db.select({ ip: admins.ip }).from(admins).where((0, import_drizzle_orm.eq)(admins.userId, userId)).limit(1);
|
|
300
|
+
return rows[0] ?? null;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
306
|
+
0 && (module.exports = {
|
|
307
|
+
createDrizzleAdapter
|
|
308
|
+
});
|
package/dist/drizzle.mjs
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
// src/adapters/drizzleAdapter.ts
|
|
2
|
+
import { eq, and, or, isNull, isNotNull, gte, ne, sql } from "drizzle-orm";
|
|
3
|
+
function createDrizzleAdapter(db, tables) {
|
|
4
|
+
const { users, sessions, otps, passwordResets, devices, admins } = tables;
|
|
5
|
+
return {
|
|
6
|
+
user: {
|
|
7
|
+
async findByEmailInsensitive(email) {
|
|
8
|
+
const rows = await db.select().from(users).where(sql`lower(${users.email}) = lower(${email})`).limit(1);
|
|
9
|
+
return rows[0] ?? null;
|
|
10
|
+
},
|
|
11
|
+
async findByUsernameInsensitive(username) {
|
|
12
|
+
const rows = await db.select().from(users).where(sql`lower(${users.username}) = lower(${username})`).limit(1);
|
|
13
|
+
return rows[0] ?? null;
|
|
14
|
+
},
|
|
15
|
+
async findByEmailOrUsernameInsensitive(identifier) {
|
|
16
|
+
const rows = await db.select().from(users).where(
|
|
17
|
+
or(
|
|
18
|
+
sql`lower(${users.email}) = lower(${identifier})`,
|
|
19
|
+
sql`lower(${users.username}) = lower(${identifier})`
|
|
20
|
+
)
|
|
21
|
+
).limit(1);
|
|
22
|
+
return rows[0] ?? null;
|
|
23
|
+
},
|
|
24
|
+
async findByEmailOrOAuthId(email, oauthId) {
|
|
25
|
+
const rows = await db.select().from(users).where(
|
|
26
|
+
or(
|
|
27
|
+
sql`lower(${users.email}) = lower(${email})`,
|
|
28
|
+
eq(users.oauthId, oauthId)
|
|
29
|
+
)
|
|
30
|
+
).limit(1);
|
|
31
|
+
return rows[0] ?? null;
|
|
32
|
+
},
|
|
33
|
+
async findById(id) {
|
|
34
|
+
const rows = await db.select().from(users).where(eq(users.id, id)).limit(1);
|
|
35
|
+
return rows[0] ?? null;
|
|
36
|
+
},
|
|
37
|
+
async findActiveById(id) {
|
|
38
|
+
const rows = await db.select().from(users).where(and(eq(users.id, id), eq(users.status, "ACTIVE"))).limit(1);
|
|
39
|
+
return rows[0] ?? null;
|
|
40
|
+
},
|
|
41
|
+
async create(data) {
|
|
42
|
+
const rows = await db.insert(users).values(data).returning();
|
|
43
|
+
return rows[0];
|
|
44
|
+
},
|
|
45
|
+
async update(id, data) {
|
|
46
|
+
const rows = await db.update(users).set(data).where(eq(users.id, id)).returning();
|
|
47
|
+
return rows[0];
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
session: {
|
|
51
|
+
async findById(id) {
|
|
52
|
+
const rows = await db.select({
|
|
53
|
+
id: sessions.id,
|
|
54
|
+
userId: sessions.userId,
|
|
55
|
+
socketId: sessions.socketId,
|
|
56
|
+
twoFaSecret: sessions.twoFaSecret,
|
|
57
|
+
browserName: sessions.browserName,
|
|
58
|
+
issuedAt: sessions.issuedAt,
|
|
59
|
+
lastUsed: sessions.lastUsed,
|
|
60
|
+
revokedAt: sessions.revokedAt,
|
|
61
|
+
deviceId: sessions.deviceId,
|
|
62
|
+
user: {
|
|
63
|
+
status: users.status,
|
|
64
|
+
verifiedHumanAt: users.verifiedHumanAt
|
|
65
|
+
}
|
|
66
|
+
}).from(sessions).innerJoin(users, eq(sessions.userId, users.id)).where(eq(sessions.id, id)).limit(1);
|
|
67
|
+
return rows[0] ?? null;
|
|
68
|
+
},
|
|
69
|
+
async create(data) {
|
|
70
|
+
const rows = await db.insert(sessions).values(data).returning();
|
|
71
|
+
return rows[0];
|
|
72
|
+
},
|
|
73
|
+
async update(id, data) {
|
|
74
|
+
const rows = await db.update(sessions).set(data).where(eq(sessions.id, id)).returning();
|
|
75
|
+
return rows[0];
|
|
76
|
+
},
|
|
77
|
+
async updateLastUsed(id) {
|
|
78
|
+
await db.update(sessions).set({ lastUsed: /* @__PURE__ */ new Date() }).where(eq(sessions.id, id));
|
|
79
|
+
const rows = await db.select({
|
|
80
|
+
id: sessions.id,
|
|
81
|
+
userId: sessions.userId,
|
|
82
|
+
socketId: sessions.socketId,
|
|
83
|
+
twoFaSecret: sessions.twoFaSecret,
|
|
84
|
+
browserName: sessions.browserName,
|
|
85
|
+
issuedAt: sessions.issuedAt,
|
|
86
|
+
lastUsed: sessions.lastUsed,
|
|
87
|
+
revokedAt: sessions.revokedAt,
|
|
88
|
+
deviceId: sessions.deviceId,
|
|
89
|
+
user: {
|
|
90
|
+
verifiedHumanAt: users.verifiedHumanAt
|
|
91
|
+
}
|
|
92
|
+
}).from(sessions).innerJoin(users, eq(sessions.userId, users.id)).where(eq(sessions.id, id)).limit(1);
|
|
93
|
+
return rows[0];
|
|
94
|
+
},
|
|
95
|
+
async revoke(id) {
|
|
96
|
+
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where(eq(sessions.id, id));
|
|
97
|
+
},
|
|
98
|
+
async findActiveByUserId(userId, excludeSessionId) {
|
|
99
|
+
const conditions = [eq(sessions.userId, userId), isNull(sessions.revokedAt)];
|
|
100
|
+
if (excludeSessionId !== void 0) {
|
|
101
|
+
conditions.push(ne(sessions.id, excludeSessionId));
|
|
102
|
+
}
|
|
103
|
+
const activeRows = await db.select({
|
|
104
|
+
id: sessions.id,
|
|
105
|
+
socketId: sessions.socketId,
|
|
106
|
+
userId: sessions.userId
|
|
107
|
+
}).from(sessions).where(and(...conditions));
|
|
108
|
+
return activeRows;
|
|
109
|
+
},
|
|
110
|
+
async revokeAllByUserId(userId, excludeSessionId) {
|
|
111
|
+
const conditions = [eq(sessions.userId, userId), isNull(sessions.revokedAt)];
|
|
112
|
+
if (excludeSessionId !== void 0) {
|
|
113
|
+
conditions.push(ne(sessions.id, excludeSessionId));
|
|
114
|
+
}
|
|
115
|
+
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where(and(...conditions));
|
|
116
|
+
},
|
|
117
|
+
async findTwoFaSecretsByUserId(userId) {
|
|
118
|
+
const secretRows = await db.select({ twoFaSecret: sessions.twoFaSecret }).from(sessions).where(and(eq(sessions.userId, userId), isNotNull(sessions.twoFaSecret)));
|
|
119
|
+
return secretRows;
|
|
120
|
+
},
|
|
121
|
+
async clearTwoFaSecrets(userId, excludeSessionId) {
|
|
122
|
+
const conditions = [eq(sessions.userId, userId)];
|
|
123
|
+
if (excludeSessionId !== void 0) {
|
|
124
|
+
conditions.push(ne(sessions.id, excludeSessionId));
|
|
125
|
+
}
|
|
126
|
+
await db.update(sessions).set({ twoFaSecret: null }).where(and(...conditions));
|
|
127
|
+
},
|
|
128
|
+
async findByIdWithDevice(id, userId) {
|
|
129
|
+
const rows = await db.select({
|
|
130
|
+
twoFaSecret: sessions.twoFaSecret,
|
|
131
|
+
deviceId: sessions.deviceId,
|
|
132
|
+
device: {
|
|
133
|
+
pushToken: devices.pushToken
|
|
134
|
+
}
|
|
135
|
+
}).from(sessions).leftJoin(devices, eq(sessions.deviceId, devices.id)).where(and(eq(sessions.id, id), eq(sessions.userId, userId))).limit(1);
|
|
136
|
+
if (!rows[0]) return null;
|
|
137
|
+
const row = rows[0];
|
|
138
|
+
const device = row.device;
|
|
139
|
+
return {
|
|
140
|
+
twoFaSecret: row.twoFaSecret,
|
|
141
|
+
deviceId: row.deviceId,
|
|
142
|
+
device: device?.pushToken ? { pushToken: device.pushToken } : null
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
async revokeByDevicePushToken(userId, pushToken, excludeSessionId) {
|
|
146
|
+
const deviceRows = await db.select({ id: devices.id }).from(devices).where(eq(devices.pushToken, pushToken)).limit(1);
|
|
147
|
+
if (!deviceRows[0]) return;
|
|
148
|
+
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where(
|
|
149
|
+
and(
|
|
150
|
+
eq(sessions.userId, userId),
|
|
151
|
+
ne(sessions.id, excludeSessionId),
|
|
152
|
+
isNull(sessions.revokedAt),
|
|
153
|
+
eq(sessions.deviceId, deviceRows[0].id)
|
|
154
|
+
)
|
|
155
|
+
);
|
|
156
|
+
},
|
|
157
|
+
async clearDeviceId(userId, deviceId) {
|
|
158
|
+
await db.update(sessions).set({ deviceId: null }).where(and(eq(sessions.userId, userId), eq(sessions.deviceId, deviceId)));
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
otp: {
|
|
162
|
+
async findValidByUserAndCode(userId, code) {
|
|
163
|
+
const rows = await db.select().from(otps).where(
|
|
164
|
+
and(eq(otps.userId, userId), eq(otps.code, code), gte(otps.expiresAt, /* @__PURE__ */ new Date()))
|
|
165
|
+
).limit(1);
|
|
166
|
+
return rows[0] ?? null;
|
|
167
|
+
},
|
|
168
|
+
async create(data) {
|
|
169
|
+
const rows = await db.insert(otps).values(data).returning();
|
|
170
|
+
return rows[0];
|
|
171
|
+
},
|
|
172
|
+
async delete(id) {
|
|
173
|
+
await db.delete(otps).where(eq(otps.id, id));
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
passwordReset: {
|
|
177
|
+
async findById(id) {
|
|
178
|
+
const rows = await db.select({
|
|
179
|
+
id: passwordResets.id,
|
|
180
|
+
createdAt: passwordResets.createdAt,
|
|
181
|
+
userId: passwordResets.userId
|
|
182
|
+
}).from(passwordResets).where(eq(passwordResets.id, id)).limit(1);
|
|
183
|
+
return rows[0] ?? null;
|
|
184
|
+
},
|
|
185
|
+
async create(userId) {
|
|
186
|
+
const rows = await db.insert(passwordResets).values({ userId }).returning();
|
|
187
|
+
return rows[0];
|
|
188
|
+
},
|
|
189
|
+
async delete(id) {
|
|
190
|
+
await db.delete(passwordResets).where(eq(passwordResets.id, id));
|
|
191
|
+
},
|
|
192
|
+
async deleteAllByUserId(userId) {
|
|
193
|
+
await db.delete(passwordResets).where(eq(passwordResets.userId, userId));
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
device: {
|
|
197
|
+
async findByTokenSessionAndUser(pushToken, sessionId, userId) {
|
|
198
|
+
const rows = await db.select({ id: devices.id }).from(devices).where(eq(devices.pushToken, pushToken)).limit(1);
|
|
199
|
+
if (!rows[0]) return null;
|
|
200
|
+
if (tables.devicesToSessions && tables.devicesToUsers) {
|
|
201
|
+
const sessionLink = await db.select().from(tables.devicesToSessions).where(
|
|
202
|
+
and(
|
|
203
|
+
eq(tables.devicesToSessions.deviceId, rows[0].id),
|
|
204
|
+
eq(tables.devicesToSessions.sessionId, sessionId)
|
|
205
|
+
)
|
|
206
|
+
).limit(1);
|
|
207
|
+
const userLink = await db.select().from(tables.devicesToUsers).where(
|
|
208
|
+
and(
|
|
209
|
+
eq(tables.devicesToUsers.deviceId, rows[0].id),
|
|
210
|
+
eq(tables.devicesToUsers.userId, userId)
|
|
211
|
+
)
|
|
212
|
+
).limit(1);
|
|
213
|
+
if (!sessionLink[0] || !userLink[0]) return null;
|
|
214
|
+
}
|
|
215
|
+
return { id: rows[0].id };
|
|
216
|
+
},
|
|
217
|
+
async upsertByPushToken(pushToken, sessionId, userId) {
|
|
218
|
+
const existing = await db.select({ id: devices.id }).from(devices).where(eq(devices.pushToken, pushToken)).limit(1);
|
|
219
|
+
let deviceId;
|
|
220
|
+
if (existing[0]) {
|
|
221
|
+
deviceId = existing[0].id;
|
|
222
|
+
} else {
|
|
223
|
+
const insertedRows = await db.insert(devices).values({ pushToken }).returning({ id: devices.id });
|
|
224
|
+
deviceId = insertedRows[0].id;
|
|
225
|
+
}
|
|
226
|
+
if (tables.devicesToSessions) {
|
|
227
|
+
await db.insert(tables.devicesToSessions).values({ deviceId, sessionId }).onConflictDoNothing();
|
|
228
|
+
}
|
|
229
|
+
if (tables.devicesToUsers) {
|
|
230
|
+
await db.insert(tables.devicesToUsers).values({ deviceId, userId }).onConflictDoNothing();
|
|
231
|
+
}
|
|
232
|
+
await db.update(sessions).set({ deviceId }).where(eq(sessions.id, sessionId));
|
|
233
|
+
},
|
|
234
|
+
async findByUserAndToken(userId, pushToken) {
|
|
235
|
+
if (tables.devicesToUsers) {
|
|
236
|
+
const joinRows = await db.select({ id: devices.id }).from(devices).innerJoin(
|
|
237
|
+
tables.devicesToUsers,
|
|
238
|
+
eq(devices.id, tables.devicesToUsers.deviceId)
|
|
239
|
+
).where(
|
|
240
|
+
and(
|
|
241
|
+
eq(devices.pushToken, pushToken),
|
|
242
|
+
eq(tables.devicesToUsers.userId, userId)
|
|
243
|
+
)
|
|
244
|
+
).limit(1);
|
|
245
|
+
return joinRows[0] ? { id: joinRows[0].id } : null;
|
|
246
|
+
}
|
|
247
|
+
const rows = await db.select({ id: devices.id }).from(devices).where(eq(devices.pushToken, pushToken)).limit(1);
|
|
248
|
+
return rows[0] ? { id: rows[0].id } : null;
|
|
249
|
+
},
|
|
250
|
+
async disconnectUser(deviceId, userId) {
|
|
251
|
+
if (tables.devicesToUsers) {
|
|
252
|
+
await db.delete(tables.devicesToUsers).where(
|
|
253
|
+
and(
|
|
254
|
+
eq(tables.devicesToUsers.deviceId, deviceId),
|
|
255
|
+
eq(tables.devicesToUsers.userId, userId)
|
|
256
|
+
)
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
async hasRemainingUsers(deviceId) {
|
|
261
|
+
if (tables.devicesToUsers) {
|
|
262
|
+
const remainingRows = await db.select({ userId: tables.devicesToUsers.userId }).from(tables.devicesToUsers).where(eq(tables.devicesToUsers.deviceId, deviceId)).limit(1);
|
|
263
|
+
return remainingRows.length > 0;
|
|
264
|
+
}
|
|
265
|
+
return false;
|
|
266
|
+
},
|
|
267
|
+
async delete(id) {
|
|
268
|
+
await db.delete(devices).where(eq(devices.id, id));
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
admin: {
|
|
272
|
+
async findByUserId(userId) {
|
|
273
|
+
const rows = await db.select({ ip: admins.ip }).from(admins).where(eq(admins.userId, userId)).limit(1);
|
|
274
|
+
return rows[0] ?? null;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
export {
|
|
280
|
+
createDrizzleAdapter
|
|
281
|
+
};
|