@strapi-community/plugin-better-auth-dashboard 1.0.0 → 1.1.1
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 -2
- 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 +468 -49
- package/dist/server/index.mjs +469 -50
- package/package.json +7 -1
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,383 @@ 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 normalizeDashDate(value) {
|
|
424
|
+
if (value == null) return null;
|
|
425
|
+
if (value instanceof Date) return value.toISOString();
|
|
426
|
+
if (typeof value === "string" || typeof value === "number") return value;
|
|
427
|
+
const maybe = value;
|
|
428
|
+
if (typeof maybe.toISOString === "function") return maybe.toISOString();
|
|
429
|
+
return String(value);
|
|
430
|
+
}
|
|
431
|
+
function mapDashUser(u) {
|
|
432
|
+
return {
|
|
433
|
+
id: u.id,
|
|
434
|
+
name: u.name ?? null,
|
|
435
|
+
email: u.email ?? null,
|
|
436
|
+
emailVerified: u.emailVerified ?? null,
|
|
437
|
+
image: u.image ?? null,
|
|
438
|
+
role: u.role ?? null,
|
|
439
|
+
banned: u.banned ?? null,
|
|
440
|
+
banReason: u.banReason ?? null,
|
|
441
|
+
banExpires: normalizeDashDate(u.banExpires),
|
|
442
|
+
createdAt: normalizeDashDate(u.createdAt),
|
|
443
|
+
updatedAt: normalizeDashDate(u.updatedAt)
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
function registerBanTools(mcp) {
|
|
447
|
+
mcp.registerTool({
|
|
448
|
+
name: "better_auth_ban_user",
|
|
449
|
+
title: "Ban a Better Auth user",
|
|
450
|
+
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.",
|
|
451
|
+
auth: { policies: [{ action: ACTION.USER_UPDATE }] },
|
|
452
|
+
resolveInputSchema: () => utils.z.object({
|
|
453
|
+
userId: utils.z.string().describe("The Better Auth user id"),
|
|
454
|
+
banReason: utils.z.string().optional(),
|
|
455
|
+
banExpiresInDays: utils.z.number().positive().optional().describe(
|
|
456
|
+
"Days from now until the ban expires; omit for a permanent ban"
|
|
457
|
+
)
|
|
458
|
+
}),
|
|
459
|
+
resolveOutputSchema: () => utils.z.object({ banned: utils.z.boolean() }),
|
|
460
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
461
|
+
const body = {};
|
|
462
|
+
if (args.banReason) body.banReason = args.banReason;
|
|
463
|
+
if (args.banExpiresInDays) {
|
|
464
|
+
body.banExpires = Date.now() + args.banExpiresInDays * 864e5;
|
|
465
|
+
}
|
|
466
|
+
try {
|
|
467
|
+
const { auth, headers } = await getDashApi(strapi2, {
|
|
468
|
+
userId: args.userId
|
|
469
|
+
});
|
|
470
|
+
await auth.api.dashBanUser({ body, headers });
|
|
471
|
+
return toolResult({ banned: true });
|
|
472
|
+
} catch (e) {
|
|
473
|
+
return toolError(`Failed to ban user: ${dashErrorMessage(e)}`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
mcp.registerTool({
|
|
478
|
+
name: "better_auth_unban_user",
|
|
479
|
+
title: "Unban a Better Auth user",
|
|
480
|
+
description: "Lift the ban on a Better Auth user by id, through the Better Auth dashboard API.",
|
|
481
|
+
auth: { policies: [{ action: ACTION.USER_UPDATE }] },
|
|
482
|
+
resolveInputSchema: () => utils.z.object({ userId: utils.z.string().describe("The Better Auth user id") }),
|
|
483
|
+
resolveOutputSchema: () => utils.z.object({ unbanned: utils.z.boolean() }),
|
|
484
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
485
|
+
try {
|
|
486
|
+
const { auth, headers } = await getDashApi(strapi2, {
|
|
487
|
+
userId: args.userId
|
|
488
|
+
});
|
|
489
|
+
await auth.api.dashUnbanUser({ headers });
|
|
490
|
+
return toolResult({ unbanned: true });
|
|
491
|
+
} catch (e) {
|
|
492
|
+
return toolError(`Failed to unban user: ${dashErrorMessage(e)}`);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
function registerGetUser(mcp) {
|
|
498
|
+
mcp.registerTool({
|
|
499
|
+
name: "better_auth_get_user",
|
|
500
|
+
title: "Get a Better Auth user",
|
|
501
|
+
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.",
|
|
502
|
+
auth: { policies: [{ action: ACTION.USER_READ }] },
|
|
503
|
+
resolveInputSchema: () => utils.z.object({
|
|
504
|
+
userId: utils.z.string().describe("The Better Auth user id")
|
|
505
|
+
}),
|
|
506
|
+
resolveOutputSchema: () => utils.z.object({
|
|
507
|
+
found: utils.z.boolean(),
|
|
508
|
+
user: dashUserOutput.nullable(),
|
|
509
|
+
sessionsCount: utils.z.number().optional(),
|
|
510
|
+
accountsCount: utils.z.number().optional()
|
|
511
|
+
}),
|
|
512
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
513
|
+
try {
|
|
514
|
+
const { auth, headers } = await getDashApi(strapi2, {
|
|
515
|
+
userId: args.userId
|
|
516
|
+
});
|
|
517
|
+
const user = await auth.api.getDashUser({ query: {}, headers });
|
|
518
|
+
const raw = user;
|
|
519
|
+
return toolResult({
|
|
520
|
+
found: true,
|
|
521
|
+
user: mapDashUser(user),
|
|
522
|
+
sessionsCount: Array.isArray(raw.session) ? raw.session.length : void 0,
|
|
523
|
+
accountsCount: Array.isArray(raw.account) ? raw.account.length : void 0
|
|
524
|
+
});
|
|
525
|
+
} catch (e) {
|
|
526
|
+
if (isNotFoundError(e)) {
|
|
527
|
+
return toolResult({ found: false, user: null });
|
|
528
|
+
}
|
|
529
|
+
return toolError(`Failed to get user: ${dashErrorMessage(e)}`);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
function registerListUsers(mcp) {
|
|
535
|
+
mcp.registerTool({
|
|
536
|
+
name: "better_auth_list_users",
|
|
537
|
+
title: "List Better Auth users",
|
|
538
|
+
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.",
|
|
539
|
+
auth: { policies: [{ action: ACTION.USER_READ }] },
|
|
540
|
+
resolveInputSchema: () => utils.z.object({
|
|
541
|
+
search: utils.z.string().optional().describe("Case-insensitive substring matched against name or email"),
|
|
542
|
+
limit: utils.z.number().int().min(1).max(100).default(25),
|
|
543
|
+
offset: utils.z.number().int().min(0).default(0)
|
|
544
|
+
}),
|
|
545
|
+
resolveOutputSchema: () => utils.z.object({
|
|
546
|
+
total: utils.z.number(),
|
|
547
|
+
count: utils.z.number(),
|
|
548
|
+
onlineUsers: utils.z.number().optional(),
|
|
549
|
+
users: utils.z.array(dashUserOutput)
|
|
550
|
+
}),
|
|
551
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
552
|
+
const { search, limit, offset } = args;
|
|
553
|
+
const where = search ? JSON.stringify([
|
|
554
|
+
{
|
|
555
|
+
field: "email",
|
|
556
|
+
operator: "contains",
|
|
557
|
+
value: search,
|
|
558
|
+
connector: "OR"
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
field: "name",
|
|
562
|
+
operator: "contains",
|
|
563
|
+
value: search,
|
|
564
|
+
connector: "OR"
|
|
565
|
+
}
|
|
566
|
+
]) : void 0;
|
|
567
|
+
try {
|
|
568
|
+
const { auth, headers } = await getDashApi(strapi2);
|
|
569
|
+
const result = await auth.api.getDashUsers({
|
|
570
|
+
query: {
|
|
571
|
+
limit,
|
|
572
|
+
offset,
|
|
573
|
+
sortBy: "createdAt",
|
|
574
|
+
sortOrder: "desc",
|
|
575
|
+
...where ? { where } : {}
|
|
576
|
+
},
|
|
577
|
+
headers
|
|
578
|
+
});
|
|
579
|
+
const users = (result.users ?? []).map(
|
|
580
|
+
(u) => mapDashUser(u)
|
|
581
|
+
);
|
|
582
|
+
return toolResult({
|
|
583
|
+
total: result.total ?? users.length,
|
|
584
|
+
count: users.length,
|
|
585
|
+
onlineUsers: result.onlineUsers,
|
|
586
|
+
users
|
|
587
|
+
});
|
|
588
|
+
} catch (e) {
|
|
589
|
+
return toolError(`Failed to list users: ${dashErrorMessage(e)}`);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
function registerSignupsGraph(mcp) {
|
|
595
|
+
mcp.registerTool({
|
|
596
|
+
name: "better_auth_signups_graph",
|
|
597
|
+
title: "Better Auth sign-ups over time",
|
|
598
|
+
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.",
|
|
599
|
+
auth: { policies: [{ action: ACTION.OVERVIEW_READ }] },
|
|
600
|
+
resolveInputSchema: () => utils.z.object({
|
|
601
|
+
period: utils.z.enum(["daily", "weekly", "monthly"]).default("daily")
|
|
602
|
+
}),
|
|
603
|
+
resolveOutputSchema: () => utils.z.object({
|
|
604
|
+
period: utils.z.string(),
|
|
605
|
+
points: utils.z.array(
|
|
606
|
+
utils.z.object({
|
|
607
|
+
date: utils.z.string(),
|
|
608
|
+
label: utils.z.string(),
|
|
609
|
+
totalUsers: utils.z.number(),
|
|
610
|
+
newUsers: utils.z.number(),
|
|
611
|
+
activeUsers: utils.z.number()
|
|
612
|
+
})
|
|
613
|
+
)
|
|
614
|
+
}),
|
|
615
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
616
|
+
try {
|
|
617
|
+
const { auth, headers } = await getDashApi(strapi2);
|
|
618
|
+
const graph = await auth.api.dashGetUserGraphData({
|
|
619
|
+
query: { period: args.period },
|
|
620
|
+
headers
|
|
621
|
+
});
|
|
622
|
+
return toolResult({
|
|
623
|
+
period: graph.period ?? args.period,
|
|
624
|
+
points: (graph.data ?? []).map((p) => ({
|
|
625
|
+
// dash() returns `date` as a JS Date; emit ISO (machine-readable)
|
|
626
|
+
// rather than the locale `String(Date)` form ("Tue Jun 16 2026 …").
|
|
627
|
+
date: String(normalizeDashDate(p.date) ?? ""),
|
|
628
|
+
label: String(p.label ?? ""),
|
|
629
|
+
totalUsers: Number(p.totalUsers ?? 0),
|
|
630
|
+
newUsers: Number(p.newUsers ?? 0),
|
|
631
|
+
activeUsers: Number(p.activeUsers ?? 0)
|
|
632
|
+
}))
|
|
633
|
+
});
|
|
634
|
+
} catch (e) {
|
|
635
|
+
return toolError(`Failed to load graph: ${dashErrorMessage(e)}`);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
function registerUpdateUser(mcp) {
|
|
641
|
+
mcp.registerTool({
|
|
642
|
+
name: "better_auth_update_user",
|
|
643
|
+
title: "Update a Better Auth user",
|
|
644
|
+
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.",
|
|
645
|
+
auth: { policies: [{ action: ACTION.USER_UPDATE }] },
|
|
646
|
+
resolveInputSchema: () => utils.z.object({
|
|
647
|
+
userId: utils.z.string().describe("The Better Auth user id"),
|
|
648
|
+
name: utils.z.string().optional(),
|
|
649
|
+
email: utils.z.string().email().optional(),
|
|
650
|
+
image: utils.z.string().optional(),
|
|
651
|
+
emailVerified: utils.z.boolean().optional()
|
|
652
|
+
}),
|
|
653
|
+
resolveOutputSchema: () => utils.z.object({
|
|
654
|
+
updated: utils.z.boolean(),
|
|
655
|
+
user: dashUserOutput.nullable()
|
|
656
|
+
}),
|
|
657
|
+
createHandler: (strapi2) => async ({ args }) => {
|
|
658
|
+
const body = {};
|
|
659
|
+
if (args.name !== void 0) body.name = args.name;
|
|
660
|
+
if (args.email !== void 0) body.email = args.email;
|
|
661
|
+
if (args.image !== void 0) body.image = args.image;
|
|
662
|
+
if (args.emailVerified !== void 0)
|
|
663
|
+
body.emailVerified = args.emailVerified;
|
|
664
|
+
if (Object.keys(body).length === 0) {
|
|
665
|
+
return toolError("No fields to update were provided.");
|
|
666
|
+
}
|
|
667
|
+
try {
|
|
668
|
+
const { auth, headers } = await getDashApi(strapi2, {
|
|
669
|
+
userId: args.userId
|
|
670
|
+
});
|
|
671
|
+
const user = await auth.api.updateDashUser({ body, headers });
|
|
672
|
+
return toolResult({
|
|
673
|
+
updated: true,
|
|
674
|
+
user: mapDashUser(user)
|
|
675
|
+
});
|
|
676
|
+
} catch (e) {
|
|
677
|
+
return toolError(`Failed to update user: ${dashErrorMessage(e)}`);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
const signupPeriod = utils.z.object({
|
|
683
|
+
signUps: utils.z.number().nullable(),
|
|
684
|
+
percentage: utils.z.number().nullable()
|
|
685
|
+
});
|
|
686
|
+
const activePeriod = utils.z.object({
|
|
687
|
+
active: utils.z.number().nullable(),
|
|
688
|
+
percentage: utils.z.number().nullable()
|
|
689
|
+
});
|
|
690
|
+
function registerUserStats(mcp) {
|
|
691
|
+
mcp.registerTool({
|
|
692
|
+
name: "better_auth_user_stats",
|
|
693
|
+
title: "Better Auth user statistics",
|
|
694
|
+
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.",
|
|
695
|
+
auth: { policies: [{ action: ACTION.OVERVIEW_READ }] },
|
|
696
|
+
resolveOutputSchema: () => utils.z.object({
|
|
697
|
+
total: utils.z.number().nullable(),
|
|
698
|
+
daily: signupPeriod,
|
|
699
|
+
weekly: signupPeriod,
|
|
700
|
+
monthly: signupPeriod,
|
|
701
|
+
activeUsers: utils.z.object({
|
|
702
|
+
daily: activePeriod,
|
|
703
|
+
weekly: activePeriod,
|
|
704
|
+
monthly: activePeriod
|
|
705
|
+
}),
|
|
706
|
+
degraded: utils.z.boolean().optional()
|
|
707
|
+
}),
|
|
708
|
+
createHandler: (strapi2) => async () => {
|
|
709
|
+
try {
|
|
710
|
+
const { auth, headers } = await getDashApi(strapi2);
|
|
711
|
+
const stats = await auth.api.dashGetUserStats({ headers });
|
|
712
|
+
return toolResult(stats);
|
|
713
|
+
} catch (e) {
|
|
714
|
+
return toolError(`Failed to load stats: ${dashErrorMessage(e)}`);
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
function registerMcpTools(strapi2) {
|
|
720
|
+
const mcp = getMcp(strapi2);
|
|
721
|
+
if (!mcp) {
|
|
722
|
+
strapi2.log.warn(
|
|
723
|
+
"[@strapi-community/plugin-better-auth-dashboard] MCP integration is enabled but this Strapi version has no MCP server (requires >= 5.47). Skipping."
|
|
724
|
+
);
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
registerListUsers(mcp);
|
|
728
|
+
registerGetUser(mcp);
|
|
729
|
+
registerUpdateUser(mcp);
|
|
730
|
+
registerUserStats(mcp);
|
|
731
|
+
registerSignupsGraph(mcp);
|
|
732
|
+
if (userHasAttribute(strapi2, USER_UID, "banned")) {
|
|
733
|
+
registerBanTools(mcp);
|
|
734
|
+
}
|
|
735
|
+
strapi2.log.info(
|
|
736
|
+
"[@strapi-community/plugin-better-auth-dashboard] Registered Better Auth MCP tools."
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
const HTTP_METHODS = ["GET", "POST"];
|
|
740
|
+
const contentApiRoutes = {
|
|
741
|
+
type: "content-api",
|
|
742
|
+
routes: HTTP_METHODS.map((method) => ({
|
|
743
|
+
method,
|
|
744
|
+
path: "/better-auth-dashboard/dash/:path*",
|
|
745
|
+
handler: "dash-controller.handleDashRequest",
|
|
746
|
+
info: { pluginName: "better-auth-dashboard" },
|
|
747
|
+
config: {
|
|
748
|
+
policies: [],
|
|
749
|
+
prefix: ""
|
|
750
|
+
}
|
|
751
|
+
}))
|
|
752
|
+
};
|
|
347
753
|
const register = ({ strapi: strapi2 }) => {
|
|
348
754
|
const actionsToRegister = [...ACTIONS.overview, ...ACTIONS.user];
|
|
349
755
|
if (strapi2.plugin("better-auth").contentTypes?.organization)
|
|
@@ -351,14 +757,27 @@ const register = ({ strapi: strapi2 }) => {
|
|
|
351
757
|
strapi2.admin.services.permission.actionProvider.registerMany(
|
|
352
758
|
actionsToRegister
|
|
353
759
|
);
|
|
760
|
+
const config2 = strapi2.config.get(`plugin::${PLUGIN_ID}`, {});
|
|
761
|
+
if (config2.dashApi?.enabled) {
|
|
762
|
+
strapi2.server.routes(contentApiRoutes);
|
|
763
|
+
}
|
|
764
|
+
if (config2.mcp?.enabled) {
|
|
765
|
+
try {
|
|
766
|
+
registerMcpTools(strapi2);
|
|
767
|
+
} catch (error) {
|
|
768
|
+
strapi2.log.warn(
|
|
769
|
+
`[@strapi-community/plugin-better-auth-dashboard] Failed to register MCP tools: ${error instanceof Error ? error.message : String(error)}`
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
354
773
|
};
|
|
355
774
|
const adminRoutes = () => ({
|
|
356
775
|
type: "admin",
|
|
357
776
|
routes: [
|
|
358
777
|
{
|
|
359
778
|
method: "GET",
|
|
360
|
-
path: "/auth/:path*",
|
|
361
|
-
handler: "
|
|
779
|
+
path: "/auth/dash/:path*",
|
|
780
|
+
handler: "dash-controller.handleDashRequest",
|
|
362
781
|
config: {
|
|
363
782
|
policies: ["has-permission"],
|
|
364
783
|
prefix: ""
|
|
@@ -366,8 +785,8 @@ const adminRoutes = () => ({
|
|
|
366
785
|
},
|
|
367
786
|
{
|
|
368
787
|
method: "POST",
|
|
369
|
-
path: "/auth/:path*",
|
|
370
|
-
handler: "
|
|
788
|
+
path: "/auth/dash/:path*",
|
|
789
|
+
handler: "dash-controller.handleDashRequest",
|
|
371
790
|
config: {
|
|
372
791
|
policies: ["has-permission"],
|
|
373
792
|
prefix: ""
|
|
@@ -375,8 +794,8 @@ const adminRoutes = () => ({
|
|
|
375
794
|
},
|
|
376
795
|
{
|
|
377
796
|
method: "PUT",
|
|
378
|
-
path: "/auth/:path*",
|
|
379
|
-
handler: "
|
|
797
|
+
path: "/auth/dash/:path*",
|
|
798
|
+
handler: "dash-controller.handleDashRequest",
|
|
380
799
|
config: {
|
|
381
800
|
policies: ["has-permission"],
|
|
382
801
|
prefix: ""
|
|
@@ -384,8 +803,8 @@ const adminRoutes = () => ({
|
|
|
384
803
|
},
|
|
385
804
|
{
|
|
386
805
|
method: "PATCH",
|
|
387
|
-
path: "/auth/:path*",
|
|
388
|
-
handler: "
|
|
806
|
+
path: "/auth/dash/:path*",
|
|
807
|
+
handler: "dash-controller.handleDashRequest",
|
|
389
808
|
config: {
|
|
390
809
|
policies: ["has-permission"],
|
|
391
810
|
prefix: ""
|
|
@@ -393,8 +812,8 @@ const adminRoutes = () => ({
|
|
|
393
812
|
},
|
|
394
813
|
{
|
|
395
814
|
method: "DELETE",
|
|
396
|
-
path: "/auth/:path*",
|
|
397
|
-
handler: "
|
|
815
|
+
path: "/auth/dash/:path*",
|
|
816
|
+
handler: "dash-controller.handleDashRequest",
|
|
398
817
|
config: {
|
|
399
818
|
policies: ["has-permission"],
|
|
400
819
|
prefix: ""
|