@xk2800/nextjs-template 0.7.2-beta.6 → 0.7.2-beta.8
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/README.md +1 -0
- package/components/auth/forms/signup-form.tsx +3 -0
- package/components/dashboard/admin/activity-logs/admin-activity-logs-table.tsx +1 -1
- package/components/dashboard/admin/users/impersonation-tab.tsx +1 -1
- package/components/dashboard/layout/app-sidebar.tsx +7 -2
- package/components/dashboard/layout/mobile-nav.tsx +95 -0
- package/components/dashboard/layout/top-nav.tsx +14 -33
- package/components/dashboard/settings/activity-logs-card.tsx +1 -1
- package/dist/activity/logger.d.mts +2 -1
- package/dist/activity/logger.d.ts +2 -1
- package/dist/activity/logger.js +64 -13
- package/dist/activity/logger.js.map +1 -1
- package/dist/activity/logger.mjs +4 -4
- package/dist/activity/queries.d.mts +25 -18
- package/dist/activity/queries.d.ts +25 -18
- package/dist/activity/queries.js +73 -20
- package/dist/activity/queries.js.map +1 -1
- package/dist/activity/queries.mjs +15 -13
- package/dist/activity/queries.mjs.map +1 -1
- package/dist/admin/queries.d.mts +2 -1
- package/dist/admin/queries.d.ts +2 -1
- package/dist/admin/queries.js +44 -13
- package/dist/admin/queries.js.map +1 -1
- package/dist/admin/queries.mjs +7 -7
- package/dist/admin/queries.mjs.map +1 -1
- package/dist/api/feedback.js +247 -224
- package/dist/api/feedback.js.map +1 -1
- package/dist/api/feedback.mjs +14 -12
- package/dist/api/feedback.mjs.map +1 -1
- package/dist/api/marketing.js +262 -239
- package/dist/api/marketing.js.map +1 -1
- package/dist/api/marketing.mjs +14 -23
- package/dist/api/marketing.mjs.map +1 -1
- package/dist/auth/helpers.d.mts +57 -15
- package/dist/auth/helpers.d.ts +57 -15
- package/dist/auth/helpers.js +276 -242
- package/dist/auth/helpers.js.map +1 -1
- package/dist/auth/helpers.mjs +9 -9
- package/dist/auth/index.d.mts +68 -47
- package/dist/auth/index.d.ts +68 -47
- package/dist/auth/index.js +242 -221
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/index.mjs +8 -8
- package/dist/auth-client/index.d.mts +1219 -2531
- package/dist/auth-client/index.d.ts +1219 -2531
- package/dist/auth-client/index.js +37 -3
- package/dist/auth-client/index.js.map +1 -1
- package/dist/auth-client/index.mjs +19 -3
- package/dist/auth-client/index.mjs.map +1 -1
- package/dist/{chunk-M6Y2XNJC.mjs → chunk-3BH6BVBQ.mjs} +16 -6
- package/dist/chunk-3BH6BVBQ.mjs.map +1 -0
- package/dist/chunk-D56ENLSX.mjs +6 -0
- package/dist/chunk-D56ENLSX.mjs.map +1 -0
- package/dist/{chunk-HUEDEAY3.mjs → chunk-DCPVEZS7.mjs} +14 -4
- package/dist/chunk-DCPVEZS7.mjs.map +1 -0
- package/dist/{chunk-J5XRJ2GW.mjs → chunk-DWLMCMXF.mjs} +4 -4
- package/dist/{chunk-J5XRJ2GW.mjs.map → chunk-DWLMCMXF.mjs.map} +1 -1
- package/dist/{chunk-AOSJUQQG.mjs → chunk-EXMUANQ3.mjs} +4 -4
- package/dist/{chunk-AOSJUQQG.mjs.map → chunk-EXMUANQ3.mjs.map} +1 -1
- package/dist/{chunk-NOFQCVHC.mjs → chunk-IH6CMZIN.mjs} +195 -215
- package/dist/chunk-IH6CMZIN.mjs.map +1 -0
- package/dist/chunk-LADVCLEQ.mjs +43 -0
- package/dist/chunk-LADVCLEQ.mjs.map +1 -0
- package/dist/{chunk-TMANA7KL.mjs → chunk-NJBRKKLK.mjs} +9 -6
- package/dist/chunk-NJBRKKLK.mjs.map +1 -0
- package/dist/{chunk-JRDMU6I2.mjs → chunk-QDUPSU5O.mjs} +3 -3
- package/dist/chunk-QDUPSU5O.mjs.map +1 -0
- package/dist/{chunk-2BKXG4MG.mjs → chunk-S23GC776.mjs} +38 -27
- package/dist/chunk-S23GC776.mjs.map +1 -0
- package/dist/{chunk-CRDC62BU.mjs → chunk-TIR7KHXL.mjs} +3 -3
- package/dist/{chunk-CRDC62BU.mjs.map → chunk-TIR7KHXL.mjs.map} +1 -1
- package/dist/{chunk-JSB36AY6.mjs → chunk-ZOQHHFYA.mjs} +7 -4
- package/dist/chunk-ZOQHHFYA.mjs.map +1 -0
- package/dist/config/env.d.mts +3 -0
- package/dist/config/env.d.ts +3 -0
- package/dist/config/env.js +12 -2
- package/dist/config/env.js.map +1 -1
- package/dist/config/env.mjs +1 -1
- package/dist/db/index.d.mts +3 -7
- package/dist/db/index.d.ts +3 -7
- package/dist/db/index.js +60 -11
- package/dist/db/index.js.map +1 -1
- package/dist/db/index.mjs +3 -3
- package/dist/db/schema.d.mts +1 -1
- package/dist/db/schema.d.ts +1 -1
- package/dist/db/schema.js +13 -3
- package/dist/db/schema.js.map +1 -1
- package/dist/db/schema.mjs +1 -1
- package/dist/errors.d.mts +2 -1
- package/dist/errors.d.ts +2 -1
- package/dist/errors.js +60 -11
- package/dist/errors.js.map +1 -1
- package/dist/errors.mjs +4 -4
- package/dist/marketing/index.d.mts +4 -3
- package/dist/marketing/index.d.ts +4 -3
- package/dist/marketing/index.js +12 -2
- package/dist/marketing/index.js.map +1 -1
- package/dist/marketing/index.mjs +3 -3
- package/dist/{schema-BPvJz0PE.d.mts → schema-BujGgvAy.d.mts} +36 -3
- package/dist/{schema-BPvJz0PE.d.ts → schema-BujGgvAy.d.ts} +36 -3
- package/dist/sessions/queries.js +60 -11
- package/dist/sessions/queries.js.map +1 -1
- package/dist/sessions/queries.mjs +3 -3
- package/dist/settings/queries.d.mts +3 -2
- package/dist/settings/queries.d.ts +3 -2
- package/dist/settings/queries.js +46 -12
- package/dist/settings/queries.js.map +1 -1
- package/dist/settings/queries.mjs +4 -4
- package/dist/users/queries.d.mts +24 -19
- package/dist/users/queries.d.ts +24 -19
- package/dist/users/queries.js +48 -13
- package/dist/users/queries.js.map +1 -1
- package/dist/users/queries.mjs +8 -6
- package/dist/users/queries.mjs.map +1 -1
- package/lib/auth/client.ts +22 -2
- package/lib/utils/request-info.ts +7 -3
- package/package.json +62 -58
- package/dist/chunk-2BKXG4MG.mjs.map +0 -1
- package/dist/chunk-HUEDEAY3.mjs.map +0 -1
- package/dist/chunk-JRDMU6I2.mjs.map +0 -1
- package/dist/chunk-JSB36AY6.mjs.map +0 -1
- package/dist/chunk-M6Y2XNJC.mjs.map +0 -1
- package/dist/chunk-NOFQCVHC.mjs.map +0 -1
- package/dist/chunk-RLZ7F7ZY.mjs +0 -30
- package/dist/chunk-RLZ7F7ZY.mjs.map +0 -1
- package/dist/chunk-TMANA7KL.mjs.map +0 -1
package/dist/auth/helpers.js
CHANGED
|
@@ -22,6 +22,7 @@ var uaParserJs = require('ua-parser-js');
|
|
|
22
22
|
var geoip = require('geoip-lite');
|
|
23
23
|
var headers = require('next/headers');
|
|
24
24
|
var navigation = require('next/navigation');
|
|
25
|
+
var server = require('next/server');
|
|
25
26
|
|
|
26
27
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
27
28
|
|
|
@@ -47,8 +48,13 @@ var __spreadValues = (a, b) => {
|
|
|
47
48
|
return a;
|
|
48
49
|
};
|
|
49
50
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
50
|
-
var __esm = (fn, res) => function __init() {
|
|
51
|
-
|
|
51
|
+
var __esm = (fn, res, err) => function __init() {
|
|
52
|
+
if (err) throw err[0];
|
|
53
|
+
try {
|
|
54
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
55
|
+
} catch (e) {
|
|
56
|
+
throw err = [e], e;
|
|
57
|
+
}
|
|
52
58
|
};
|
|
53
59
|
var __export = (target, all) => {
|
|
54
60
|
for (var name in all)
|
|
@@ -290,7 +296,10 @@ var sessions = pgCore.pgTable("session", {
|
|
|
290
296
|
// Set by better-auth's admin plugin while this session is an admin
|
|
291
297
|
// impersonating another user; holds the impersonating admin's user id.
|
|
292
298
|
impersonatedBy: pgCore.text("impersonatedBy")
|
|
293
|
-
})
|
|
299
|
+
}, (table) => [
|
|
300
|
+
// Read by userId on every login (session count), ban/delete and the admin sessions view.
|
|
301
|
+
pgCore.index("session_user_idx").on(table.userId)
|
|
302
|
+
]);
|
|
294
303
|
var accounts = pgCore.pgTable("account", {
|
|
295
304
|
id: pgCore.text("id").notNull().primaryKey().$defaultFn(() => cuid2.createId()),
|
|
296
305
|
accountId: pgCore.text("accountId").notNull(),
|
|
@@ -339,7 +348,10 @@ var verifications = pgCore.pgTable("verification", {
|
|
|
339
348
|
});
|
|
340
349
|
var activityLogs = pgCore.pgTable("activity_log", {
|
|
341
350
|
id: pgCore.text("id").notNull().primaryKey().$defaultFn(() => cuid2.createId()),
|
|
342
|
-
|
|
351
|
+
// SET NULL, not cascade: deleting an admin must not erase the audit trail of
|
|
352
|
+
// what they did. actorEmail keeps who it was once the user row is gone.
|
|
353
|
+
userId: pgCore.text("userId").references(() => users.id, { onDelete: "set null" }),
|
|
354
|
+
actorEmail: pgCore.text("actorEmail"),
|
|
343
355
|
action: ActivityActionEnum("action").notNull(),
|
|
344
356
|
description: pgCore.text("description").notNull(),
|
|
345
357
|
ipAddress: pgCore.text("ipAddress"),
|
|
@@ -355,7 +367,11 @@ var activityLogs = pgCore.pgTable("activity_log", {
|
|
|
355
367
|
country: pgCore.text("country"),
|
|
356
368
|
city: pgCore.text("city"),
|
|
357
369
|
createdAt: pgCore.timestamp("createdAt").notNull().defaultNow()
|
|
358
|
-
})
|
|
370
|
+
}, (table) => [
|
|
371
|
+
// Per-user history, and the admin log's newest-first ordering.
|
|
372
|
+
pgCore.index("activity_log_user_created_idx").on(table.userId, table.createdAt),
|
|
373
|
+
pgCore.index("activity_log_created_idx").on(table.createdAt)
|
|
374
|
+
]);
|
|
359
375
|
var deviceFingerprints = pgCore.pgTable("device_fingerprint", {
|
|
360
376
|
id: pgCore.text("id").notNull().primaryKey().$defaultFn(() => cuid2.createId()),
|
|
361
377
|
userId: pgCore.text("userId").notNull().references(() => users.id, { onDelete: "cascade" }),
|
|
@@ -470,7 +486,9 @@ var envSchema = zod.z.object({
|
|
|
470
486
|
RESEND_MARKETING_SEGMENT_ID: zod.z.string().default(""),
|
|
471
487
|
// Sender for every transactional email. Must be on a domain verified in
|
|
472
488
|
// your Resend account.
|
|
473
|
-
|
|
489
|
+
// Resend's shared sandbox sender: works without a verified domain but only
|
|
490
|
+
// delivers to your own Resend account email. Set your verified domain for real use.
|
|
491
|
+
EMAIL_FROM: zod.z.string().default("Acme <onboarding@resend.dev>"),
|
|
474
492
|
// Display name for this deployment — shown as the authenticator-app label
|
|
475
493
|
// (2FA issuer) and the passkey relying-party name. Consuming projects set
|
|
476
494
|
// this in their own .env; defaults to the template name.
|
|
@@ -480,6 +498,9 @@ var envSchema = zod.z.object({
|
|
|
480
498
|
// a comma-separated list and sends through Resend (needs RESEND_API_KEY).
|
|
481
499
|
FEEDBACK_DISCORD_WEBHOOK_URL: zod.z.string().default(""),
|
|
482
500
|
FEEDBACK_EMAIL: zod.z.string().default(""),
|
|
501
|
+
// Seconds between feedback messages per user (default 30, 0 = off). Read
|
|
502
|
+
// straight from process.env in lib/feedback/route.ts; listed here for docs.
|
|
503
|
+
FEEDBACK_COOLDOWN_SECONDS: zod.z.string().default("30"),
|
|
483
504
|
// Web Push (example, see lib/push.ts). Generate keys with
|
|
484
505
|
// `bunx web-push generate-vapid-keys`. Blank = the notifications card is hidden.
|
|
485
506
|
// The public key is also read in the browser as NEXT_PUBLIC_VAPID_PUBLIC_KEY.
|
|
@@ -496,7 +517,12 @@ var envSchema = zod.z.object({
|
|
|
496
517
|
DB_DRIVER: zod.z.enum(["pg", "neon"]).default("pg"),
|
|
497
518
|
// Only relevant when DB_DRIVER=pg — whether the target Postgres server has SSL
|
|
498
519
|
// enabled. Self-hosted Postgres (e.g. via Docker/Dokploy) typically does not.
|
|
499
|
-
DATABASE_SSL: zod.z.string().default("false").transform((val) => val === "true")
|
|
520
|
+
DATABASE_SSL: zod.z.string().default("false").transform((val) => val === "true"),
|
|
521
|
+
// With DATABASE_SSL=true the server certificate is verified. Set the CA (PEM,
|
|
522
|
+
// "\n" for newlines) for a self-signed or private CA; DATABASE_SSL_INSECURE=true
|
|
523
|
+
// skips verification (encrypts only). Both are read in config/pg-ssl.ts.
|
|
524
|
+
DATABASE_SSL_CA: zod.z.string().default(""),
|
|
525
|
+
DATABASE_SSL_INSECURE: zod.z.string().default("false")
|
|
500
526
|
});
|
|
501
527
|
|
|
502
528
|
// config/env.ts
|
|
@@ -510,24 +536,47 @@ if (!results.success) {
|
|
|
510
536
|
}
|
|
511
537
|
var config = results.data;
|
|
512
538
|
|
|
539
|
+
// config/pg-ssl.ts
|
|
540
|
+
function pgSslOptions() {
|
|
541
|
+
var _a;
|
|
542
|
+
if (process.env.DATABASE_SSL_INSECURE === "true") return { rejectUnauthorized: false };
|
|
543
|
+
const ca = (_a = process.env.DATABASE_SSL_CA) == null ? void 0 : _a.replace(/\\n/g, "\n");
|
|
544
|
+
return __spreadValues({ rejectUnauthorized: true }, ca ? { ca } : {});
|
|
545
|
+
}
|
|
546
|
+
|
|
513
547
|
// server/db/index.ts
|
|
514
548
|
var db;
|
|
515
549
|
if (config.DB_DRIVER === "pg") {
|
|
516
550
|
const pool = new pg.Pool({
|
|
517
551
|
connectionString: config.DATABASE_URL,
|
|
518
|
-
ssl: config.DATABASE_SSL ?
|
|
552
|
+
ssl: config.DATABASE_SSL ? pgSslOptions() : void 0,
|
|
553
|
+
// pg waits forever by default; a hung DB would otherwise freeze every request (proxy reads settings).
|
|
554
|
+
connectionTimeoutMillis: 5e3,
|
|
555
|
+
query_timeout: 15e3
|
|
556
|
+
// roomy enough for the 10k-row exports
|
|
519
557
|
});
|
|
520
558
|
db = nodePostgres.drizzle(pool, {
|
|
521
559
|
schema: schema_exports,
|
|
522
|
-
logger:
|
|
560
|
+
logger: process.env.NODE_ENV === "development"
|
|
523
561
|
});
|
|
524
562
|
console.log("\u{1F7E1} Using PostgreSQL (pg) driver");
|
|
525
|
-
} else if (config.DB_DRIVER === "neon") {
|
|
526
|
-
const sql4 = serverless.neon(config.DATABASE_URL);
|
|
527
|
-
db = neonHttp.drizzle(sql4, { schema: schema_exports, logger: true });
|
|
528
|
-
console.log("\u{1F7E2} Using Neon (HTTP) driver");
|
|
529
563
|
} else {
|
|
530
|
-
|
|
564
|
+
serverless.neonConfig.fetchFunction = (url, init) => fetch(url, __spreadProps(__spreadValues({}, init), { signal: AbortSignal.timeout(15e3) }));
|
|
565
|
+
const sql6 = serverless.neon(config.DATABASE_URL);
|
|
566
|
+
db = neonHttp.drizzle(sql6, { schema: schema_exports, logger: process.env.NODE_ENV === "development" });
|
|
567
|
+
console.log("\u{1F7E2} Using Neon (HTTP) driver");
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// node_modules/@better-auth/utils/dist/bytes.mjs
|
|
571
|
+
function toBufferSource(data) {
|
|
572
|
+
if (typeof data === "string") return new TextEncoder().encode(data);
|
|
573
|
+
if (!ArrayBuffer.isView(data)) return data;
|
|
574
|
+
if (data.buffer instanceof ArrayBuffer) return data;
|
|
575
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength).slice();
|
|
576
|
+
}
|
|
577
|
+
function toUint8Array(data) {
|
|
578
|
+
if (typeof data === "string") return new TextEncoder().encode(data);
|
|
579
|
+
return new Uint8Array(data);
|
|
531
580
|
}
|
|
532
581
|
|
|
533
582
|
// node_modules/@better-auth/utils/dist/base32.mjs
|
|
@@ -536,9 +585,7 @@ function getAlphabet(hex2) {
|
|
|
536
585
|
}
|
|
537
586
|
function createDecodeMap(alphabet) {
|
|
538
587
|
const decodeMap = /* @__PURE__ */ new Map();
|
|
539
|
-
for (let i = 0; i < alphabet.length; i++)
|
|
540
|
-
decodeMap.set(alphabet[i], i);
|
|
541
|
-
}
|
|
588
|
+
for (let i = 0; i < alphabet.length; i++) decodeMap.set(alphabet[i], i);
|
|
542
589
|
return decodeMap;
|
|
543
590
|
}
|
|
544
591
|
function base32Encode(data, alphabet, padding) {
|
|
@@ -553,9 +600,7 @@ function base32Encode(data, alphabet, padding) {
|
|
|
553
600
|
result += alphabet[buffer >> shift & 31];
|
|
554
601
|
}
|
|
555
602
|
}
|
|
556
|
-
if (shift > 0)
|
|
557
|
-
result += alphabet[buffer << 5 - shift & 31];
|
|
558
|
-
}
|
|
603
|
+
if (shift > 0) result += alphabet[buffer << 5 - shift & 31];
|
|
559
604
|
if (padding) {
|
|
560
605
|
const padCount = (8 - result.length % 8) % 8;
|
|
561
606
|
result += "=".repeat(padCount);
|
|
@@ -568,12 +613,9 @@ function base32Decode(data, alphabet) {
|
|
|
568
613
|
let buffer = 0;
|
|
569
614
|
let bitsCollected = 0;
|
|
570
615
|
for (const char of data) {
|
|
571
|
-
if (char === "=")
|
|
572
|
-
break;
|
|
616
|
+
if (char === "=") break;
|
|
573
617
|
const value = decodeMap.get(char);
|
|
574
|
-
if (value === void 0) {
|
|
575
|
-
throw new Error(`Invalid Base32 character: ${char}`);
|
|
576
|
-
}
|
|
618
|
+
if (value === void 0) throw new Error(`Invalid Base32 character: ${char}`);
|
|
577
619
|
buffer = buffer << 5 | value;
|
|
578
620
|
bitsCollected += 5;
|
|
579
621
|
while (bitsCollected >= 8) {
|
|
@@ -585,68 +627,34 @@ function base32Decode(data, alphabet) {
|
|
|
585
627
|
}
|
|
586
628
|
var base32 = {
|
|
587
629
|
/**
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
630
|
+
* Encodes data into a Base32 string.
|
|
631
|
+
* @param data - The data to encode (ArrayBuffer, number-based TypedArray, or string).
|
|
632
|
+
* @param options - Encoding options.
|
|
633
|
+
* @returns The Base32 encoded string.
|
|
634
|
+
*/
|
|
593
635
|
encode(data, options = {}) {
|
|
594
636
|
var _a;
|
|
595
637
|
const alphabet = getAlphabet();
|
|
596
|
-
|
|
597
|
-
return base32Encode(buffer, alphabet, (_a = options.padding) != null ? _a : true);
|
|
638
|
+
return base32Encode(toUint8Array(data), alphabet, (_a = options.padding) != null ? _a : true);
|
|
598
639
|
},
|
|
599
640
|
/**
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
641
|
+
* Decodes a Base32 string into a Uint8Array.
|
|
642
|
+
* @param data - The Base32 encoded string or ArrayBuffer/TypedArray.
|
|
643
|
+
* @returns The decoded Uint8Array.
|
|
644
|
+
*/
|
|
604
645
|
decode(data) {
|
|
605
|
-
if (typeof data !== "string")
|
|
606
|
-
data = new TextDecoder().decode(data);
|
|
607
|
-
}
|
|
646
|
+
if (typeof data !== "string") data = new TextDecoder().decode(data);
|
|
608
647
|
const alphabet = getAlphabet();
|
|
609
648
|
return base32Decode(data, alphabet);
|
|
610
649
|
}
|
|
611
650
|
};
|
|
612
651
|
|
|
613
|
-
// node_modules/@better-auth/utils/dist/
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
}
|
|
620
|
-
if (data.byteLength === 0) {
|
|
621
|
-
return "";
|
|
622
|
-
}
|
|
623
|
-
const buffer = new Uint8Array(data);
|
|
624
|
-
let result = "";
|
|
625
|
-
for (const byte of buffer) {
|
|
626
|
-
result += byte.toString(16).padStart(2, "0");
|
|
627
|
-
}
|
|
628
|
-
return result;
|
|
629
|
-
},
|
|
630
|
-
decode: (data) => {
|
|
631
|
-
if (!data) {
|
|
632
|
-
return "";
|
|
633
|
-
}
|
|
634
|
-
if (typeof data === "string") {
|
|
635
|
-
if (data.length % 2 !== 0) {
|
|
636
|
-
throw new Error("Invalid hexadecimal string");
|
|
637
|
-
}
|
|
638
|
-
if (!new RegExp(`^[${hexadecimal}]+$`).test(data)) {
|
|
639
|
-
throw new Error("Invalid hexadecimal string");
|
|
640
|
-
}
|
|
641
|
-
const result = new Uint8Array(data.length / 2);
|
|
642
|
-
for (let i = 0; i < data.length; i += 2) {
|
|
643
|
-
result[i / 2] = parseInt(data.slice(i, i + 2), 16);
|
|
644
|
-
}
|
|
645
|
-
return new TextDecoder().decode(result);
|
|
646
|
-
}
|
|
647
|
-
return new TextDecoder().decode(data);
|
|
648
|
-
}
|
|
649
|
-
};
|
|
652
|
+
// node_modules/@better-auth/utils/dist/index.mjs
|
|
653
|
+
function getWebcryptoSubtle() {
|
|
654
|
+
const cr = typeof globalThis !== "undefined" && globalThis.crypto;
|
|
655
|
+
if (cr && typeof cr.subtle === "object" && cr.subtle != null) return cr.subtle;
|
|
656
|
+
throw new Error("crypto.subtle must be defined");
|
|
657
|
+
}
|
|
650
658
|
|
|
651
659
|
// node_modules/@better-auth/utils/dist/base64.mjs
|
|
652
660
|
function getAlphabet2(urlSafe) {
|
|
@@ -664,9 +672,7 @@ function base64Encode(data, alphabet, padding) {
|
|
|
664
672
|
result += alphabet[buffer >> shift & 63];
|
|
665
673
|
}
|
|
666
674
|
}
|
|
667
|
-
if (shift > 0)
|
|
668
|
-
result += alphabet[buffer << 6 - shift & 63];
|
|
669
|
-
}
|
|
675
|
+
if (shift > 0) result += alphabet[buffer << 6 - shift & 63];
|
|
670
676
|
if (padding) {
|
|
671
677
|
const padCount = (4 - result.length % 4) % 4;
|
|
672
678
|
result += "=".repeat(padCount);
|
|
@@ -675,19 +681,14 @@ function base64Encode(data, alphabet, padding) {
|
|
|
675
681
|
}
|
|
676
682
|
function base64Decode(data, alphabet) {
|
|
677
683
|
const decodeMap = /* @__PURE__ */ new Map();
|
|
678
|
-
for (let i = 0; i < alphabet.length; i++)
|
|
679
|
-
decodeMap.set(alphabet[i], i);
|
|
680
|
-
}
|
|
684
|
+
for (let i = 0; i < alphabet.length; i++) decodeMap.set(alphabet[i], i);
|
|
681
685
|
const result = [];
|
|
682
686
|
let buffer = 0;
|
|
683
687
|
let bitsCollected = 0;
|
|
684
688
|
for (const char of data) {
|
|
685
|
-
if (char === "=")
|
|
686
|
-
break;
|
|
689
|
+
if (char === "=") break;
|
|
687
690
|
const value = decodeMap.get(char);
|
|
688
|
-
if (value === void 0) {
|
|
689
|
-
throw new Error(`Invalid Base64 character: ${char}`);
|
|
690
|
-
}
|
|
691
|
+
if (value === void 0) throw new Error(`Invalid Base64 character: ${char}`);
|
|
691
692
|
buffer = buffer << 6 | value;
|
|
692
693
|
bitsCollected += 6;
|
|
693
694
|
if (bitsCollected >= 8) {
|
|
@@ -701,15 +702,11 @@ var base64 = {
|
|
|
701
702
|
encode(data, options = {}) {
|
|
702
703
|
var _a;
|
|
703
704
|
const alphabet = getAlphabet2(false);
|
|
704
|
-
|
|
705
|
-
return base64Encode(buffer, alphabet, (_a = options.padding) != null ? _a : true);
|
|
705
|
+
return base64Encode(toUint8Array(data), alphabet, (_a = options.padding) != null ? _a : true);
|
|
706
706
|
},
|
|
707
707
|
decode(data) {
|
|
708
|
-
if (typeof data !== "string")
|
|
709
|
-
|
|
710
|
-
}
|
|
711
|
-
const urlSafe = data.includes("-") || data.includes("_");
|
|
712
|
-
const alphabet = getAlphabet2(urlSafe);
|
|
708
|
+
if (typeof data !== "string") data = new TextDecoder().decode(data);
|
|
709
|
+
const alphabet = getAlphabet2(data.includes("-") || data.includes("_"));
|
|
713
710
|
return base64Decode(data, alphabet);
|
|
714
711
|
}
|
|
715
712
|
};
|
|
@@ -717,152 +714,142 @@ var base64Url = {
|
|
|
717
714
|
encode(data, options = {}) {
|
|
718
715
|
var _a;
|
|
719
716
|
const alphabet = getAlphabet2(true);
|
|
720
|
-
|
|
721
|
-
return base64Encode(buffer, alphabet, (_a = options.padding) != null ? _a : true);
|
|
717
|
+
return base64Encode(toUint8Array(data), alphabet, (_a = options.padding) != null ? _a : true);
|
|
722
718
|
},
|
|
723
719
|
decode(data) {
|
|
724
|
-
|
|
725
|
-
const alphabet = getAlphabet2(urlSafe);
|
|
726
|
-
return base64Decode(data, alphabet);
|
|
720
|
+
return base64Decode(data, getAlphabet2(data.includes("-") || data.includes("_")));
|
|
727
721
|
}
|
|
728
722
|
};
|
|
729
723
|
|
|
730
|
-
// node_modules/@
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
724
|
+
// node_modules/@noble/hashes/utils.js
|
|
725
|
+
var hasHexBuiltin = /* @__PURE__ */ (() => (
|
|
726
|
+
// @ts-ignore
|
|
727
|
+
typeof Uint8Array.from([]).toHex === "function" && typeof Uint8Array.fromHex === "function"
|
|
728
|
+
))();
|
|
729
|
+
function asciiToBase16(ch) {
|
|
730
|
+
return ch >= 48 && ch <= 57 ? ch - 48 : ch >= 65 && ch <= 70 ? ch - (65 - 10) : ch >= 97 && ch <= 102 ? ch - (97 - 10) : void 0;
|
|
736
731
|
}
|
|
732
|
+
function hexToBytes(hex2) {
|
|
733
|
+
if (typeof hex2 !== "string")
|
|
734
|
+
throw new TypeError("hex string expected, got " + typeof hex2);
|
|
735
|
+
if (hasHexBuiltin) {
|
|
736
|
+
try {
|
|
737
|
+
return Uint8Array.fromHex(hex2);
|
|
738
|
+
} catch (error) {
|
|
739
|
+
if (error instanceof SyntaxError)
|
|
740
|
+
throw new RangeError(error.message);
|
|
741
|
+
throw error;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
const hl = hex2.length;
|
|
745
|
+
const al = hl / 2;
|
|
746
|
+
if (hl % 2)
|
|
747
|
+
throw new RangeError("hex string expected, got unpadded hex of length " + hl);
|
|
748
|
+
const array = new Uint8Array(al);
|
|
749
|
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
|
750
|
+
const n1 = asciiToBase16(hex2.charCodeAt(hi));
|
|
751
|
+
const n2 = asciiToBase16(hex2.charCodeAt(hi + 1));
|
|
752
|
+
if (n1 === void 0 || n2 === void 0) {
|
|
753
|
+
const char = hex2[hi] + hex2[hi + 1];
|
|
754
|
+
throw new RangeError('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
|
755
|
+
}
|
|
756
|
+
array[ai] = n1 * 16 + n2;
|
|
757
|
+
}
|
|
758
|
+
return array;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// node_modules/@better-auth/utils/dist/hex.mjs
|
|
762
|
+
var toBytes = (data) => hexToBytes(data);
|
|
763
|
+
var hex = {
|
|
764
|
+
encode: (data) => {
|
|
765
|
+
const buffer = toUint8Array(data);
|
|
766
|
+
if (buffer.byteLength === 0) return "";
|
|
767
|
+
let result = "";
|
|
768
|
+
for (const byte of buffer) result += byte.toString(16).padStart(2, "0");
|
|
769
|
+
return result;
|
|
770
|
+
},
|
|
771
|
+
decode: (data) => {
|
|
772
|
+
if (!data) return "";
|
|
773
|
+
if (typeof data === "string") return new TextDecoder().decode(toBytes(data));
|
|
774
|
+
return new TextDecoder().decode(data);
|
|
775
|
+
},
|
|
776
|
+
toBytes
|
|
777
|
+
};
|
|
737
778
|
|
|
738
779
|
// node_modules/@better-auth/utils/dist/hmac.mjs
|
|
739
780
|
var createHMAC = (algorithm = "SHA-256", encoding = "none") => {
|
|
740
|
-
const
|
|
781
|
+
const hmac2 = {
|
|
741
782
|
importKey: async (key, keyUsage) => {
|
|
742
|
-
return getWebcryptoSubtle().importKey(
|
|
743
|
-
"
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
false,
|
|
747
|
-
[keyUsage]
|
|
748
|
-
);
|
|
783
|
+
return getWebcryptoSubtle().importKey("raw", toBufferSource(key), {
|
|
784
|
+
name: "HMAC",
|
|
785
|
+
hash: { name: algorithm }
|
|
786
|
+
}, false, [keyUsage]);
|
|
749
787
|
},
|
|
750
788
|
sign: async (hmacKey, data) => {
|
|
751
|
-
if (typeof hmacKey === "string")
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
hmacKey,
|
|
757
|
-
typeof data === "string" ? new TextEncoder().encode(data) : data
|
|
758
|
-
);
|
|
759
|
-
if (encoding === "hex") {
|
|
760
|
-
return hex.encode(signature);
|
|
761
|
-
}
|
|
762
|
-
if (encoding === "base64" || encoding === "base64url" || encoding === "base64urlnopad") {
|
|
763
|
-
return base64Url.encode(signature, {
|
|
764
|
-
padding: encoding !== "base64urlnopad"
|
|
765
|
-
});
|
|
766
|
-
}
|
|
789
|
+
if (typeof hmacKey === "string") hmacKey = await hmac2.importKey(hmacKey, "sign");
|
|
790
|
+
const signature = await getWebcryptoSubtle().sign("HMAC", hmacKey, toBufferSource(data));
|
|
791
|
+
if (encoding === "hex") return hex.encode(signature);
|
|
792
|
+
if (encoding === "base64") return base64.encode(signature);
|
|
793
|
+
if (encoding === "base64url" || encoding === "base64urlnopad") return base64Url.encode(signature, { padding: encoding !== "base64urlnopad" });
|
|
767
794
|
return signature;
|
|
768
795
|
},
|
|
769
796
|
verify: async (hmacKey, data, signature) => {
|
|
770
|
-
if (typeof hmacKey === "string")
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
signature = hex.decode(signature);
|
|
775
|
-
}
|
|
776
|
-
if (encoding === "base64" || encoding === "base64url" || encoding === "base64urlnopad") {
|
|
777
|
-
signature = await base64.decode(signature);
|
|
778
|
-
}
|
|
779
|
-
return getWebcryptoSubtle().verify(
|
|
780
|
-
"HMAC",
|
|
781
|
-
hmacKey,
|
|
782
|
-
typeof signature === "string" ? new TextEncoder().encode(signature) : signature,
|
|
783
|
-
typeof data === "string" ? new TextEncoder().encode(data) : data
|
|
784
|
-
);
|
|
797
|
+
if (typeof hmacKey === "string") hmacKey = await hmac2.importKey(hmacKey, "verify");
|
|
798
|
+
if (encoding === "hex") signature = typeof signature === "string" ? hex.toBytes(signature) : signature;
|
|
799
|
+
if (encoding === "base64" || encoding === "base64url" || encoding === "base64urlnopad") signature = await base64.decode(signature);
|
|
800
|
+
return getWebcryptoSubtle().verify("HMAC", hmacKey, toBufferSource(signature), toBufferSource(data));
|
|
785
801
|
}
|
|
786
802
|
};
|
|
787
|
-
return
|
|
803
|
+
return hmac2;
|
|
788
804
|
};
|
|
789
805
|
|
|
790
806
|
// node_modules/@better-auth/utils/dist/otp.mjs
|
|
791
807
|
var defaultPeriod = 30;
|
|
792
808
|
var defaultDigits = 6;
|
|
809
|
+
var hmac = createHMAC("SHA-1");
|
|
793
810
|
function constantTimeEqualOTP(input, expected) {
|
|
794
811
|
let difference = input.length ^ expected.length;
|
|
795
|
-
for (let i = 0; i < expected.length; i++)
|
|
796
|
-
difference |= input.charCodeAt(i) ^ expected.charCodeAt(i);
|
|
797
|
-
}
|
|
812
|
+
for (let i = 0; i < expected.length; i++) difference |= input.charCodeAt(i) ^ expected.charCodeAt(i);
|
|
798
813
|
return difference === 0;
|
|
799
814
|
}
|
|
800
|
-
async function generateHOTP(
|
|
801
|
-
counter,
|
|
802
|
-
digits,
|
|
803
|
-
hash = "SHA-1"
|
|
804
|
-
}) {
|
|
815
|
+
async function generateHOTP(key, { counter, digits }) {
|
|
805
816
|
const _digits = digits != null ? digits : defaultDigits;
|
|
806
|
-
if (_digits < 1 || _digits > 8)
|
|
807
|
-
|
|
808
|
-
}
|
|
809
|
-
const buffer = new ArrayBuffer(8);
|
|
817
|
+
if (_digits < 1 || _digits > 8) throw new TypeError("Digits must be between 1 and 8");
|
|
818
|
+
const buffer = /* @__PURE__ */ new ArrayBuffer(8);
|
|
810
819
|
new DataView(buffer).setBigUint64(0, BigInt(counter), false);
|
|
811
820
|
const bytes = new Uint8Array(buffer);
|
|
812
|
-
const
|
|
821
|
+
const signature = await hmac.sign(key, bytes);
|
|
822
|
+
const hmacResult = new Uint8Array(signature);
|
|
813
823
|
const offset = hmacResult[hmacResult.length - 1] & 15;
|
|
814
|
-
|
|
815
|
-
const otp = truncated % 10 ** _digits;
|
|
816
|
-
return otp.toString().padStart(_digits, "0");
|
|
824
|
+
return (((hmacResult[offset] & 127) << 24 | (hmacResult[offset + 1] & 255) << 16 | (hmacResult[offset + 2] & 255) << 8 | hmacResult[offset + 3] & 255) % 10 ** _digits).toString().padStart(_digits, "0");
|
|
817
825
|
}
|
|
818
|
-
async function generateTOTP(
|
|
826
|
+
async function generateTOTP(key, options) {
|
|
819
827
|
var _a, _b;
|
|
820
828
|
const digits = (_a = options == null ? void 0 : options.digits) != null ? _a : defaultDigits;
|
|
821
|
-
const
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
829
|
+
const milliseconds = ((_b = options == null ? void 0 : options.period) != null ? _b : defaultPeriod) * 1e3;
|
|
830
|
+
return await generateHOTP(key, {
|
|
831
|
+
counter: Math.floor(Date.now() / milliseconds),
|
|
832
|
+
digits
|
|
833
|
+
});
|
|
825
834
|
}
|
|
826
|
-
async function verifyTOTP(otp, {
|
|
827
|
-
window = 1,
|
|
828
|
-
digits = defaultDigits,
|
|
829
|
-
secret,
|
|
830
|
-
period = defaultPeriod
|
|
831
|
-
}) {
|
|
835
|
+
async function verifyTOTP(otp, { window = 1, digits = defaultDigits, key, period = defaultPeriod }) {
|
|
832
836
|
const milliseconds = period * 1e3;
|
|
833
837
|
const counter = Math.floor(Date.now() / milliseconds);
|
|
834
838
|
let matched = false;
|
|
835
|
-
for (let i = -window; i <= window; i++) {
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
});
|
|
840
|
-
matched = constantTimeEqualOTP(otp, generatedOTP) || matched;
|
|
841
|
-
}
|
|
839
|
+
for (let i = -window; i <= window; i++) matched = constantTimeEqualOTP(otp, await generateHOTP(key, {
|
|
840
|
+
counter: counter + i,
|
|
841
|
+
digits
|
|
842
|
+
})) || matched;
|
|
842
843
|
return matched;
|
|
843
844
|
}
|
|
844
|
-
function generateQRCode({
|
|
845
|
-
issuer
|
|
846
|
-
account,
|
|
847
|
-
secret,
|
|
848
|
-
digits = defaultDigits,
|
|
849
|
-
period = defaultPeriod
|
|
850
|
-
}) {
|
|
851
|
-
const encodedIssuer = encodeURIComponent(issuer);
|
|
852
|
-
const encodedAccountName = encodeURIComponent(account);
|
|
853
|
-
const baseURI = `otpauth://totp/${encodedIssuer}:${encodedAccountName}`;
|
|
845
|
+
function generateQRCode({ issuer, account, secret, digits = defaultDigits, period = defaultPeriod }) {
|
|
846
|
+
const baseURI = `otpauth://totp/${encodeURIComponent(issuer)}:${encodeURIComponent(account)}`;
|
|
854
847
|
const params = new URLSearchParams({
|
|
855
|
-
secret: base32.encode(secret, {
|
|
856
|
-
padding: false
|
|
857
|
-
}),
|
|
848
|
+
secret: base32.encode(secret, { padding: false }),
|
|
858
849
|
issuer
|
|
859
850
|
});
|
|
860
|
-
if (digits !== void 0)
|
|
861
|
-
|
|
862
|
-
}
|
|
863
|
-
if (period !== void 0) {
|
|
864
|
-
params.set("period", period.toString());
|
|
865
|
-
}
|
|
851
|
+
if (digits !== void 0) params.set("digits", digits.toString());
|
|
852
|
+
if (period !== void 0) params.set("period", period.toString());
|
|
866
853
|
return `${baseURI}?${params.toString()}`;
|
|
867
854
|
}
|
|
868
855
|
var createOTP = (secret, opts) => {
|
|
@@ -870,10 +857,34 @@ var createOTP = (secret, opts) => {
|
|
|
870
857
|
const digits = (_a = void 0 ) != null ? _a : defaultDigits;
|
|
871
858
|
const period = (_b = void 0 ) != null ? _b : defaultPeriod;
|
|
872
859
|
return {
|
|
873
|
-
hotp: (counter) =>
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
860
|
+
hotp: async (counter) => {
|
|
861
|
+
return generateHOTP(await hmac.importKey(secret, "sign"), {
|
|
862
|
+
counter,
|
|
863
|
+
digits
|
|
864
|
+
});
|
|
865
|
+
},
|
|
866
|
+
totp: async () => {
|
|
867
|
+
return generateTOTP(await hmac.importKey(secret, "sign"), {
|
|
868
|
+
digits,
|
|
869
|
+
period
|
|
870
|
+
});
|
|
871
|
+
},
|
|
872
|
+
verify: async (otp, options) => {
|
|
873
|
+
return verifyTOTP(otp, __spreadValues({
|
|
874
|
+
key: await hmac.importKey(secret, "sign"),
|
|
875
|
+
digits,
|
|
876
|
+
period
|
|
877
|
+
}, options));
|
|
878
|
+
},
|
|
879
|
+
url: (issuer, account) => {
|
|
880
|
+
return generateQRCode({
|
|
881
|
+
issuer,
|
|
882
|
+
account,
|
|
883
|
+
secret,
|
|
884
|
+
digits,
|
|
885
|
+
period
|
|
886
|
+
});
|
|
887
|
+
}
|
|
877
888
|
};
|
|
878
889
|
};
|
|
879
890
|
|
|
@@ -950,6 +961,8 @@ async function logActivity({
|
|
|
950
961
|
try {
|
|
951
962
|
await db.insert(activityLogs).values({
|
|
952
963
|
userId,
|
|
964
|
+
// Snapshot the email at write time; survives deletion of the user row.
|
|
965
|
+
actorEmail: drizzleOrm.sql`(select "email" from "user" where "id" = ${userId})`,
|
|
953
966
|
action,
|
|
954
967
|
description,
|
|
955
968
|
ipAddress: ipAddress || null,
|
|
@@ -970,7 +983,7 @@ var countryNames = new Intl.DisplayNames(["en"], { type: "region" });
|
|
|
970
983
|
function getClientIp(headers2) {
|
|
971
984
|
var _a;
|
|
972
985
|
const forwardedFor = headers2 == null ? void 0 : headers2.get("x-forwarded-for");
|
|
973
|
-
if (forwardedFor) return forwardedFor.split(",")
|
|
986
|
+
if (forwardedFor) return forwardedFor.split(",").at(-1).trim();
|
|
974
987
|
return (_a = headers2 == null ? void 0 : headers2.get("x-real-ip")) != null ? _a : null;
|
|
975
988
|
}
|
|
976
989
|
function parseUserAgent(userAgent) {
|
|
@@ -1013,6 +1026,16 @@ function formatLocation({ country, city }) {
|
|
|
1013
1026
|
var LOGIN_REFERRER_COOKIE = "login_referrer";
|
|
1014
1027
|
var DEVICE_FINGERPRINT_HEADER = "x-device-fingerprint";
|
|
1015
1028
|
|
|
1029
|
+
// lib/auth/base-url.ts
|
|
1030
|
+
function getBaseUrl() {
|
|
1031
|
+
const url = process.env.BETTER_AUTH_URL || process.env.NEXTAUTH_URL || process.env.NEXT_PUBLIC_APP_URL || "http://localhost:3000";
|
|
1032
|
+
return url.replace(/\/+$/, "");
|
|
1033
|
+
}
|
|
1034
|
+
function otherActiveAdminExists(excludeIds) {
|
|
1035
|
+
const ids = drizzleOrm.sql.join(excludeIds.map((id) => drizzleOrm.sql`${id}`), drizzleOrm.sql`, `);
|
|
1036
|
+
return drizzleOrm.sql`(select count(*) from (select ${users.id} as id from ${users} where ${users.role} = 'admin' and ${users.banned} = false for update) a where a.id not in (${ids})) > 0`;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1016
1039
|
// lib/auth/throttle-limits.ts
|
|
1017
1040
|
var WINDOW_MS = 15 * 60 * 1e3;
|
|
1018
1041
|
var MAX = 10;
|
|
@@ -1048,8 +1071,10 @@ async function recordDeviceAttempt(fingerprint, meta) {
|
|
|
1048
1071
|
await db.insert(authThrottle).values(__spreadValues({ fingerprint, count: 1, windowStart: now }, seen)).onConflictDoUpdate({
|
|
1049
1072
|
target: authThrottle.fingerprint,
|
|
1050
1073
|
set: __spreadValues({
|
|
1051
|
-
|
|
1052
|
-
|
|
1074
|
+
// ISO strings, not Dates: pg sends a raw Date as server-local time, which a
|
|
1075
|
+
// tz-less `timestamp` column (stored as UTC) misreads on any non-UTC host.
|
|
1076
|
+
count: drizzleOrm.sql`case when ${authThrottle.windowStart} < ${cutoff.toISOString()} then 1 else ${authThrottle.count} + 1 end`,
|
|
1077
|
+
windowStart: drizzleOrm.sql`case when ${authThrottle.windowStart} < ${cutoff.toISOString()} then ${now.toISOString()} else ${authThrottle.windowStart} end`
|
|
1053
1078
|
}, seen)
|
|
1054
1079
|
});
|
|
1055
1080
|
} catch (error) {
|
|
@@ -1068,15 +1093,9 @@ async function pruneStaleRows(now = /* @__PURE__ */ new Date()) {
|
|
|
1068
1093
|
console.error("auth-throttle prune failed", error);
|
|
1069
1094
|
}
|
|
1070
1095
|
}
|
|
1071
|
-
async function clearDeviceThrottle(fingerprint) {
|
|
1072
|
-
try {
|
|
1073
|
-
await db.delete(authThrottle).where(drizzleOrm.eq(authThrottle.fingerprint, fingerprint));
|
|
1074
|
-
} catch (error) {
|
|
1075
|
-
console.error("auth-throttle clear failed", error);
|
|
1076
|
-
}
|
|
1077
|
-
}
|
|
1078
1096
|
var SETTINGS_ROW_ID = "default";
|
|
1079
1097
|
var CACHE_TTL_MS = 1e4;
|
|
1098
|
+
var FAILURE_CACHE_TTL_MS = 5e3;
|
|
1080
1099
|
var cached = null;
|
|
1081
1100
|
function fallbackSettings() {
|
|
1082
1101
|
return {
|
|
@@ -1119,7 +1138,9 @@ async function getSystemSettings() {
|
|
|
1119
1138
|
return value;
|
|
1120
1139
|
} catch (error) {
|
|
1121
1140
|
console.error("Failed to load system settings, failing open:", error);
|
|
1122
|
-
|
|
1141
|
+
const value = fallbackSettings();
|
|
1142
|
+
cached = { value, expiresAt: now + FAILURE_CACHE_TTL_MS };
|
|
1143
|
+
return value;
|
|
1123
1144
|
}
|
|
1124
1145
|
}
|
|
1125
1146
|
async function getEffectiveAuthFlags() {
|
|
@@ -1188,7 +1209,7 @@ async function notifyPasskeyChange(ctx, action, log) {
|
|
|
1188
1209
|
location: formatLocation(lookupGeoLocation(ip)),
|
|
1189
1210
|
ipAddress: ip || "Unknown",
|
|
1190
1211
|
when: (/* @__PURE__ */ new Date()).toUTCString(),
|
|
1191
|
-
manageUrl: `${
|
|
1212
|
+
manageUrl: `${getBaseUrl()}/dashboard/settings`
|
|
1192
1213
|
})
|
|
1193
1214
|
});
|
|
1194
1215
|
} catch (error) {
|
|
@@ -1263,7 +1284,7 @@ function createAuth(overrides = {}) {
|
|
|
1263
1284
|
"No auth provider is enabled \u2014 every social provider is off and emailAndPassword.enabled is false. Enable AUTH_ENABLE_GOOGLE / AUTH_ENABLE_EMAIL_PASSWORD, or pass your own provider via createAuth({ socialProviders: {...} })."
|
|
1264
1285
|
);
|
|
1265
1286
|
}
|
|
1266
|
-
const baseURL =
|
|
1287
|
+
const baseURL = getBaseUrl();
|
|
1267
1288
|
const log = (_a = overrides.logActivity) != null ? _a : logActivity;
|
|
1268
1289
|
const [twoFactorPlugin, twoFactorExtras] = twoFactorPlugins(baseURL, {
|
|
1269
1290
|
issuer: config.APP_NAME,
|
|
@@ -1397,7 +1418,7 @@ function createAuth(overrides = {}) {
|
|
|
1397
1418
|
// available together.
|
|
1398
1419
|
hooks: {
|
|
1399
1420
|
before: api.createAuthMiddleware(async (ctx) => {
|
|
1400
|
-
var _a2, _b2, _c2, _d2;
|
|
1421
|
+
var _a2, _b2, _c2, _d2, _e2;
|
|
1401
1422
|
if (ctx.path === "/sign-in/email" || ctx.path === "/sign-up/email") {
|
|
1402
1423
|
const fingerprint = (_a2 = ctx.headers) == null ? void 0 : _a2.get(DEVICE_FINGERPRINT_HEADER);
|
|
1403
1424
|
const ip = getClientIp(ctx.headers);
|
|
@@ -1431,6 +1452,9 @@ function createAuth(overrides = {}) {
|
|
|
1431
1452
|
}
|
|
1432
1453
|
return;
|
|
1433
1454
|
}
|
|
1455
|
+
if (ctx.request && ((_c2 = ctx.path) == null ? void 0 : _c2.startsWith("/admin/")) && ctx.path !== "/admin/impersonate-user" && ctx.path !== "/admin/stop-impersonating") {
|
|
1456
|
+
throw new api.APIError("FORBIDDEN", { message: "Use the dashboard to manage users." });
|
|
1457
|
+
}
|
|
1434
1458
|
if (ctx.path === "/admin/impersonate-user" && overrides.impersonation === false) {
|
|
1435
1459
|
throw new api.APIError("FORBIDDEN", { message: "Impersonation is turned off." });
|
|
1436
1460
|
}
|
|
@@ -1441,8 +1465,8 @@ function createAuth(overrides = {}) {
|
|
|
1441
1465
|
throw new api.APIError("FORBIDDEN", { message: "Stop impersonating before deleting an account." });
|
|
1442
1466
|
}
|
|
1443
1467
|
if (current2.user.role === "admin") {
|
|
1444
|
-
const [
|
|
1445
|
-
if (
|
|
1468
|
+
const [other] = await db.select({ n: drizzleOrm.sql`1` }).from(users).where(otherActiveAdminExists([current2.user.id])).limit(1);
|
|
1469
|
+
if (!other) {
|
|
1446
1470
|
throw new api.APIError("FORBIDDEN", { message: "You're the only admin. Promote another user to admin first." });
|
|
1447
1471
|
}
|
|
1448
1472
|
}
|
|
@@ -1455,7 +1479,7 @@ function createAuth(overrides = {}) {
|
|
|
1455
1479
|
await log({
|
|
1456
1480
|
userId: current.session.impersonatedBy,
|
|
1457
1481
|
action: "impersonation_stopped",
|
|
1458
|
-
description: `Admin stopped impersonating ${(
|
|
1482
|
+
description: `Admin stopped impersonating ${(_e2 = (_d2 = target[0]) == null ? void 0 : _d2.email) != null ? _e2 : current.session.userId}`,
|
|
1459
1483
|
metadata: { impersonatedUserId: current.session.userId }
|
|
1460
1484
|
});
|
|
1461
1485
|
}
|
|
@@ -1491,8 +1515,6 @@ function createAuth(overrides = {}) {
|
|
|
1491
1515
|
if (failed) {
|
|
1492
1516
|
await Promise.all(keys.map((key) => recordDeviceAttempt(key, __spreadProps(__spreadValues({}, meta), { kind: "signin" }))));
|
|
1493
1517
|
await logFailedLogin(ctx, log);
|
|
1494
|
-
} else {
|
|
1495
|
-
await Promise.all(keys.map((key) => clearDeviceThrottle(key)));
|
|
1496
1518
|
}
|
|
1497
1519
|
})
|
|
1498
1520
|
},
|
|
@@ -1573,9 +1595,35 @@ var auth = new Proxy({}, {
|
|
|
1573
1595
|
return Reflect.getOwnPropertyDescriptor(getDefaultAuth(), prop);
|
|
1574
1596
|
}
|
|
1575
1597
|
});
|
|
1598
|
+
|
|
1599
|
+
// lib/auth/pure.ts
|
|
1600
|
+
function hasRole(userRole, requiredRole) {
|
|
1601
|
+
if (!userRole) return false;
|
|
1602
|
+
if (requiredRole === "admin") {
|
|
1603
|
+
return userRole === "admin";
|
|
1604
|
+
}
|
|
1605
|
+
return userRole === "user" || userRole === "admin" || userRole === "tester";
|
|
1606
|
+
}
|
|
1607
|
+
function normalizeCallbackUrl(value) {
|
|
1608
|
+
if (typeof value !== "string" || !value.trim()) return "/dashboard";
|
|
1609
|
+
const candidate = value.trim();
|
|
1610
|
+
try {
|
|
1611
|
+
const appOrigin = new URL(getBaseUrl()).origin;
|
|
1612
|
+
const callbackUrl = new URL(candidate, appOrigin);
|
|
1613
|
+
if (callbackUrl.origin !== appOrigin) {
|
|
1614
|
+
return "/dashboard";
|
|
1615
|
+
}
|
|
1616
|
+
return `${callbackUrl.pathname}${callbackUrl.search}${callbackUrl.hash}` || "/dashboard";
|
|
1617
|
+
} catch (e) {
|
|
1618
|
+
return "/dashboard";
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
// lib/auth/helpers.ts
|
|
1576
1623
|
var getCurrentSession = react.cache(async () => {
|
|
1577
1624
|
const session = await auth.api.getSession({
|
|
1578
|
-
headers: await headers.headers()
|
|
1625
|
+
headers: await headers.headers(),
|
|
1626
|
+
query: { disableCookieCache: true }
|
|
1579
1627
|
});
|
|
1580
1628
|
return session;
|
|
1581
1629
|
});
|
|
@@ -1594,13 +1642,6 @@ var requireAuth = react.cache(async () => {
|
|
|
1594
1642
|
}
|
|
1595
1643
|
return __spreadProps(__spreadValues({}, session), { user: __spreadProps(__spreadValues({}, session.user), { role: (_c = (_b = user[0]) == null ? void 0 : _b.role) != null ? _c : session.user.role }) });
|
|
1596
1644
|
});
|
|
1597
|
-
function hasRole(userRole, requiredRole) {
|
|
1598
|
-
if (!userRole) return false;
|
|
1599
|
-
if (requiredRole === "admin") {
|
|
1600
|
-
return userRole === "admin";
|
|
1601
|
-
}
|
|
1602
|
-
return userRole === "user" || userRole === "admin" || userRole === "tester";
|
|
1603
|
-
}
|
|
1604
1645
|
async function requireRole(requiredRole) {
|
|
1605
1646
|
const session = await requireAuth();
|
|
1606
1647
|
if (!hasRole(session.user.role, requiredRole)) {
|
|
@@ -1608,24 +1649,17 @@ async function requireRole(requiredRole) {
|
|
|
1608
1649
|
}
|
|
1609
1650
|
return session;
|
|
1610
1651
|
}
|
|
1611
|
-
function
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
const callbackUrl = new URL(candidate, appOrigin);
|
|
1617
|
-
if (callbackUrl.origin !== appOrigin) {
|
|
1618
|
-
return "/dashboard";
|
|
1619
|
-
}
|
|
1620
|
-
return `${callbackUrl.pathname}${callbackUrl.search}${callbackUrl.hash}` || "/dashboard";
|
|
1621
|
-
} catch (e) {
|
|
1622
|
-
return "/dashboard";
|
|
1623
|
-
}
|
|
1652
|
+
async function requireAdminApi() {
|
|
1653
|
+
const session = await auth.api.getSession({ headers: await headers.headers(), query: { disableCookieCache: true } });
|
|
1654
|
+
if (!(session == null ? void 0 : session.user)) return server.NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
1655
|
+
if (!hasRole(session.user.role, "admin")) return server.NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
|
1656
|
+
return session;
|
|
1624
1657
|
}
|
|
1625
1658
|
|
|
1626
1659
|
exports.getCurrentSession = getCurrentSession;
|
|
1627
1660
|
exports.hasRole = hasRole;
|
|
1628
1661
|
exports.normalizeCallbackUrl = normalizeCallbackUrl;
|
|
1662
|
+
exports.requireAdminApi = requireAdminApi;
|
|
1629
1663
|
exports.requireAuth = requireAuth;
|
|
1630
1664
|
exports.requireRole = requireRole;
|
|
1631
1665
|
//# sourceMappingURL=helpers.js.map
|