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