@strapi-community/plugin-better-auth-dashboard 1.0.0-beta.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -5
- package/dist/admin/{Root-CgVBJuoM.mjs → Root-C-dQhq8o.mjs} +1 -1
- package/dist/admin/{Root-CsiOnzlB.js → Root-CMQ7swi6.js} +1 -1
- package/dist/admin/{index-DvfwvCj0.mjs → index-B5jiojjV.mjs} +1 -1
- package/dist/admin/{index-BCBu-0nr.js → index-Bko3vlk0.js} +4 -4
- package/dist/admin/{index-CPSoAbZt.js → index-CSyPvFnN.js} +6 -4
- package/dist/admin/{index-gFqHlMLP.mjs → index-CvNwHg1k.mjs} +2 -2
- package/dist/admin/{index-D9ttdrT3.js → index-D1nrIz0Y.js} +2 -2
- package/dist/admin/{index-BI2WIsds.mjs → index-DAkkUU0o.mjs} +4 -4
- package/dist/admin/{index-VyHXoQ6p.mjs → index-NF35nlqQ.mjs} +6 -4
- package/dist/admin/{index-DQdn5s8q.js → index-hbz3gTsi.js} +1 -1
- package/dist/admin/index.js +1 -1
- package/dist/admin/index.mjs +1 -1
- package/dist/admin/{useModelSchema-C6bxNjW4.mjs → useModelSchema-Bo8WEBwb.mjs} +1 -1
- package/dist/admin/{useModelSchema-BogQah8f.js → useModelSchema-DON5XCju.js} +1 -1
- package/dist/server/index.js +458 -49
- package/dist/server/index.mjs +459 -50
- package/package.json +8 -2
package/dist/server/index.js
CHANGED
|
@@ -37,8 +37,14 @@ const bootstrap = async ({ strapi: strapi2 }) => {
|
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
39
|
const config = {
|
|
40
|
-
default: {},
|
|
40
|
+
default: { mcp: { enabled: false }, dashApi: { enabled: false } },
|
|
41
41
|
validator(config2) {
|
|
42
|
+
if (config2.mcp !== void 0 && config2.mcp.enabled !== void 0 && typeof config2.mcp.enabled !== "boolean") {
|
|
43
|
+
throw new Error("mcp.enabled must be a boolean");
|
|
44
|
+
}
|
|
45
|
+
if (config2.dashApi !== void 0 && config2.dashApi.enabled !== void 0 && typeof config2.dashApi.enabled !== "boolean") {
|
|
46
|
+
throw new Error("dashApi.enabled must be a boolean");
|
|
47
|
+
}
|
|
42
48
|
if (config2.email_callback_url === void 0) return;
|
|
43
49
|
if (typeof config2.email_callback_url !== "string" || config2.email_callback_url.length === 0) {
|
|
44
50
|
throw new Error("email_callback_url must be a non-empty string");
|
|
@@ -52,60 +58,83 @@ const config = {
|
|
|
52
58
|
};
|
|
53
59
|
const DASHBOARD_API_KEY = process.env.BETTER_AUTH_DASHBOARD_SECRET || "strapi-internal-dashboard-key";
|
|
54
60
|
const DASH_CONTEXT_HEADER = "x-dash-context";
|
|
61
|
+
function decodeDashContextHeader(value) {
|
|
62
|
+
if (typeof value !== "string" || value.length === 0) return {};
|
|
63
|
+
try {
|
|
64
|
+
const parsed = JSON.parse(Buffer.from(value, "base64").toString("utf8"));
|
|
65
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
66
|
+
return parsed;
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
}
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
55
72
|
async function hashApiKey(value) {
|
|
56
73
|
const data = new TextEncoder().encode(value);
|
|
57
74
|
const buf = await crypto.subtle.digest("SHA-256", data);
|
|
58
75
|
return Array.from(new Uint8Array(buf)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
59
76
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
77
|
+
async function signDashToken(auth, extra = {}) {
|
|
78
|
+
const { token } = await auth.api.signJWT({
|
|
79
|
+
body: {
|
|
80
|
+
payload: {
|
|
81
|
+
iat: Math.floor(Date.now() / 1e3),
|
|
82
|
+
apiKeyHash: await hashApiKey(DASHBOARD_API_KEY),
|
|
83
|
+
...extra
|
|
84
|
+
},
|
|
85
|
+
overrideOptions: { jwt: { expirationTime: "5m" } }
|
|
69
86
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
87
|
+
});
|
|
88
|
+
return token;
|
|
89
|
+
}
|
|
90
|
+
function getAuth(strapi2) {
|
|
91
|
+
const auth = strapi2.service("plugin::better-auth.auth-service").getAuth();
|
|
92
|
+
if (!auth) {
|
|
93
|
+
throw new Error("No Better Auth configuration was found.");
|
|
94
|
+
}
|
|
95
|
+
if (!auth.api?.signJWT) {
|
|
96
|
+
throw new Error(
|
|
97
|
+
"The Better Auth jwt() plugin is required for dashboard/MCP features. Add jwt() from 'better-auth/plugins' to your Better Auth configuration."
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
return auth;
|
|
101
|
+
}
|
|
102
|
+
async function getDashApi(strapi2, context = {}) {
|
|
103
|
+
const auth = getAuth(strapi2);
|
|
104
|
+
const token = await signDashToken(auth, context);
|
|
105
|
+
return { auth, headers: new Headers({ Authorization: `Bearer ${token}` }) };
|
|
106
|
+
}
|
|
107
|
+
const dashController = () => ({
|
|
108
|
+
async handleDashRequest(ctx) {
|
|
109
|
+
const path = ctx.params.path;
|
|
110
|
+
if (!path) {
|
|
111
|
+
throw new utils.errors.ValidationError("Missing dash endpoint path.");
|
|
81
112
|
}
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
});
|
|
113
|
+
const auth = getAuth(strapi);
|
|
114
|
+
const token = await signDashToken(
|
|
115
|
+
auth,
|
|
116
|
+
decodeDashContextHeader(ctx.request.headers[DASH_CONTEXT_HEADER])
|
|
117
|
+
);
|
|
89
118
|
const url = new URL(
|
|
90
119
|
ctx.request.url,
|
|
91
120
|
`${ctx.request.protocol}://${ctx.request.host}`
|
|
92
121
|
);
|
|
93
|
-
|
|
94
|
-
url.pathname = `/api/auth/${betterAuthPath}`;
|
|
122
|
+
url.pathname = `/api/auth/dash/${path}`;
|
|
95
123
|
const headers = new Headers();
|
|
96
|
-
Object.entries(ctx.request.headers)
|
|
124
|
+
for (const [key, value] of Object.entries(ctx.request.headers)) {
|
|
97
125
|
if (value) {
|
|
98
126
|
headers.set(key, Array.isArray(value) ? value[0] : String(value));
|
|
99
127
|
}
|
|
100
|
-
}
|
|
128
|
+
}
|
|
101
129
|
headers.set("Authorization", `Bearer ${token}`);
|
|
102
130
|
const hasBody = ctx.request.method !== "GET" && ctx.request.method !== "HEAD";
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
131
|
+
const response = await auth.handler(
|
|
132
|
+
new Request(url.toString(), {
|
|
133
|
+
method: ctx.request.method,
|
|
134
|
+
headers,
|
|
135
|
+
body: hasBody ? JSON.stringify(ctx.request.body) : void 0
|
|
136
|
+
})
|
|
137
|
+
);
|
|
109
138
|
ctx.status = response.status;
|
|
110
139
|
response.headers.forEach((value, key) => {
|
|
111
140
|
ctx.set(key, value);
|
|
@@ -206,7 +235,7 @@ const settingsController = () => ({
|
|
|
206
235
|
}
|
|
207
236
|
});
|
|
208
237
|
const controllers = {
|
|
209
|
-
"
|
|
238
|
+
"dash-controller": dashController,
|
|
210
239
|
"db-controller": dbController,
|
|
211
240
|
"settings-controller": settingsController
|
|
212
241
|
};
|
|
@@ -344,6 +373,373 @@ const ACTIONS = {
|
|
|
344
373
|
}
|
|
345
374
|
]
|
|
346
375
|
};
|
|
376
|
+
const USER_UID = "plugin::better-auth.user";
|
|
377
|
+
const ACTION = {
|
|
378
|
+
OVERVIEW_READ: `plugin::${PLUGIN_ID}.overview.read`,
|
|
379
|
+
USER_READ: `plugin::${PLUGIN_ID}.user.read`,
|
|
380
|
+
USER_UPDATE: `plugin::${PLUGIN_ID}.user.update`
|
|
381
|
+
};
|
|
382
|
+
function getMcp(strapi2) {
|
|
383
|
+
return strapi2.ai?.mcp;
|
|
384
|
+
}
|
|
385
|
+
function userHasAttribute(strapi2, uid, attribute) {
|
|
386
|
+
const map = strapi2.contentTypes;
|
|
387
|
+
return Boolean(map[uid]?.attributes && attribute in map[uid].attributes);
|
|
388
|
+
}
|
|
389
|
+
function toolResult(payload) {
|
|
390
|
+
return {
|
|
391
|
+
content: [{ type: "text", text: JSON.stringify(payload, null, 2) }],
|
|
392
|
+
structuredContent: payload
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
function toolError(message) {
|
|
396
|
+
return { content: [{ type: "text", text: message }], isError: true };
|
|
397
|
+
}
|
|
398
|
+
function dashErrorMessage(e) {
|
|
399
|
+
if (e && typeof e === "object") {
|
|
400
|
+
const anyE = e;
|
|
401
|
+
if (anyE.body?.message) return String(anyE.body.message);
|
|
402
|
+
if (anyE.message) return String(anyE.message);
|
|
403
|
+
}
|
|
404
|
+
return String(e);
|
|
405
|
+
}
|
|
406
|
+
function isNotFoundError(e) {
|
|
407
|
+
const anyE = e;
|
|
408
|
+
return anyE?.statusCode === 404 || anyE?.status === "NOT_FOUND";
|
|
409
|
+
}
|
|
410
|
+
const dashUserOutput = utils.z.object({
|
|
411
|
+
id: utils.z.union([utils.z.string(), utils.z.number()]),
|
|
412
|
+
name: utils.z.string().nullable().optional(),
|
|
413
|
+
email: utils.z.string().nullable().optional(),
|
|
414
|
+
emailVerified: utils.z.boolean().nullable().optional(),
|
|
415
|
+
image: utils.z.string().nullable().optional(),
|
|
416
|
+
role: utils.z.string().nullable().optional(),
|
|
417
|
+
banned: utils.z.boolean().nullable().optional(),
|
|
418
|
+
banReason: utils.z.string().nullable().optional(),
|
|
419
|
+
banExpires: utils.z.union([utils.z.string(), utils.z.number()]).nullable().optional(),
|
|
420
|
+
createdAt: utils.z.union([utils.z.string(), utils.z.number()]).nullable().optional(),
|
|
421
|
+
updatedAt: utils.z.union([utils.z.string(), utils.z.number()]).nullable().optional()
|
|
422
|
+
});
|
|
423
|
+
function mapDashUser(u) {
|
|
424
|
+
return {
|
|
425
|
+
id: u.id,
|
|
426
|
+
name: u.name ?? null,
|
|
427
|
+
email: u.email ?? null,
|
|
428
|
+
emailVerified: u.emailVerified ?? null,
|
|
429
|
+
image: u.image ?? null,
|
|
430
|
+
role: u.role ?? null,
|
|
431
|
+
banned: u.banned ?? null,
|
|
432
|
+
banReason: u.banReason ?? null,
|
|
433
|
+
banExpires: u.banExpires ?? null,
|
|
434
|
+
createdAt: u.createdAt ?? null,
|
|
435
|
+
updatedAt: u.updatedAt ?? null
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
function registerBanTools(mcp) {
|
|
439
|
+
mcp.registerTool({
|
|
440
|
+
name: "better_auth_ban_user",
|
|
441
|
+
title: "Ban a Better Auth user",
|
|
442
|
+
description: "Ban (block) a Better Auth user by id, through the Better Auth dashboard API. Optionally set a reason and an expiry in days; Better Auth revokes the user's sessions.",
|
|
443
|
+
auth: { policies: [{ action: ACTION.USER_UPDATE }] },
|
|
444
|
+
resolveInputSchema: () => utils.z.object({
|
|
445
|
+
userId: utils.z.string().describe("The Better Auth user id"),
|
|
446
|
+
banReason: utils.z.string().optional(),
|
|
447
|
+
banExpiresInDays: utils.z.number().positive().optional().describe(
|
|
448
|
+
"Days from now until the ban expires; omit for a permanent ban"
|
|
449
|
+
)
|
|
450
|
+
}),
|
|
451
|
+
resolveOutputSchema: () => utils.z.object({ banned: utils.z.boolean() }),
|
|
452
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
453
|
+
const body = {};
|
|
454
|
+
if (args.banReason) body.banReason = args.banReason;
|
|
455
|
+
if (args.banExpiresInDays) {
|
|
456
|
+
body.banExpires = Date.now() + args.banExpiresInDays * 864e5;
|
|
457
|
+
}
|
|
458
|
+
try {
|
|
459
|
+
const { auth, headers } = await getDashApi(strapi2, {
|
|
460
|
+
userId: args.userId
|
|
461
|
+
});
|
|
462
|
+
await auth.api.dashBanUser({ body, headers });
|
|
463
|
+
return toolResult({ banned: true });
|
|
464
|
+
} catch (e) {
|
|
465
|
+
return toolError(`Failed to ban user: ${dashErrorMessage(e)}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
mcp.registerTool({
|
|
470
|
+
name: "better_auth_unban_user",
|
|
471
|
+
title: "Unban a Better Auth user",
|
|
472
|
+
description: "Lift the ban on a Better Auth user by id, through the Better Auth dashboard API.",
|
|
473
|
+
auth: { policies: [{ action: ACTION.USER_UPDATE }] },
|
|
474
|
+
resolveInputSchema: () => utils.z.object({ userId: utils.z.string().describe("The Better Auth user id") }),
|
|
475
|
+
resolveOutputSchema: () => utils.z.object({ unbanned: utils.z.boolean() }),
|
|
476
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
477
|
+
try {
|
|
478
|
+
const { auth, headers } = await getDashApi(strapi2, {
|
|
479
|
+
userId: args.userId
|
|
480
|
+
});
|
|
481
|
+
await auth.api.dashUnbanUser({ headers });
|
|
482
|
+
return toolResult({ unbanned: true });
|
|
483
|
+
} catch (e) {
|
|
484
|
+
return toolError(`Failed to unban user: ${dashErrorMessage(e)}`);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
function registerGetUser(mcp) {
|
|
490
|
+
mcp.registerTool({
|
|
491
|
+
name: "better_auth_get_user",
|
|
492
|
+
title: "Get a Better Auth user",
|
|
493
|
+
description: "Fetch a single Better Auth user by its Better Auth user id (use better_auth_list_users to find the id). Returns the user and its session/account counts.",
|
|
494
|
+
auth: { policies: [{ action: ACTION.USER_READ }] },
|
|
495
|
+
resolveInputSchema: () => utils.z.object({
|
|
496
|
+
userId: utils.z.string().describe("The Better Auth user id")
|
|
497
|
+
}),
|
|
498
|
+
resolveOutputSchema: () => utils.z.object({
|
|
499
|
+
found: utils.z.boolean(),
|
|
500
|
+
user: dashUserOutput.nullable(),
|
|
501
|
+
sessionsCount: utils.z.number().optional(),
|
|
502
|
+
accountsCount: utils.z.number().optional()
|
|
503
|
+
}),
|
|
504
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
505
|
+
try {
|
|
506
|
+
const { auth, headers } = await getDashApi(strapi2, {
|
|
507
|
+
userId: args.userId
|
|
508
|
+
});
|
|
509
|
+
const user = await auth.api.getDashUser({ query: {}, headers });
|
|
510
|
+
const raw = user;
|
|
511
|
+
return toolResult({
|
|
512
|
+
found: true,
|
|
513
|
+
user: mapDashUser(user),
|
|
514
|
+
sessionsCount: Array.isArray(raw.session) ? raw.session.length : void 0,
|
|
515
|
+
accountsCount: Array.isArray(raw.account) ? raw.account.length : void 0
|
|
516
|
+
});
|
|
517
|
+
} catch (e) {
|
|
518
|
+
if (isNotFoundError(e)) {
|
|
519
|
+
return toolResult({ found: false, user: null });
|
|
520
|
+
}
|
|
521
|
+
return toolError(`Failed to get user: ${dashErrorMessage(e)}`);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
function registerListUsers(mcp) {
|
|
527
|
+
mcp.registerTool({
|
|
528
|
+
name: "better_auth_list_users",
|
|
529
|
+
title: "List Better Auth users",
|
|
530
|
+
description: "List and search Better Auth users (via the Better Auth dashboard API). `search` matches name or email, case-insensitively. Returns users plus the total count.",
|
|
531
|
+
auth: { policies: [{ action: ACTION.USER_READ }] },
|
|
532
|
+
resolveInputSchema: () => utils.z.object({
|
|
533
|
+
search: utils.z.string().optional().describe("Case-insensitive substring matched against name or email"),
|
|
534
|
+
limit: utils.z.number().int().min(1).max(100).default(25),
|
|
535
|
+
offset: utils.z.number().int().min(0).default(0)
|
|
536
|
+
}),
|
|
537
|
+
resolveOutputSchema: () => utils.z.object({
|
|
538
|
+
total: utils.z.number(),
|
|
539
|
+
count: utils.z.number(),
|
|
540
|
+
onlineUsers: utils.z.number().optional(),
|
|
541
|
+
users: utils.z.array(dashUserOutput)
|
|
542
|
+
}),
|
|
543
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
544
|
+
const { search, limit, offset } = args;
|
|
545
|
+
const where = search ? JSON.stringify([
|
|
546
|
+
{
|
|
547
|
+
field: "email",
|
|
548
|
+
operator: "contains",
|
|
549
|
+
value: search,
|
|
550
|
+
connector: "OR"
|
|
551
|
+
},
|
|
552
|
+
{
|
|
553
|
+
field: "name",
|
|
554
|
+
operator: "contains",
|
|
555
|
+
value: search,
|
|
556
|
+
connector: "OR"
|
|
557
|
+
}
|
|
558
|
+
]) : void 0;
|
|
559
|
+
try {
|
|
560
|
+
const { auth, headers } = await getDashApi(strapi2);
|
|
561
|
+
const result = await auth.api.getDashUsers({
|
|
562
|
+
query: {
|
|
563
|
+
limit,
|
|
564
|
+
offset,
|
|
565
|
+
sortBy: "createdAt",
|
|
566
|
+
sortOrder: "desc",
|
|
567
|
+
...where ? { where } : {}
|
|
568
|
+
},
|
|
569
|
+
headers
|
|
570
|
+
});
|
|
571
|
+
const users = (result.users ?? []).map(
|
|
572
|
+
(u) => mapDashUser(u)
|
|
573
|
+
);
|
|
574
|
+
return toolResult({
|
|
575
|
+
total: result.total ?? users.length,
|
|
576
|
+
count: users.length,
|
|
577
|
+
onlineUsers: result.onlineUsers,
|
|
578
|
+
users
|
|
579
|
+
});
|
|
580
|
+
} catch (e) {
|
|
581
|
+
return toolError(`Failed to list users: ${dashErrorMessage(e)}`);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
function registerSignupsGraph(mcp) {
|
|
587
|
+
mcp.registerTool({
|
|
588
|
+
name: "better_auth_signups_graph",
|
|
589
|
+
title: "Better Auth sign-ups over time",
|
|
590
|
+
description: "Return a time series of Better Auth user sign-ups (via the dashboard API), bucketed by day, week or month. Each point has newUsers, activeUsers and the running totalUsers.",
|
|
591
|
+
auth: { policies: [{ action: ACTION.OVERVIEW_READ }] },
|
|
592
|
+
resolveInputSchema: () => utils.z.object({
|
|
593
|
+
period: utils.z.enum(["daily", "weekly", "monthly"]).default("daily")
|
|
594
|
+
}),
|
|
595
|
+
resolveOutputSchema: () => utils.z.object({
|
|
596
|
+
period: utils.z.string(),
|
|
597
|
+
points: utils.z.array(
|
|
598
|
+
utils.z.object({
|
|
599
|
+
date: utils.z.string(),
|
|
600
|
+
label: utils.z.string(),
|
|
601
|
+
totalUsers: utils.z.number(),
|
|
602
|
+
newUsers: utils.z.number(),
|
|
603
|
+
activeUsers: utils.z.number()
|
|
604
|
+
})
|
|
605
|
+
)
|
|
606
|
+
}),
|
|
607
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
608
|
+
try {
|
|
609
|
+
const { auth, headers } = await getDashApi(strapi2);
|
|
610
|
+
const graph = await auth.api.dashGetUserGraphData({
|
|
611
|
+
query: { period: args.period },
|
|
612
|
+
headers
|
|
613
|
+
});
|
|
614
|
+
return toolResult({
|
|
615
|
+
period: graph.period ?? args.period,
|
|
616
|
+
points: (graph.data ?? []).map((p) => ({
|
|
617
|
+
date: String(p.date ?? ""),
|
|
618
|
+
label: String(p.label ?? ""),
|
|
619
|
+
totalUsers: Number(p.totalUsers ?? 0),
|
|
620
|
+
newUsers: Number(p.newUsers ?? 0),
|
|
621
|
+
activeUsers: Number(p.activeUsers ?? 0)
|
|
622
|
+
}))
|
|
623
|
+
});
|
|
624
|
+
} catch (e) {
|
|
625
|
+
return toolError(`Failed to load graph: ${dashErrorMessage(e)}`);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
function registerUpdateUser(mcp) {
|
|
631
|
+
mcp.registerTool({
|
|
632
|
+
name: "better_auth_update_user",
|
|
633
|
+
title: "Update a Better Auth user",
|
|
634
|
+
description: "Update a Better Auth user's profile (name, email, image, emailVerified) by Better Auth user id, through the Better Auth dashboard API. Use the ban tools for ban state.",
|
|
635
|
+
auth: { policies: [{ action: ACTION.USER_UPDATE }] },
|
|
636
|
+
resolveInputSchema: () => utils.z.object({
|
|
637
|
+
userId: utils.z.string().describe("The Better Auth user id"),
|
|
638
|
+
name: utils.z.string().optional(),
|
|
639
|
+
email: utils.z.string().email().optional(),
|
|
640
|
+
image: utils.z.string().optional(),
|
|
641
|
+
emailVerified: utils.z.boolean().optional()
|
|
642
|
+
}),
|
|
643
|
+
resolveOutputSchema: () => utils.z.object({
|
|
644
|
+
updated: utils.z.boolean(),
|
|
645
|
+
user: dashUserOutput.nullable()
|
|
646
|
+
}),
|
|
647
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
648
|
+
const body = {};
|
|
649
|
+
if (args.name !== void 0) body.name = args.name;
|
|
650
|
+
if (args.email !== void 0) body.email = args.email;
|
|
651
|
+
if (args.image !== void 0) body.image = args.image;
|
|
652
|
+
if (args.emailVerified !== void 0)
|
|
653
|
+
body.emailVerified = args.emailVerified;
|
|
654
|
+
if (Object.keys(body).length === 0) {
|
|
655
|
+
return toolError("No fields to update were provided.");
|
|
656
|
+
}
|
|
657
|
+
try {
|
|
658
|
+
const { auth, headers } = await getDashApi(strapi2, {
|
|
659
|
+
userId: args.userId
|
|
660
|
+
});
|
|
661
|
+
const user = await auth.api.updateDashUser({ body, headers });
|
|
662
|
+
return toolResult({
|
|
663
|
+
updated: true,
|
|
664
|
+
user: mapDashUser(user)
|
|
665
|
+
});
|
|
666
|
+
} catch (e) {
|
|
667
|
+
return toolError(`Failed to update user: ${dashErrorMessage(e)}`);
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
const signupPeriod = utils.z.object({
|
|
673
|
+
signUps: utils.z.number().nullable(),
|
|
674
|
+
percentage: utils.z.number().nullable()
|
|
675
|
+
});
|
|
676
|
+
const activePeriod = utils.z.object({
|
|
677
|
+
active: utils.z.number().nullable(),
|
|
678
|
+
percentage: utils.z.number().nullable()
|
|
679
|
+
});
|
|
680
|
+
function registerUserStats(mcp) {
|
|
681
|
+
mcp.registerTool({
|
|
682
|
+
name: "better_auth_user_stats",
|
|
683
|
+
title: "Better Auth user statistics",
|
|
684
|
+
description: "Return Better Auth user statistics (via the dashboard API): total users, sign-ups in the last day/week/month with percentage change vs the previous period, and active users per period.",
|
|
685
|
+
auth: { policies: [{ action: ACTION.OVERVIEW_READ }] },
|
|
686
|
+
resolveOutputSchema: () => utils.z.object({
|
|
687
|
+
total: utils.z.number().nullable(),
|
|
688
|
+
daily: signupPeriod,
|
|
689
|
+
weekly: signupPeriod,
|
|
690
|
+
monthly: signupPeriod,
|
|
691
|
+
activeUsers: utils.z.object({
|
|
692
|
+
daily: activePeriod,
|
|
693
|
+
weekly: activePeriod,
|
|
694
|
+
monthly: activePeriod
|
|
695
|
+
}),
|
|
696
|
+
degraded: utils.z.boolean().optional()
|
|
697
|
+
}),
|
|
698
|
+
createHandler: (strapi2) => async () => {
|
|
699
|
+
try {
|
|
700
|
+
const { auth, headers } = await getDashApi(strapi2);
|
|
701
|
+
const stats = await auth.api.dashGetUserStats({ headers });
|
|
702
|
+
return toolResult(stats);
|
|
703
|
+
} catch (e) {
|
|
704
|
+
return toolError(`Failed to load stats: ${dashErrorMessage(e)}`);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
function registerMcpTools(strapi2) {
|
|
710
|
+
const mcp = getMcp(strapi2);
|
|
711
|
+
if (!mcp) {
|
|
712
|
+
strapi2.log.warn(
|
|
713
|
+
"[@strapi-community/plugin-better-auth-dashboard] MCP integration is enabled but this Strapi version has no MCP server (requires >= 5.47). Skipping."
|
|
714
|
+
);
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
registerListUsers(mcp);
|
|
718
|
+
registerGetUser(mcp);
|
|
719
|
+
registerUpdateUser(mcp);
|
|
720
|
+
registerUserStats(mcp);
|
|
721
|
+
registerSignupsGraph(mcp);
|
|
722
|
+
if (userHasAttribute(strapi2, USER_UID, "banned")) {
|
|
723
|
+
registerBanTools(mcp);
|
|
724
|
+
}
|
|
725
|
+
strapi2.log.info(
|
|
726
|
+
"[@strapi-community/plugin-better-auth-dashboard] Registered Better Auth MCP tools."
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
const HTTP_METHODS = ["GET", "POST"];
|
|
730
|
+
const contentApiRoutes = {
|
|
731
|
+
type: "content-api",
|
|
732
|
+
routes: HTTP_METHODS.map((method) => ({
|
|
733
|
+
method,
|
|
734
|
+
path: "/better-auth-dashboard/dash/:path*",
|
|
735
|
+
handler: "dash-controller.handleDashRequest",
|
|
736
|
+
info: { pluginName: "better-auth-dashboard" },
|
|
737
|
+
config: {
|
|
738
|
+
policies: [],
|
|
739
|
+
prefix: ""
|
|
740
|
+
}
|
|
741
|
+
}))
|
|
742
|
+
};
|
|
347
743
|
const register = ({ strapi: strapi2 }) => {
|
|
348
744
|
const actionsToRegister = [...ACTIONS.overview, ...ACTIONS.user];
|
|
349
745
|
if (strapi2.plugin("better-auth").contentTypes?.organization)
|
|
@@ -351,14 +747,27 @@ const register = ({ strapi: strapi2 }) => {
|
|
|
351
747
|
strapi2.admin.services.permission.actionProvider.registerMany(
|
|
352
748
|
actionsToRegister
|
|
353
749
|
);
|
|
750
|
+
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`, {});
|
|
751
|
+
if (config2.dashApi?.enabled) {
|
|
752
|
+
strapi2.server.routes(contentApiRoutes);
|
|
753
|
+
}
|
|
754
|
+
if (config2.mcp?.enabled) {
|
|
755
|
+
try {
|
|
756
|
+
registerMcpTools(strapi2);
|
|
757
|
+
} catch (error) {
|
|
758
|
+
strapi2.log.warn(
|
|
759
|
+
`[@strapi-community/plugin-better-auth-dashboard] Failed to register MCP tools: ${error instanceof Error ? error.message : String(error)}`
|
|
760
|
+
);
|
|
761
|
+
}
|
|
762
|
+
}
|
|
354
763
|
};
|
|
355
764
|
const adminRoutes = () => ({
|
|
356
765
|
type: "admin",
|
|
357
766
|
routes: [
|
|
358
767
|
{
|
|
359
768
|
method: "GET",
|
|
360
|
-
path: "/auth/:path*",
|
|
361
|
-
handler: "
|
|
769
|
+
path: "/auth/dash/:path*",
|
|
770
|
+
handler: "dash-controller.handleDashRequest",
|
|
362
771
|
config: {
|
|
363
772
|
policies: ["has-permission"],
|
|
364
773
|
prefix: ""
|
|
@@ -366,8 +775,8 @@ const adminRoutes = () => ({
|
|
|
366
775
|
},
|
|
367
776
|
{
|
|
368
777
|
method: "POST",
|
|
369
|
-
path: "/auth/:path*",
|
|
370
|
-
handler: "
|
|
778
|
+
path: "/auth/dash/:path*",
|
|
779
|
+
handler: "dash-controller.handleDashRequest",
|
|
371
780
|
config: {
|
|
372
781
|
policies: ["has-permission"],
|
|
373
782
|
prefix: ""
|
|
@@ -375,8 +784,8 @@ const adminRoutes = () => ({
|
|
|
375
784
|
},
|
|
376
785
|
{
|
|
377
786
|
method: "PUT",
|
|
378
|
-
path: "/auth/:path*",
|
|
379
|
-
handler: "
|
|
787
|
+
path: "/auth/dash/:path*",
|
|
788
|
+
handler: "dash-controller.handleDashRequest",
|
|
380
789
|
config: {
|
|
381
790
|
policies: ["has-permission"],
|
|
382
791
|
prefix: ""
|
|
@@ -384,8 +793,8 @@ const adminRoutes = () => ({
|
|
|
384
793
|
},
|
|
385
794
|
{
|
|
386
795
|
method: "PATCH",
|
|
387
|
-
path: "/auth/:path*",
|
|
388
|
-
handler: "
|
|
796
|
+
path: "/auth/dash/:path*",
|
|
797
|
+
handler: "dash-controller.handleDashRequest",
|
|
389
798
|
config: {
|
|
390
799
|
policies: ["has-permission"],
|
|
391
800
|
prefix: ""
|
|
@@ -393,8 +802,8 @@ const adminRoutes = () => ({
|
|
|
393
802
|
},
|
|
394
803
|
{
|
|
395
804
|
method: "DELETE",
|
|
396
|
-
path: "/auth/:path*",
|
|
397
|
-
handler: "
|
|
805
|
+
path: "/auth/dash/:path*",
|
|
806
|
+
handler: "dash-controller.handleDashRequest",
|
|
398
807
|
config: {
|
|
399
808
|
policies: ["has-permission"],
|
|
400
809
|
prefix: ""
|