qarpeo 0.1.32 → 0.1.33
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/chunks/wrangler.mjs +1 -1
- package/dist/module.mjs +12 -0
- package/dist/runtime/app/composables/organization.d.ts +3 -3
- package/dist/runtime/server/utils/auth.d.ts +55 -0
- package/dist/runtime/server/utils/auth.mjs +78 -0
- package/dist/runtime/server.d.ts +1 -0
- package/dist/runtime/server.mjs +5 -0
- package/dist/runtime/types/auth.d.ts +12 -1
- package/package.json +1 -1
package/dist/chunks/wrangler.mjs
CHANGED
|
@@ -9,7 +9,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
9
9
|
import { mkdir, writeFile, readFile } from 'node:fs/promises';
|
|
10
10
|
import { defu } from 'defu';
|
|
11
11
|
|
|
12
|
-
const version = "0.1.
|
|
12
|
+
const version = "0.1.33";
|
|
13
13
|
|
|
14
14
|
function scanSchemaExports(schemaFiles) {
|
|
15
15
|
const types = [];
|
package/dist/module.mjs
CHANGED
|
@@ -3399,6 +3399,18 @@ export default async function (batch: any, env?: any, ctx?: any) {
|
|
|
3399
3399
|
name: "ensureAuthTables",
|
|
3400
3400
|
from: resolver.resolve("./runtime/server/utils/auth")
|
|
3401
3401
|
},
|
|
3402
|
+
{
|
|
3403
|
+
name: "resolveUserRole",
|
|
3404
|
+
from: resolver.resolve("./runtime/server/utils/auth")
|
|
3405
|
+
},
|
|
3406
|
+
{
|
|
3407
|
+
name: "checkIfExistsBeforeCreate",
|
|
3408
|
+
from: resolver.resolve("./runtime/server/utils/auth")
|
|
3409
|
+
},
|
|
3410
|
+
{
|
|
3411
|
+
name: "getRoleFromEnv",
|
|
3412
|
+
from: resolver.resolve("./runtime/server/utils/auth")
|
|
3413
|
+
},
|
|
3402
3414
|
// Audit log utilities
|
|
3403
3415
|
{
|
|
3404
3416
|
name: "ensureAuditTables",
|
|
@@ -36,7 +36,7 @@ export declare function useOrganization(): {
|
|
|
36
36
|
name?: string | null | undefined;
|
|
37
37
|
avatarUrl?: string | null | undefined;
|
|
38
38
|
emailVerified?: boolean | undefined;
|
|
39
|
-
role?: string | undefined;
|
|
39
|
+
role?: (import("../..").QarpeoRole | string) | undefined;
|
|
40
40
|
} | undefined;
|
|
41
41
|
email?: string | undefined;
|
|
42
42
|
name?: string | undefined;
|
|
@@ -58,7 +58,7 @@ export declare function useOrganization(): {
|
|
|
58
58
|
name?: string | null | undefined;
|
|
59
59
|
avatarUrl?: string | null | undefined;
|
|
60
60
|
emailVerified?: boolean | undefined;
|
|
61
|
-
role?: string | undefined;
|
|
61
|
+
role?: (import("../..").QarpeoRole | string) | undefined;
|
|
62
62
|
} | undefined;
|
|
63
63
|
email?: string | undefined;
|
|
64
64
|
name?: string | undefined;
|
|
@@ -113,7 +113,7 @@ export declare function useOrganization(): {
|
|
|
113
113
|
name?: string | null | undefined;
|
|
114
114
|
avatarUrl?: string | null | undefined;
|
|
115
115
|
emailVerified?: boolean | undefined;
|
|
116
|
-
role?: string | undefined;
|
|
116
|
+
role?: (import("../..").QarpeoRole | string) | undefined;
|
|
117
117
|
} | undefined;
|
|
118
118
|
email?: string | undefined;
|
|
119
119
|
name?: string | undefined;
|
|
@@ -73,3 +73,58 @@ export declare function resolveMfaStatus(d1: any, user: {
|
|
|
73
73
|
mfaRequired: boolean;
|
|
74
74
|
mfaVerified: boolean;
|
|
75
75
|
}>;
|
|
76
|
+
/**
|
|
77
|
+
* Role tiers recognized by Qarpeo role resolution:
|
|
78
|
+
* - 'super': Full system & tenant control, owner of the instance
|
|
79
|
+
* - 'admin': Administrative operations (content, users, settings)
|
|
80
|
+
* - 'moderator': Moderation capabilities (comments, reviews, submissions)
|
|
81
|
+
* - 'developer': Technical observer (analytics, logs, error monitoring, read-only diagnostic)
|
|
82
|
+
* - 'user': Standard user / member
|
|
83
|
+
*/
|
|
84
|
+
export type QarpeoRole = "super" | "admin" | "moderator" | "developer" | "user";
|
|
85
|
+
export interface ResolveUserRoleResult {
|
|
86
|
+
/** Whether the user already exists in the database */
|
|
87
|
+
exists: boolean;
|
|
88
|
+
/** The suggested role derived from QARPEO_*_EMAILS env vars (defaults to 'user') */
|
|
89
|
+
suggestedRole: QarpeoRole;
|
|
90
|
+
/** The user's current role in the database, if they exist */
|
|
91
|
+
currentRole?: string | null;
|
|
92
|
+
/** The user record from the database, if found */
|
|
93
|
+
user?: any | null;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Resolves the configured role for an email address from environment variables:
|
|
97
|
+
* - `QARPEO_SUPER_EMAILS` -> 'super'
|
|
98
|
+
* - `QARPEO_ADMIN_EMAILS` -> 'admin'
|
|
99
|
+
* - `QARPEO_MODERATOR_EMAILS` -> 'moderator'
|
|
100
|
+
* - `QARPEO_DEVELOPER_EMAILS` -> 'developer'
|
|
101
|
+
* - Default -> 'user'
|
|
102
|
+
*
|
|
103
|
+
* Checks both Cloudflare Worker env (via event context or globalThis) and process.env.
|
|
104
|
+
* Supports both `(event, email)` (standard Nuxt/Nitro convention) and `(email, event?)`.
|
|
105
|
+
*/
|
|
106
|
+
export declare function getRoleFromEnv(event: any, email: string): QarpeoRole;
|
|
107
|
+
export declare function getRoleFromEnv(email: string, event?: any): QarpeoRole;
|
|
108
|
+
/**
|
|
109
|
+
* Preflight utility to inspect user registration status and suggested role before creating an account.
|
|
110
|
+
*
|
|
111
|
+
* Informs developers whether the email already exists in D1 and what role it maps to
|
|
112
|
+
* according to environment configuration (QARPEO_*_EMAILS).
|
|
113
|
+
*
|
|
114
|
+
* Developers retain full programmatic control over role assignment and user creation.
|
|
115
|
+
* Supports both `(event, email)` (standard Nuxt/Nitro convention) and `(email, event?)`.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* const { exists, suggestedRole } = await resolveUserRole(event, email)
|
|
120
|
+
* if (!exists) {
|
|
121
|
+
* await createUser({ email, role: suggestedRole })
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
export declare function resolveUserRole(event: any, email: string): Promise<ResolveUserRoleResult>;
|
|
126
|
+
export declare function resolveUserRole(email: string, event?: any): Promise<ResolveUserRoleResult>;
|
|
127
|
+
/**
|
|
128
|
+
* Alias for resolveUserRole.
|
|
129
|
+
*/
|
|
130
|
+
export declare const checkIfExistsBeforeCreate: typeof resolveUserRole;
|
|
@@ -524,3 +524,81 @@ export async function resolveMfaStatus(d1, user, authConfig = {}) {
|
|
|
524
524
|
mfaVerified: !required
|
|
525
525
|
};
|
|
526
526
|
}
|
|
527
|
+
function parseEmailList(raw) {
|
|
528
|
+
if (!raw || typeof raw !== "string") return [];
|
|
529
|
+
return raw.split(",").map((e) => e.trim().toLowerCase()).filter(Boolean);
|
|
530
|
+
}
|
|
531
|
+
export function getRoleFromEnv(arg1, arg2) {
|
|
532
|
+
let event;
|
|
533
|
+
let email;
|
|
534
|
+
if (typeof arg1 === "string") {
|
|
535
|
+
email = arg1;
|
|
536
|
+
event = arg2;
|
|
537
|
+
} else {
|
|
538
|
+
event = arg1;
|
|
539
|
+
email = typeof arg2 === "string" ? arg2 : "";
|
|
540
|
+
}
|
|
541
|
+
const normalizedEmail = (email || "").trim().toLowerCase();
|
|
542
|
+
if (!normalizedEmail) return "user";
|
|
543
|
+
const cfEnv = event?.context?.cloudflare?.env || globalThis.__qarpeo_env__ || {};
|
|
544
|
+
const getVal = (key) => {
|
|
545
|
+
if (cfEnv[key] !== void 0 && cfEnv[key] !== null) return String(cfEnv[key]);
|
|
546
|
+
if (typeof process !== "undefined" && process?.env && process.env[key] !== void 0) {
|
|
547
|
+
return String(process.env[key]);
|
|
548
|
+
}
|
|
549
|
+
return "";
|
|
550
|
+
};
|
|
551
|
+
const superEmails = parseEmailList(getVal("QARPEO_SUPER_EMAILS"));
|
|
552
|
+
if (superEmails.includes(normalizedEmail)) return "super";
|
|
553
|
+
const adminEmails = parseEmailList(getVal("QARPEO_ADMIN_EMAILS"));
|
|
554
|
+
if (adminEmails.includes(normalizedEmail)) return "admin";
|
|
555
|
+
const moderatorEmails = parseEmailList(getVal("QARPEO_MODERATOR_EMAILS"));
|
|
556
|
+
if (moderatorEmails.includes(normalizedEmail)) return "moderator";
|
|
557
|
+
const developerEmails = parseEmailList(getVal("QARPEO_DEVELOPER_EMAILS"));
|
|
558
|
+
if (developerEmails.includes(normalizedEmail)) return "developer";
|
|
559
|
+
return "user";
|
|
560
|
+
}
|
|
561
|
+
export async function resolveUserRole(arg1, arg2) {
|
|
562
|
+
let event;
|
|
563
|
+
let email;
|
|
564
|
+
if (typeof arg1 === "string") {
|
|
565
|
+
email = arg1;
|
|
566
|
+
event = arg2;
|
|
567
|
+
} else {
|
|
568
|
+
event = arg1;
|
|
569
|
+
email = typeof arg2 === "string" ? arg2 : "";
|
|
570
|
+
}
|
|
571
|
+
const normalizedEmail = (email || "").trim().toLowerCase();
|
|
572
|
+
const suggestedRole = getRoleFromEnv(event, normalizedEmail);
|
|
573
|
+
if (!normalizedEmail) {
|
|
574
|
+
return {
|
|
575
|
+
exists: false,
|
|
576
|
+
suggestedRole: "user",
|
|
577
|
+
currentRole: null,
|
|
578
|
+
user: null
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
let exists = false;
|
|
582
|
+
let currentRole = null;
|
|
583
|
+
let user = null;
|
|
584
|
+
try {
|
|
585
|
+
const bindingName = globalThis.process?.env?.QARPEO_DEFAULT_DB_BINDING || event?.context?.cloudflare?.env?.QARPEO_DEFAULT_DB_BINDING || "DB";
|
|
586
|
+
const d1 = getD1Binding(bindingName, event);
|
|
587
|
+
if (d1) {
|
|
588
|
+
const existingUser = await d1.prepare("SELECT * FROM user WHERE LOWER(email) = LOWER(?) LIMIT 1").bind(normalizedEmail).first();
|
|
589
|
+
if (existingUser) {
|
|
590
|
+
exists = true;
|
|
591
|
+
currentRole = existingUser.role ? String(existingUser.role) : "user";
|
|
592
|
+
user = existingUser;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
} catch {
|
|
596
|
+
}
|
|
597
|
+
return {
|
|
598
|
+
exists,
|
|
599
|
+
suggestedRole,
|
|
600
|
+
currentRole,
|
|
601
|
+
user
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
export const checkIfExistsBeforeCreate = resolveUserRole;
|
package/dist/runtime/server.d.ts
CHANGED
package/dist/runtime/server.mjs
CHANGED
|
@@ -23,3 +23,8 @@ export * from "./bots.mjs";
|
|
|
23
23
|
export * from "./rag.mjs";
|
|
24
24
|
export * from "./server/utils/string.mjs";
|
|
25
25
|
export * from "./server/utils/audit.mjs";
|
|
26
|
+
export {
|
|
27
|
+
resolveUserRole,
|
|
28
|
+
checkIfExistsBeforeCreate,
|
|
29
|
+
getRoleFromEnv
|
|
30
|
+
} from "./server/utils/auth.mjs";
|
|
@@ -1,10 +1,21 @@
|
|
|
1
|
+
export type QarpeoRole = "super" | "admin" | "moderator" | "developer" | "user";
|
|
2
|
+
export interface ResolveUserRoleResult {
|
|
3
|
+
/** Whether the user already exists in the database */
|
|
4
|
+
exists: boolean;
|
|
5
|
+
/** The suggested role derived from QARPEO_*_EMAILS env vars (defaults to 'user') */
|
|
6
|
+
suggestedRole: QarpeoRole;
|
|
7
|
+
/** The user's current role in the database, if they exist */
|
|
8
|
+
currentRole?: string | null;
|
|
9
|
+
/** The user record from the database, if found */
|
|
10
|
+
user?: any | null;
|
|
11
|
+
}
|
|
1
12
|
export interface QarpeoUser {
|
|
2
13
|
id: string;
|
|
3
14
|
email: string;
|
|
4
15
|
name?: string | null;
|
|
5
16
|
avatarUrl?: string | null;
|
|
6
17
|
emailVerified?: boolean;
|
|
7
|
-
role?: string;
|
|
18
|
+
role?: QarpeoRole | string;
|
|
8
19
|
[key: string]: any;
|
|
9
20
|
}
|
|
10
21
|
export interface QarpeoOrganization {
|