@factiii/auth 0.5.4 → 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 +53 -199
- package/dist/index.d.ts +53 -199
- package/dist/index.js +85 -335
- package/dist/index.mjs +84 -336
- package/dist/validators.mjs +1 -1
- package/package.json +25 -33
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {
|
|
2
|
-
__require,
|
|
3
2
|
biometricVerifySchema,
|
|
4
3
|
changePasswordSchema,
|
|
5
4
|
checkPasswordResetSchema,
|
|
@@ -18,27 +17,28 @@ import {
|
|
|
18
17
|
twoFaResetVerifySchema,
|
|
19
18
|
twoFaVerifySchema,
|
|
20
19
|
verifyEmailSchema
|
|
21
|
-
} from "./chunk-
|
|
20
|
+
} from "./chunk-EHI4P63M.mjs";
|
|
22
21
|
|
|
23
22
|
// src/middleware/authGuard.ts
|
|
24
23
|
import { TRPCError } from "@trpc/server";
|
|
25
24
|
|
|
26
25
|
// src/adapters/prismaAdapter.ts
|
|
27
26
|
function createPrismaAdapter(prisma) {
|
|
27
|
+
const db = prisma;
|
|
28
28
|
return {
|
|
29
29
|
user: {
|
|
30
30
|
async findByEmailInsensitive(email) {
|
|
31
|
-
return
|
|
31
|
+
return db.user.findFirst({
|
|
32
32
|
where: { email: { equals: email, mode: "insensitive" } }
|
|
33
33
|
});
|
|
34
34
|
},
|
|
35
35
|
async findByUsernameInsensitive(username) {
|
|
36
|
-
return
|
|
36
|
+
return db.user.findFirst({
|
|
37
37
|
where: { username: { equals: username, mode: "insensitive" } }
|
|
38
38
|
});
|
|
39
39
|
},
|
|
40
40
|
async findByEmailOrUsernameInsensitive(identifier) {
|
|
41
|
-
return
|
|
41
|
+
return db.user.findFirst({
|
|
42
42
|
where: {
|
|
43
43
|
OR: [
|
|
44
44
|
{ email: { equals: identifier, mode: "insensitive" } },
|
|
@@ -48,7 +48,7 @@ function createPrismaAdapter(prisma) {
|
|
|
48
48
|
});
|
|
49
49
|
},
|
|
50
50
|
async findByEmailOrOAuthId(email, oauthId) {
|
|
51
|
-
return
|
|
51
|
+
return db.user.findFirst({
|
|
52
52
|
where: {
|
|
53
53
|
OR: [
|
|
54
54
|
{ email: { equals: email, mode: "insensitive" } },
|
|
@@ -58,23 +58,23 @@ function createPrismaAdapter(prisma) {
|
|
|
58
58
|
});
|
|
59
59
|
},
|
|
60
60
|
async findById(id) {
|
|
61
|
-
return
|
|
61
|
+
return db.user.findUnique({ where: { id } });
|
|
62
62
|
},
|
|
63
63
|
async findActiveById(id) {
|
|
64
|
-
return
|
|
64
|
+
return db.user.findUnique({
|
|
65
65
|
where: { id, status: "ACTIVE" }
|
|
66
66
|
});
|
|
67
67
|
},
|
|
68
68
|
async create(data) {
|
|
69
|
-
return
|
|
69
|
+
return db.user.create({ data });
|
|
70
70
|
},
|
|
71
71
|
async update(id, data) {
|
|
72
|
-
return
|
|
72
|
+
return db.user.update({ where: { id }, data });
|
|
73
73
|
}
|
|
74
74
|
},
|
|
75
75
|
session: {
|
|
76
76
|
async findById(id) {
|
|
77
|
-
const session = await
|
|
77
|
+
const session = await db.session.findUnique({
|
|
78
78
|
where: { id },
|
|
79
79
|
select: {
|
|
80
80
|
id: true,
|
|
@@ -92,13 +92,13 @@ function createPrismaAdapter(prisma) {
|
|
|
92
92
|
return session;
|
|
93
93
|
},
|
|
94
94
|
async create(data) {
|
|
95
|
-
return
|
|
95
|
+
return db.session.create({ data });
|
|
96
96
|
},
|
|
97
97
|
async update(id, data) {
|
|
98
|
-
return
|
|
98
|
+
return db.session.update({ where: { id }, data });
|
|
99
99
|
},
|
|
100
100
|
async updateLastUsed(id) {
|
|
101
|
-
const session = await
|
|
101
|
+
const session = await db.session.update({
|
|
102
102
|
where: { id },
|
|
103
103
|
data: { lastUsed: /* @__PURE__ */ new Date() },
|
|
104
104
|
select: {
|
|
@@ -117,13 +117,13 @@ function createPrismaAdapter(prisma) {
|
|
|
117
117
|
return session;
|
|
118
118
|
},
|
|
119
119
|
async revoke(id) {
|
|
120
|
-
await
|
|
120
|
+
await db.session.update({
|
|
121
121
|
where: { id },
|
|
122
122
|
data: { revokedAt: /* @__PURE__ */ new Date() }
|
|
123
123
|
});
|
|
124
124
|
},
|
|
125
125
|
async findActiveByUserId(userId, excludeSessionId) {
|
|
126
|
-
return
|
|
126
|
+
return db.session.findMany({
|
|
127
127
|
where: {
|
|
128
128
|
userId,
|
|
129
129
|
revokedAt: null,
|
|
@@ -133,7 +133,7 @@ function createPrismaAdapter(prisma) {
|
|
|
133
133
|
});
|
|
134
134
|
},
|
|
135
135
|
async revokeAllByUserId(userId, excludeSessionId) {
|
|
136
|
-
await
|
|
136
|
+
await db.session.updateMany({
|
|
137
137
|
where: {
|
|
138
138
|
userId,
|
|
139
139
|
revokedAt: null,
|
|
@@ -143,13 +143,13 @@ function createPrismaAdapter(prisma) {
|
|
|
143
143
|
});
|
|
144
144
|
},
|
|
145
145
|
async findTwoFaSecretsByUserId(userId) {
|
|
146
|
-
return
|
|
146
|
+
return db.session.findMany({
|
|
147
147
|
where: { userId, twoFaSecret: { not: null } },
|
|
148
148
|
select: { twoFaSecret: true }
|
|
149
149
|
});
|
|
150
150
|
},
|
|
151
151
|
async clearTwoFaSecrets(userId, excludeSessionId) {
|
|
152
|
-
await
|
|
152
|
+
await db.session.updateMany({
|
|
153
153
|
where: {
|
|
154
154
|
userId,
|
|
155
155
|
...excludeSessionId ? { NOT: { id: excludeSessionId } } : {}
|
|
@@ -158,7 +158,7 @@ function createPrismaAdapter(prisma) {
|
|
|
158
158
|
});
|
|
159
159
|
},
|
|
160
160
|
async findByIdWithDevice(id, userId) {
|
|
161
|
-
const session = await
|
|
161
|
+
const session = await db.session.findUnique({
|
|
162
162
|
where: { id, userId },
|
|
163
163
|
select: {
|
|
164
164
|
twoFaSecret: true,
|
|
@@ -169,7 +169,7 @@ function createPrismaAdapter(prisma) {
|
|
|
169
169
|
return session;
|
|
170
170
|
},
|
|
171
171
|
async revokeByDevicePushToken(userId, pushToken, excludeSessionId) {
|
|
172
|
-
await
|
|
172
|
+
await db.session.updateMany({
|
|
173
173
|
where: {
|
|
174
174
|
userId,
|
|
175
175
|
id: { not: excludeSessionId },
|
|
@@ -180,7 +180,7 @@ function createPrismaAdapter(prisma) {
|
|
|
180
180
|
});
|
|
181
181
|
},
|
|
182
182
|
async clearDeviceId(userId, deviceId) {
|
|
183
|
-
await
|
|
183
|
+
await db.session.updateMany({
|
|
184
184
|
where: { userId, deviceId },
|
|
185
185
|
data: { deviceId: null }
|
|
186
186
|
});
|
|
@@ -188,39 +188,39 @@ function createPrismaAdapter(prisma) {
|
|
|
188
188
|
},
|
|
189
189
|
otp: {
|
|
190
190
|
async findValidByUserAndCode(userId, code) {
|
|
191
|
-
return
|
|
191
|
+
return db.oTP.findFirst({
|
|
192
192
|
where: { userId, code, expiresAt: { gte: /* @__PURE__ */ new Date() } }
|
|
193
193
|
});
|
|
194
194
|
},
|
|
195
195
|
async create(data) {
|
|
196
|
-
return
|
|
196
|
+
return db.oTP.create({ data });
|
|
197
197
|
},
|
|
198
198
|
async delete(id) {
|
|
199
|
-
await
|
|
199
|
+
await db.oTP.delete({ where: { id } });
|
|
200
200
|
}
|
|
201
201
|
},
|
|
202
202
|
passwordReset: {
|
|
203
203
|
async findById(id) {
|
|
204
|
-
return
|
|
204
|
+
return db.passwordReset.findUnique({
|
|
205
205
|
where: { id },
|
|
206
206
|
select: { id: true, createdAt: true, userId: true }
|
|
207
207
|
});
|
|
208
208
|
},
|
|
209
209
|
async create(userId) {
|
|
210
|
-
return
|
|
210
|
+
return db.passwordReset.create({
|
|
211
211
|
data: { userId }
|
|
212
212
|
});
|
|
213
213
|
},
|
|
214
214
|
async delete(id) {
|
|
215
|
-
await
|
|
215
|
+
await db.passwordReset.delete({ where: { id } });
|
|
216
216
|
},
|
|
217
217
|
async deleteAllByUserId(userId) {
|
|
218
|
-
await
|
|
218
|
+
await db.passwordReset.deleteMany({ where: { userId } });
|
|
219
219
|
}
|
|
220
220
|
},
|
|
221
221
|
device: {
|
|
222
222
|
async findByTokenSessionAndUser(pushToken, sessionId, userId) {
|
|
223
|
-
return
|
|
223
|
+
return db.device.findFirst({
|
|
224
224
|
where: {
|
|
225
225
|
pushToken,
|
|
226
226
|
sessions: { some: { id: sessionId } },
|
|
@@ -230,7 +230,7 @@ function createPrismaAdapter(prisma) {
|
|
|
230
230
|
});
|
|
231
231
|
},
|
|
232
232
|
async upsertByPushToken(pushToken, sessionId, userId) {
|
|
233
|
-
await
|
|
233
|
+
await db.device.upsert({
|
|
234
234
|
where: { pushToken },
|
|
235
235
|
create: {
|
|
236
236
|
pushToken,
|
|
@@ -244,31 +244,31 @@ function createPrismaAdapter(prisma) {
|
|
|
244
244
|
});
|
|
245
245
|
},
|
|
246
246
|
async findByUserAndToken(userId, pushToken) {
|
|
247
|
-
return
|
|
247
|
+
return db.device.findFirst({
|
|
248
248
|
where: { users: { some: { id: userId } }, pushToken },
|
|
249
249
|
select: { id: true }
|
|
250
250
|
});
|
|
251
251
|
},
|
|
252
252
|
async disconnectUser(deviceId, userId) {
|
|
253
|
-
await
|
|
253
|
+
await db.device.update({
|
|
254
254
|
where: { id: deviceId },
|
|
255
255
|
data: { users: { disconnect: { id: userId } } }
|
|
256
256
|
});
|
|
257
257
|
},
|
|
258
258
|
async hasRemainingUsers(deviceId) {
|
|
259
|
-
const result = await
|
|
259
|
+
const result = await db.device.findUnique({
|
|
260
260
|
where: { id: deviceId },
|
|
261
261
|
select: { users: { select: { id: true }, take: 1 } }
|
|
262
262
|
});
|
|
263
263
|
return (result?.users.length ?? 0) > 0;
|
|
264
264
|
},
|
|
265
265
|
async delete(id) {
|
|
266
|
-
await
|
|
266
|
+
await db.device.delete({ where: { id } });
|
|
267
267
|
}
|
|
268
268
|
},
|
|
269
269
|
admin: {
|
|
270
270
|
async findByUserId(userId) {
|
|
271
|
-
return
|
|
271
|
+
return db.admin.findFirst({
|
|
272
272
|
where: { userId },
|
|
273
273
|
select: { ip: true }
|
|
274
274
|
});
|
|
@@ -330,295 +330,10 @@ function createConsoleEmailAdapter() {
|
|
|
330
330
|
};
|
|
331
331
|
}
|
|
332
332
|
|
|
333
|
-
// src/adapters/drizzleAdapter.ts
|
|
334
|
-
function createDrizzleAdapter(db, tables) {
|
|
335
|
-
const {
|
|
336
|
-
eq,
|
|
337
|
-
and,
|
|
338
|
-
or,
|
|
339
|
-
isNull,
|
|
340
|
-
isNotNull,
|
|
341
|
-
gte,
|
|
342
|
-
ne,
|
|
343
|
-
sql
|
|
344
|
-
} = __require("drizzle-orm");
|
|
345
|
-
const { users, sessions, otps, passwordResets, devices, admins } = tables;
|
|
346
|
-
return {
|
|
347
|
-
user: {
|
|
348
|
-
async findByEmailInsensitive(email) {
|
|
349
|
-
const rows = await db.select().from(users).where(sql`lower(${users.email}) = lower(${email})`).limit(1);
|
|
350
|
-
return rows[0] ?? null;
|
|
351
|
-
},
|
|
352
|
-
async findByUsernameInsensitive(username) {
|
|
353
|
-
const rows = await db.select().from(users).where(sql`lower(${users.username}) = lower(${username})`).limit(1);
|
|
354
|
-
return rows[0] ?? null;
|
|
355
|
-
},
|
|
356
|
-
async findByEmailOrUsernameInsensitive(identifier) {
|
|
357
|
-
const rows = await db.select().from(users).where(
|
|
358
|
-
or(
|
|
359
|
-
sql`lower(${users.email}) = lower(${identifier})`,
|
|
360
|
-
sql`lower(${users.username}) = lower(${identifier})`
|
|
361
|
-
)
|
|
362
|
-
).limit(1);
|
|
363
|
-
return rows[0] ?? null;
|
|
364
|
-
},
|
|
365
|
-
async findByEmailOrOAuthId(email, oauthId) {
|
|
366
|
-
const rows = await db.select().from(users).where(
|
|
367
|
-
or(
|
|
368
|
-
sql`lower(${users.email}) = lower(${email})`,
|
|
369
|
-
eq(users.oauthId, oauthId)
|
|
370
|
-
)
|
|
371
|
-
).limit(1);
|
|
372
|
-
return rows[0] ?? null;
|
|
373
|
-
},
|
|
374
|
-
async findById(id) {
|
|
375
|
-
const rows = await db.select().from(users).where(eq(users.id, id)).limit(1);
|
|
376
|
-
return rows[0] ?? null;
|
|
377
|
-
},
|
|
378
|
-
async findActiveById(id) {
|
|
379
|
-
const rows = await db.select().from(users).where(and(eq(users.id, id), eq(users.status, "ACTIVE"))).limit(1);
|
|
380
|
-
return rows[0] ?? null;
|
|
381
|
-
},
|
|
382
|
-
async create(data) {
|
|
383
|
-
const rows = await db.insert(users).values(data).returning();
|
|
384
|
-
return rows[0];
|
|
385
|
-
},
|
|
386
|
-
async update(id, data) {
|
|
387
|
-
const rows = await db.update(users).set(data).where(eq(users.id, id)).returning();
|
|
388
|
-
return rows[0];
|
|
389
|
-
}
|
|
390
|
-
},
|
|
391
|
-
session: {
|
|
392
|
-
async findById(id) {
|
|
393
|
-
const rows = await db.select({
|
|
394
|
-
id: sessions.id,
|
|
395
|
-
userId: sessions.userId,
|
|
396
|
-
socketId: sessions.socketId,
|
|
397
|
-
twoFaSecret: sessions.twoFaSecret,
|
|
398
|
-
browserName: sessions.browserName,
|
|
399
|
-
issuedAt: sessions.issuedAt,
|
|
400
|
-
lastUsed: sessions.lastUsed,
|
|
401
|
-
revokedAt: sessions.revokedAt,
|
|
402
|
-
deviceId: sessions.deviceId,
|
|
403
|
-
user: {
|
|
404
|
-
status: users.status,
|
|
405
|
-
verifiedHumanAt: users.verifiedHumanAt
|
|
406
|
-
}
|
|
407
|
-
}).from(sessions).innerJoin(users, eq(sessions.userId, users.id)).where(eq(sessions.id, id)).limit(1);
|
|
408
|
-
return rows[0] ?? null;
|
|
409
|
-
},
|
|
410
|
-
async create(data) {
|
|
411
|
-
const rows = await db.insert(sessions).values(data).returning();
|
|
412
|
-
return rows[0];
|
|
413
|
-
},
|
|
414
|
-
async update(id, data) {
|
|
415
|
-
const rows = await db.update(sessions).set(data).where(eq(sessions.id, id)).returning();
|
|
416
|
-
return rows[0];
|
|
417
|
-
},
|
|
418
|
-
async updateLastUsed(id) {
|
|
419
|
-
await db.update(sessions).set({ lastUsed: /* @__PURE__ */ new Date() }).where(eq(sessions.id, id));
|
|
420
|
-
const rows = await db.select({
|
|
421
|
-
id: sessions.id,
|
|
422
|
-
userId: sessions.userId,
|
|
423
|
-
socketId: sessions.socketId,
|
|
424
|
-
twoFaSecret: sessions.twoFaSecret,
|
|
425
|
-
browserName: sessions.browserName,
|
|
426
|
-
issuedAt: sessions.issuedAt,
|
|
427
|
-
lastUsed: sessions.lastUsed,
|
|
428
|
-
revokedAt: sessions.revokedAt,
|
|
429
|
-
deviceId: sessions.deviceId,
|
|
430
|
-
user: {
|
|
431
|
-
verifiedHumanAt: users.verifiedHumanAt
|
|
432
|
-
}
|
|
433
|
-
}).from(sessions).innerJoin(users, eq(sessions.userId, users.id)).where(eq(sessions.id, id)).limit(1);
|
|
434
|
-
return rows[0];
|
|
435
|
-
},
|
|
436
|
-
async revoke(id) {
|
|
437
|
-
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where(eq(sessions.id, id));
|
|
438
|
-
},
|
|
439
|
-
async findActiveByUserId(userId, excludeSessionId) {
|
|
440
|
-
const conditions = [eq(sessions.userId, userId), isNull(sessions.revokedAt)];
|
|
441
|
-
if (excludeSessionId !== void 0) {
|
|
442
|
-
conditions.push(ne(sessions.id, excludeSessionId));
|
|
443
|
-
}
|
|
444
|
-
return db.select({
|
|
445
|
-
id: sessions.id,
|
|
446
|
-
socketId: sessions.socketId,
|
|
447
|
-
userId: sessions.userId
|
|
448
|
-
}).from(sessions).where(and(...conditions));
|
|
449
|
-
},
|
|
450
|
-
async revokeAllByUserId(userId, excludeSessionId) {
|
|
451
|
-
const conditions = [eq(sessions.userId, userId), isNull(sessions.revokedAt)];
|
|
452
|
-
if (excludeSessionId !== void 0) {
|
|
453
|
-
conditions.push(ne(sessions.id, excludeSessionId));
|
|
454
|
-
}
|
|
455
|
-
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where(and(...conditions));
|
|
456
|
-
},
|
|
457
|
-
async findTwoFaSecretsByUserId(userId) {
|
|
458
|
-
return db.select({ twoFaSecret: sessions.twoFaSecret }).from(sessions).where(and(eq(sessions.userId, userId), isNotNull(sessions.twoFaSecret)));
|
|
459
|
-
},
|
|
460
|
-
async clearTwoFaSecrets(userId, excludeSessionId) {
|
|
461
|
-
const conditions = [eq(sessions.userId, userId)];
|
|
462
|
-
if (excludeSessionId !== void 0) {
|
|
463
|
-
conditions.push(ne(sessions.id, excludeSessionId));
|
|
464
|
-
}
|
|
465
|
-
await db.update(sessions).set({ twoFaSecret: null }).where(and(...conditions));
|
|
466
|
-
},
|
|
467
|
-
async findByIdWithDevice(id, userId) {
|
|
468
|
-
const rows = await db.select({
|
|
469
|
-
twoFaSecret: sessions.twoFaSecret,
|
|
470
|
-
deviceId: sessions.deviceId,
|
|
471
|
-
device: {
|
|
472
|
-
pushToken: devices.pushToken
|
|
473
|
-
}
|
|
474
|
-
}).from(sessions).leftJoin(devices, eq(sessions.deviceId, devices.id)).where(and(eq(sessions.id, id), eq(sessions.userId, userId))).limit(1);
|
|
475
|
-
if (!rows[0]) return null;
|
|
476
|
-
const row = rows[0];
|
|
477
|
-
return {
|
|
478
|
-
twoFaSecret: row.twoFaSecret,
|
|
479
|
-
deviceId: row.deviceId,
|
|
480
|
-
device: row.device?.pushToken ? { pushToken: row.device.pushToken } : null
|
|
481
|
-
};
|
|
482
|
-
},
|
|
483
|
-
async revokeByDevicePushToken(userId, pushToken, excludeSessionId) {
|
|
484
|
-
const deviceRows = await db.select({ id: devices.id }).from(devices).where(eq(devices.pushToken, pushToken)).limit(1);
|
|
485
|
-
if (!deviceRows[0]) return;
|
|
486
|
-
await db.update(sessions).set({ revokedAt: /* @__PURE__ */ new Date() }).where(
|
|
487
|
-
and(
|
|
488
|
-
eq(sessions.userId, userId),
|
|
489
|
-
ne(sessions.id, excludeSessionId),
|
|
490
|
-
isNull(sessions.revokedAt),
|
|
491
|
-
eq(sessions.deviceId, deviceRows[0].id)
|
|
492
|
-
)
|
|
493
|
-
);
|
|
494
|
-
},
|
|
495
|
-
async clearDeviceId(userId, deviceId) {
|
|
496
|
-
await db.update(sessions).set({ deviceId: null }).where(and(eq(sessions.userId, userId), eq(sessions.deviceId, deviceId)));
|
|
497
|
-
}
|
|
498
|
-
},
|
|
499
|
-
otp: {
|
|
500
|
-
async findValidByUserAndCode(userId, code) {
|
|
501
|
-
const rows = await db.select().from(otps).where(
|
|
502
|
-
and(eq(otps.userId, userId), eq(otps.code, code), gte(otps.expiresAt, /* @__PURE__ */ new Date()))
|
|
503
|
-
).limit(1);
|
|
504
|
-
return rows[0] ?? null;
|
|
505
|
-
},
|
|
506
|
-
async create(data) {
|
|
507
|
-
const rows = await db.insert(otps).values(data).returning();
|
|
508
|
-
return rows[0];
|
|
509
|
-
},
|
|
510
|
-
async delete(id) {
|
|
511
|
-
await db.delete(otps).where(eq(otps.id, id));
|
|
512
|
-
}
|
|
513
|
-
},
|
|
514
|
-
passwordReset: {
|
|
515
|
-
async findById(id) {
|
|
516
|
-
const rows = await db.select({
|
|
517
|
-
id: passwordResets.id,
|
|
518
|
-
createdAt: passwordResets.createdAt,
|
|
519
|
-
userId: passwordResets.userId
|
|
520
|
-
}).from(passwordResets).where(eq(passwordResets.id, id)).limit(1);
|
|
521
|
-
return rows[0] ?? null;
|
|
522
|
-
},
|
|
523
|
-
async create(userId) {
|
|
524
|
-
const rows = await db.insert(passwordResets).values({ userId }).returning();
|
|
525
|
-
return rows[0];
|
|
526
|
-
},
|
|
527
|
-
async delete(id) {
|
|
528
|
-
await db.delete(passwordResets).where(eq(passwordResets.id, id));
|
|
529
|
-
},
|
|
530
|
-
async deleteAllByUserId(userId) {
|
|
531
|
-
await db.delete(passwordResets).where(eq(passwordResets.userId, userId));
|
|
532
|
-
}
|
|
533
|
-
},
|
|
534
|
-
device: {
|
|
535
|
-
async findByTokenSessionAndUser(pushToken, sessionId, userId) {
|
|
536
|
-
const rows = await db.select({ id: devices.id }).from(devices).where(eq(devices.pushToken, pushToken)).limit(1);
|
|
537
|
-
if (!rows[0]) return null;
|
|
538
|
-
if (tables.devicesToSessions && tables.devicesToUsers) {
|
|
539
|
-
const sessionLink = await db.select().from(tables.devicesToSessions).where(
|
|
540
|
-
and(
|
|
541
|
-
eq(tables.devicesToSessions.deviceId, rows[0].id),
|
|
542
|
-
eq(tables.devicesToSessions.sessionId, sessionId)
|
|
543
|
-
)
|
|
544
|
-
).limit(1);
|
|
545
|
-
const userLink = await db.select().from(tables.devicesToUsers).where(
|
|
546
|
-
and(
|
|
547
|
-
eq(tables.devicesToUsers.deviceId, rows[0].id),
|
|
548
|
-
eq(tables.devicesToUsers.userId, userId)
|
|
549
|
-
)
|
|
550
|
-
).limit(1);
|
|
551
|
-
if (!sessionLink[0] || !userLink[0]) return null;
|
|
552
|
-
}
|
|
553
|
-
return { id: rows[0].id };
|
|
554
|
-
},
|
|
555
|
-
async upsertByPushToken(pushToken, sessionId, userId) {
|
|
556
|
-
const existing = await db.select({ id: devices.id }).from(devices).where(eq(devices.pushToken, pushToken)).limit(1);
|
|
557
|
-
let deviceId;
|
|
558
|
-
if (existing[0]) {
|
|
559
|
-
deviceId = existing[0].id;
|
|
560
|
-
} else {
|
|
561
|
-
const rows = await db.insert(devices).values({ pushToken }).returning({ id: devices.id });
|
|
562
|
-
deviceId = rows[0].id;
|
|
563
|
-
}
|
|
564
|
-
if (tables.devicesToSessions) {
|
|
565
|
-
await db.insert(tables.devicesToSessions).values({ deviceId, sessionId }).onConflictDoNothing();
|
|
566
|
-
}
|
|
567
|
-
if (tables.devicesToUsers) {
|
|
568
|
-
await db.insert(tables.devicesToUsers).values({ deviceId, userId }).onConflictDoNothing();
|
|
569
|
-
}
|
|
570
|
-
await db.update(sessions).set({ deviceId }).where(eq(sessions.id, sessionId));
|
|
571
|
-
},
|
|
572
|
-
async findByUserAndToken(userId, pushToken) {
|
|
573
|
-
if (tables.devicesToUsers) {
|
|
574
|
-
const rows2 = await db.select({ id: devices.id }).from(devices).innerJoin(
|
|
575
|
-
tables.devicesToUsers,
|
|
576
|
-
eq(devices.id, tables.devicesToUsers.deviceId)
|
|
577
|
-
).where(
|
|
578
|
-
and(
|
|
579
|
-
eq(devices.pushToken, pushToken),
|
|
580
|
-
eq(tables.devicesToUsers.userId, userId)
|
|
581
|
-
)
|
|
582
|
-
).limit(1);
|
|
583
|
-
return rows2[0] ? { id: rows2[0].id } : null;
|
|
584
|
-
}
|
|
585
|
-
const rows = await db.select({ id: devices.id }).from(devices).where(eq(devices.pushToken, pushToken)).limit(1);
|
|
586
|
-
return rows[0] ? { id: rows[0].id } : null;
|
|
587
|
-
},
|
|
588
|
-
async disconnectUser(deviceId, userId) {
|
|
589
|
-
if (tables.devicesToUsers) {
|
|
590
|
-
await db.delete(tables.devicesToUsers).where(
|
|
591
|
-
and(
|
|
592
|
-
eq(tables.devicesToUsers.deviceId, deviceId),
|
|
593
|
-
eq(tables.devicesToUsers.userId, userId)
|
|
594
|
-
)
|
|
595
|
-
);
|
|
596
|
-
}
|
|
597
|
-
},
|
|
598
|
-
async hasRemainingUsers(deviceId) {
|
|
599
|
-
if (tables.devicesToUsers) {
|
|
600
|
-
const rows = await db.select({ userId: tables.devicesToUsers.userId }).from(tables.devicesToUsers).where(eq(tables.devicesToUsers.deviceId, deviceId)).limit(1);
|
|
601
|
-
return rows.length > 0;
|
|
602
|
-
}
|
|
603
|
-
return false;
|
|
604
|
-
},
|
|
605
|
-
async delete(id) {
|
|
606
|
-
await db.delete(devices).where(eq(devices.id, id));
|
|
607
|
-
}
|
|
608
|
-
},
|
|
609
|
-
admin: {
|
|
610
|
-
async findByUserId(userId) {
|
|
611
|
-
const rows = await db.select({ ip: admins.ip }).from(admins).where(eq(admins.userId, userId)).limit(1);
|
|
612
|
-
return rows[0] ?? null;
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
};
|
|
616
|
-
}
|
|
617
|
-
|
|
618
333
|
// src/utilities/config.ts
|
|
619
334
|
var defaultTokenSettings = {
|
|
620
|
-
jwtExpiry:
|
|
621
|
-
//
|
|
335
|
+
jwtExpiry: 365 * 24 * 60 * 60,
|
|
336
|
+
// 1 year in seconds
|
|
622
337
|
passwordResetExpiryMs: 60 * 60 * 1e3,
|
|
623
338
|
// 1 hour
|
|
624
339
|
otpValidityMs: 15 * 60 * 1e3
|
|
@@ -629,8 +344,8 @@ var defaultCookieSettings = {
|
|
|
629
344
|
sameSite: "Strict",
|
|
630
345
|
httpOnly: false,
|
|
631
346
|
path: "/",
|
|
632
|
-
maxAge:
|
|
633
|
-
//
|
|
347
|
+
maxAge: 365 * 24 * 60 * 60
|
|
348
|
+
// 1 year in seconds (matches jwtExpiry)
|
|
634
349
|
};
|
|
635
350
|
var defaultStorageKeys = {
|
|
636
351
|
authToken: "auth-token"
|
|
@@ -1080,6 +795,36 @@ function validatePasswordStrength(password, minLength = 6) {
|
|
|
1080
795
|
return { valid: true };
|
|
1081
796
|
}
|
|
1082
797
|
|
|
798
|
+
// src/utilities/session.ts
|
|
799
|
+
async function createSessionWithToken(config, params) {
|
|
800
|
+
const { userId, browserName, socketId, deviceId, extraSessionData } = params;
|
|
801
|
+
const session = await config.database.session.create({
|
|
802
|
+
userId,
|
|
803
|
+
browserName,
|
|
804
|
+
socketId,
|
|
805
|
+
...deviceId != null ? { deviceId } : {},
|
|
806
|
+
...extraSessionData
|
|
807
|
+
});
|
|
808
|
+
const user = await config.database.user.findById(userId);
|
|
809
|
+
const accessToken = createAuthToken(
|
|
810
|
+
{
|
|
811
|
+
id: session.id,
|
|
812
|
+
userId: session.userId,
|
|
813
|
+
verifiedHumanAt: user?.verifiedHumanAt ?? null
|
|
814
|
+
},
|
|
815
|
+
{
|
|
816
|
+
secret: config.secrets.jwt,
|
|
817
|
+
expiresIn: config.tokenSettings.jwtExpiry
|
|
818
|
+
}
|
|
819
|
+
);
|
|
820
|
+
return { accessToken, sessionId: session.id };
|
|
821
|
+
}
|
|
822
|
+
async function createSessionWithTokenAndCookie(config, params, res) {
|
|
823
|
+
const result = await createSessionWithToken(config, params);
|
|
824
|
+
setAuthCookie(res, result.accessToken, config.cookieSettings, config.storageKeys);
|
|
825
|
+
return result;
|
|
826
|
+
}
|
|
827
|
+
|
|
1083
828
|
// src/utilities/totp.ts
|
|
1084
829
|
import crypto from "crypto";
|
|
1085
830
|
import { TOTP } from "totp-generator";
|
|
@@ -1963,13 +1708,15 @@ var TwoFaProcedureFactory = class {
|
|
|
1963
1708
|
import { initTRPC } from "@trpc/server";
|
|
1964
1709
|
import SuperJSON from "superjson";
|
|
1965
1710
|
import { ZodError } from "zod";
|
|
1711
|
+
function hasStringProp(obj, key) {
|
|
1712
|
+
return typeof obj === "object" && obj !== null && key in obj && typeof obj[key] === "string";
|
|
1713
|
+
}
|
|
1966
1714
|
function isPrismaConnectionError(error) {
|
|
1967
1715
|
if (!error || typeof error !== "object") {
|
|
1968
1716
|
return false;
|
|
1969
1717
|
}
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
const codeMatch = errorCode.match(/^P(\d+)$/);
|
|
1718
|
+
if (hasStringProp(error, "code")) {
|
|
1719
|
+
const codeMatch = error.code.match(/^P(\d+)$/);
|
|
1973
1720
|
if (codeMatch) {
|
|
1974
1721
|
const codeNum = parseInt(codeMatch[1], 10);
|
|
1975
1722
|
if (codeNum >= 1e3 && codeNum <= 1003) {
|
|
@@ -1979,14 +1726,13 @@ function isPrismaConnectionError(error) {
|
|
|
1979
1726
|
}
|
|
1980
1727
|
const constructorName = error.constructor?.name || "";
|
|
1981
1728
|
if (constructorName.includes("Prisma")) {
|
|
1982
|
-
const errorMessage = error.message
|
|
1729
|
+
const errorMessage = hasStringProp(error, "message") ? error.message.toLowerCase() : "";
|
|
1983
1730
|
if (errorMessage.includes("can't reach database") || errorMessage.includes("authentication failed") || errorMessage.includes("database server") || errorMessage.includes("timeout") || errorMessage.includes("connection")) {
|
|
1984
1731
|
return true;
|
|
1985
1732
|
}
|
|
1986
1733
|
}
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
return isPrismaConnectionError(cause);
|
|
1734
|
+
if ("cause" in error) {
|
|
1735
|
+
return isPrismaConnectionError(error.cause);
|
|
1990
1736
|
}
|
|
1991
1737
|
return false;
|
|
1992
1738
|
}
|
|
@@ -2032,8 +1778,9 @@ function createBaseProcedure(t, authGuard) {
|
|
|
2032
1778
|
}
|
|
2033
1779
|
function getClientIp(req) {
|
|
2034
1780
|
const forwarded = req.headers["x-forwarded-for"];
|
|
2035
|
-
|
|
2036
|
-
|
|
1781
|
+
const forwardedStr = Array.isArray(forwarded) ? forwarded[0] : forwarded;
|
|
1782
|
+
if (forwardedStr) {
|
|
1783
|
+
return forwardedStr.split(",")[0]?.trim();
|
|
2037
1784
|
}
|
|
2038
1785
|
return req.socket.remoteAddress || void 0;
|
|
2039
1786
|
}
|
|
@@ -2051,7 +1798,7 @@ var AuthRouterFactory = class {
|
|
|
2051
1798
|
constructor(userConfig) {
|
|
2052
1799
|
this.userConfig = userConfig;
|
|
2053
1800
|
this.config = createAuthConfig(this.userConfig);
|
|
2054
|
-
this.schemas = createSchemas(this.
|
|
1801
|
+
this.schemas = createSchemas(this.userConfig.schemaExtensions);
|
|
2055
1802
|
this.t = createTrpcBuilder(this.config);
|
|
2056
1803
|
this.authGuard = createAuthGuard(this.config, this.t);
|
|
2057
1804
|
this.procedure = createBaseProcedure(this.t, this.authGuard);
|
|
@@ -2108,10 +1855,11 @@ export {
|
|
|
2108
1855
|
createAuthRouter,
|
|
2109
1856
|
createAuthToken,
|
|
2110
1857
|
createConsoleEmailAdapter,
|
|
2111
|
-
createDrizzleAdapter,
|
|
2112
1858
|
createNoopEmailAdapter,
|
|
2113
1859
|
createOAuthVerifier,
|
|
2114
1860
|
createPrismaAdapter,
|
|
1861
|
+
createSessionWithToken,
|
|
1862
|
+
createSessionWithTokenAndCookie,
|
|
2115
1863
|
decodeToken,
|
|
2116
1864
|
defaultAuthConfig,
|
|
2117
1865
|
defaultCookieSettings,
|