@eutobias/serenojs 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +642 -2
- package/admin-dist/assets/index-B3EgqtT6.js +9 -0
- package/admin-dist/assets/index-D2m0Eb4k.css +2 -0
- package/admin-dist/index.html +17 -0
- package/dist/auth/auth.server.d.ts +31 -0
- package/dist/auth/auth.server.js +141 -0
- package/dist/auth/config.d.ts +32 -0
- package/dist/auth/config.js +25 -0
- package/dist/auth/cookies.server.d.ts +7 -0
- package/dist/auth/cookies.server.js +65 -0
- package/dist/auth/http.server.d.ts +8 -0
- package/dist/auth/http.server.js +30 -0
- package/dist/auth/password.server.d.ts +2 -0
- package/dist/auth/password.server.js +21 -0
- package/dist/auth/session.server.d.ts +2 -0
- package/dist/auth/session.server.js +14 -0
- package/dist/auth/users.server.d.ts +16 -0
- package/dist/auth/users.server.js +123 -0
- package/dist/config.d.ts +91 -0
- package/dist/config.js +39 -0
- package/dist/core/backup.server.d.ts +52 -0
- package/dist/core/backup.server.js +349 -0
- package/dist/core/db.server.d.ts +24 -0
- package/dist/core/db.server.js +526 -0
- package/dist/core/media.d.ts +22 -0
- package/dist/core/media.js +76 -0
- package/dist/core/media.server.d.ts +18 -0
- package/dist/core/media.server.js +102 -0
- package/dist/core/schema.d.ts +26 -0
- package/dist/core/schema.js +99 -0
- package/dist/core/types.d.ts +143 -0
- package/dist/core/types.js +1 -0
- package/dist/core/validation.d.ts +16 -0
- package/dist/core/validation.js +192 -0
- package/dist/locale.d.ts +2 -0
- package/dist/locale.js +14 -0
- package/dist/runtime.d.ts +8 -0
- package/dist/runtime.js +37 -0
- package/dist/server/app.d.ts +20 -0
- package/dist/server/app.js +661 -0
- package/dist/ui/admin/admin-shell.d.ts +16 -0
- package/dist/ui/admin/admin-shell.js +38 -0
- package/dist/ui/admin/backup-manager.d.ts +5 -0
- package/dist/ui/admin/backup-manager.js +165 -0
- package/dist/ui/admin/collection-icon.d.ts +22 -0
- package/dist/ui/admin/collection-icon.js +34 -0
- package/dist/ui/admin/collection-manager.d.ts +7 -0
- package/dist/ui/admin/collection-manager.js +212 -0
- package/dist/ui/admin/display.d.ts +7 -0
- package/dist/ui/admin/display.js +65 -0
- package/dist/ui/admin/field-control.d.ts +14 -0
- package/dist/ui/admin/field-control.js +58 -0
- package/dist/ui/admin/i18n.d.ts +27 -0
- package/dist/ui/admin/i18n.js +324 -0
- package/dist/ui/admin/item-form.d.ts +20 -0
- package/dist/ui/admin/item-form.js +60 -0
- package/dist/ui/admin/list-control.d.ts +13 -0
- package/dist/ui/admin/list-control.js +31 -0
- package/dist/ui/admin/locales.d.ts +4 -0
- package/dist/ui/admin/locales.js +8 -0
- package/dist/ui/admin/login-form.d.ts +3 -0
- package/dist/ui/admin/login-form.js +52 -0
- package/dist/ui/admin/media-control.d.ts +14 -0
- package/dist/ui/admin/media-control.js +74 -0
- package/dist/ui/admin/media-list-control.d.ts +14 -0
- package/dist/ui/admin/media-list-control.js +91 -0
- package/dist/ui/admin/pagination.d.ts +11 -0
- package/dist/ui/admin/pagination.js +14 -0
- package/dist/ui/admin/relation-control.d.ts +14 -0
- package/dist/ui/admin/relation-control.js +33 -0
- package/dist/ui/admin/router.d.ts +17 -0
- package/dist/ui/admin/router.js +58 -0
- package/dist/ui/admin/runtime.d.ts +28 -0
- package/dist/ui/admin/runtime.js +29 -0
- package/dist/ui/admin/sidebar.d.ts +13 -0
- package/dist/ui/admin/sidebar.js +29 -0
- package/dist/ui/admin/standalone-manager.d.ts +11 -0
- package/dist/ui/admin/standalone-manager.js +51 -0
- package/dist/ui/admin/table.d.ts +21 -0
- package/dist/ui/admin/table.js +23 -0
- package/dist/ui/admin/toast.d.ts +16 -0
- package/dist/ui/admin/toast.js +59 -0
- package/dist/ui/admin/users-manager.d.ts +6 -0
- package/dist/ui/admin/users-manager.js +157 -0
- package/dist/ui/badge.d.ts +9 -0
- package/dist/ui/badge.js +14 -0
- package/dist/ui/box.d.ts +17 -0
- package/dist/ui/box.js +14 -0
- package/dist/ui/button.d.ts +25 -0
- package/dist/ui/button.js +28 -0
- package/dist/ui/card.d.ts +45 -0
- package/dist/ui/card.js +49 -0
- package/dist/ui/container.d.ts +13 -0
- package/dist/ui/container.js +13 -0
- package/dist/ui/drawer.d.ts +15 -0
- package/dist/ui/drawer.js +31 -0
- package/dist/ui/field-wrapper.d.ts +15 -0
- package/dist/ui/field-wrapper.js +11 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.js +8 -0
- package/dist/ui/text.d.ts +19 -0
- package/dist/ui/text.js +40 -0
- package/package.json +81 -3
- package/src/admin/app.tsx +371 -0
- package/src/admin/index.html +16 -0
- package/src/admin/main.tsx +42 -0
- package/src/admin/styles.css +9 -0
- package/src/auth/auth.server.ts +203 -0
- package/src/auth/config.ts +46 -0
- package/src/auth/cookies.server.ts +84 -0
- package/src/auth/http.server.ts +32 -0
- package/src/auth/password.server.ts +25 -0
- package/src/auth/session.server.ts +16 -0
- package/src/auth/users.server.ts +159 -0
- package/src/config.ts +120 -0
- package/src/core/backup.server.ts +446 -0
- package/src/core/db.server.ts +685 -0
- package/src/core/media.server.ts +136 -0
- package/src/core/media.ts +91 -0
- package/src/core/schema.ts +126 -0
- package/src/core/types.ts +194 -0
- package/src/core/validation.ts +215 -0
- package/src/global.d.ts +1 -0
- package/src/locale.ts +16 -0
- package/src/runtime.ts +54 -0
- package/src/server/app.ts +737 -0
- package/src/ui/admin/admin-shell.tsx +106 -0
- package/src/ui/admin/backup-manager.tsx +327 -0
- package/src/ui/admin/collection-icon.tsx +69 -0
- package/src/ui/admin/collection-manager.tsx +408 -0
- package/src/ui/admin/display.tsx +138 -0
- package/src/ui/admin/field-control.tsx +214 -0
- package/src/ui/admin/i18n.tsx +401 -0
- package/src/ui/admin/item-form.tsx +200 -0
- package/src/ui/admin/list-control.tsx +121 -0
- package/src/ui/admin/locales.ts +12 -0
- package/src/ui/admin/login-form.tsx +98 -0
- package/src/ui/admin/media-control.tsx +161 -0
- package/src/ui/admin/media-list-control.tsx +242 -0
- package/src/ui/admin/pagination.tsx +58 -0
- package/src/ui/admin/relation-control.tsx +142 -0
- package/src/ui/admin/router.tsx +105 -0
- package/src/ui/admin/runtime.tsx +58 -0
- package/src/ui/admin/sidebar.tsx +178 -0
- package/src/ui/admin/standalone-manager.tsx +103 -0
- package/src/ui/admin/table.tsx +129 -0
- package/src/ui/admin/toast.tsx +126 -0
- package/src/ui/admin/users-manager.tsx +344 -0
- package/src/ui/badge.tsx +27 -0
- package/src/ui/box.tsx +33 -0
- package/src/ui/button.tsx +77 -0
- package/src/ui/card.tsx +92 -0
- package/src/ui/container.tsx +28 -0
- package/src/ui/drawer.tsx +104 -0
- package/src/ui/field-wrapper.tsx +54 -0
- package/src/ui/index.ts +8 -0
- package/src/ui/text.tsx +93 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useCallback, useEffect, useState } from "react";
|
|
4
|
+
|
|
5
|
+
import { Badge } from "../ui/badge.js";
|
|
6
|
+
import { Box } from "../ui/box.js";
|
|
7
|
+
import { Text } from "../ui/text.js";
|
|
8
|
+
import type { AuthUser, UserSummary } from "../auth/config.js";
|
|
9
|
+
import type { CollectionDef, Item, ListResult, RelationOptionsMap } from "../core/types.js";
|
|
10
|
+
import type { BackupInfo } from "../core/backup.server.js";
|
|
11
|
+
import { AdminShell } from "../ui/admin/admin-shell.js";
|
|
12
|
+
import { CollectionIcon } from "../ui/admin/collection-icon.js";
|
|
13
|
+
import { CollectionManager } from "../ui/admin/collection-manager.js";
|
|
14
|
+
import { StandaloneManager } from "../ui/admin/standalone-manager.js";
|
|
15
|
+
import { UsersManager } from "../ui/admin/users-manager.js";
|
|
16
|
+
import { BackupManager } from "../ui/admin/backup-manager.js";
|
|
17
|
+
import { LoginForm } from "../ui/admin/login-form.js";
|
|
18
|
+
import { Link, useRouter } from "../ui/admin/router.js";
|
|
19
|
+
import { useCms, useCmsUrls } from "../ui/admin/runtime.js";
|
|
20
|
+
import { useI18n } from "../ui/admin/i18n.js";
|
|
21
|
+
|
|
22
|
+
const LIMIT = 10;
|
|
23
|
+
|
|
24
|
+
function Loading({ label }: { label?: string }) {
|
|
25
|
+
const { t } = useI18n();
|
|
26
|
+
return (
|
|
27
|
+
<Box className="items-center justify-center p-10">
|
|
28
|
+
<Text size="caption" className="text-gray-500">
|
|
29
|
+
{label ?? t("common.loading")}
|
|
30
|
+
</Text>
|
|
31
|
+
</Box>
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function ErrorBox({ message }: { message: string }) {
|
|
36
|
+
const { t } = useI18n();
|
|
37
|
+
return (
|
|
38
|
+
<Box className="flex-col gap-2 p-4 sm:p-6 lg:p-8">
|
|
39
|
+
<Text size="title" className="font-semibold text-gray-900">
|
|
40
|
+
{t("app.errorTitle")}
|
|
41
|
+
</Text>
|
|
42
|
+
<Text className="text-gray-500">{message}</Text>
|
|
43
|
+
</Box>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* -------------------------------------------------------------------------- */
|
|
48
|
+
/* Login */
|
|
49
|
+
/* -------------------------------------------------------------------------- */
|
|
50
|
+
|
|
51
|
+
function LoginPage({ onLogin }: { onLogin: (user: AuthUser) => void }) {
|
|
52
|
+
const config = useCms();
|
|
53
|
+
const { t } = useI18n();
|
|
54
|
+
return (
|
|
55
|
+
<Box className="min-h-screen items-center justify-center bg-gray-50 p-4">
|
|
56
|
+
<Box className="w-full max-w-sm flex-col gap-6 rounded-xl border border-gray-200 bg-white p-6 shadow-sm sm:p-8">
|
|
57
|
+
<Box className="flex-col gap-1">
|
|
58
|
+
<Text size="title" className="font-semibold tracking-tight text-gray-900">
|
|
59
|
+
{t("login.title")}
|
|
60
|
+
</Text>
|
|
61
|
+
<Text size="caption" className="text-gray-500">
|
|
62
|
+
{t("login.subtitle", { title: config.title })}
|
|
63
|
+
</Text>
|
|
64
|
+
</Box>
|
|
65
|
+
<LoginForm
|
|
66
|
+
onSuccess={() => {
|
|
67
|
+
void fetch(config.authPath + "/me", { credentials: "same-origin" })
|
|
68
|
+
.then((response) => (response.ok ? response.json() : null))
|
|
69
|
+
.then((json: { user?: AuthUser } | null) => {
|
|
70
|
+
if (json?.user) onLogin(json.user);
|
|
71
|
+
});
|
|
72
|
+
}}
|
|
73
|
+
/>
|
|
74
|
+
</Box>
|
|
75
|
+
</Box>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* -------------------------------------------------------------------------- */
|
|
80
|
+
/* Dashboard */
|
|
81
|
+
/* -------------------------------------------------------------------------- */
|
|
82
|
+
|
|
83
|
+
function Dashboard() {
|
|
84
|
+
const config = useCms();
|
|
85
|
+
const { api } = useCmsUrls();
|
|
86
|
+
const { t } = useI18n();
|
|
87
|
+
const [totals, setTotals] = useState<Record<string, number>>({});
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
let active = true;
|
|
91
|
+
void Promise.all(
|
|
92
|
+
config.collections.map(async (collection) => {
|
|
93
|
+
if (collection.standalone) return [collection.slug, 1] as const;
|
|
94
|
+
try {
|
|
95
|
+
const response = await fetch(`${api(collection.slug)}?limit=1`);
|
|
96
|
+
if (!response.ok) return [collection.slug, 0] as const;
|
|
97
|
+
const json = (await response.json()) as { total?: number };
|
|
98
|
+
return [collection.slug, json.total ?? 0] as const;
|
|
99
|
+
} catch {
|
|
100
|
+
return [collection.slug, 0] as const;
|
|
101
|
+
}
|
|
102
|
+
}),
|
|
103
|
+
).then((entries) => {
|
|
104
|
+
if (active) setTotals(Object.fromEntries(entries));
|
|
105
|
+
});
|
|
106
|
+
return () => {
|
|
107
|
+
active = false;
|
|
108
|
+
};
|
|
109
|
+
}, [api, config.collections]);
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
<Box className="flex-col gap-8 p-4 sm:p-6 lg:p-8">
|
|
113
|
+
<Box className="flex-col gap-1">
|
|
114
|
+
<Text size="title/xl" className="font-bold tracking-tight text-gray-900">
|
|
115
|
+
{config.title}
|
|
116
|
+
</Text>
|
|
117
|
+
<Text className="text-gray-500">
|
|
118
|
+
{t("dashboard.subtitle")}
|
|
119
|
+
</Text>
|
|
120
|
+
</Box>
|
|
121
|
+
|
|
122
|
+
<Box
|
|
123
|
+
style={{ display: "grid" }}
|
|
124
|
+
className="grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-3"
|
|
125
|
+
>
|
|
126
|
+
{config.collections.map((collection) => (
|
|
127
|
+
<Link
|
|
128
|
+
key={collection.slug}
|
|
129
|
+
to={`/${collection.slug}`}
|
|
130
|
+
className="group flex flex-col gap-3 rounded-lg border border-gray-200 bg-white p-5 transition-colors hover:border-gray-900"
|
|
131
|
+
>
|
|
132
|
+
<Box className="items-center justify-between gap-3">
|
|
133
|
+
<Box className="items-center gap-2">
|
|
134
|
+
<CollectionIcon name={collection.icon} className="h-5 w-5 shrink-0 text-gray-500" />
|
|
135
|
+
<Text size="subtitle" className="font-semibold text-gray-900">
|
|
136
|
+
{collection.label}
|
|
137
|
+
</Text>
|
|
138
|
+
</Box>
|
|
139
|
+
<Badge tone={collection.standalone ? "purple" : "gray"}>
|
|
140
|
+
{collection.standalone ? t("dashboard.single") : (totals[collection.slug] ?? "…")}
|
|
141
|
+
</Badge>
|
|
142
|
+
</Box>
|
|
143
|
+
<Text size="caption" className="text-gray-500">
|
|
144
|
+
{collection.description ??
|
|
145
|
+
t("dashboard.fields", { count: collection.fields.length })}
|
|
146
|
+
</Text>
|
|
147
|
+
<Text size="caption" className="font-medium text-gray-400 group-hover:text-gray-900">
|
|
148
|
+
{t("dashboard.manage")}
|
|
149
|
+
</Text>
|
|
150
|
+
</Link>
|
|
151
|
+
))}
|
|
152
|
+
</Box>
|
|
153
|
+
</Box>
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/* -------------------------------------------------------------------------- */
|
|
158
|
+
/* Collections */
|
|
159
|
+
/* -------------------------------------------------------------------------- */
|
|
160
|
+
|
|
161
|
+
function relationsFor(apiPath: string, slug: string): Promise<RelationOptionsMap> {
|
|
162
|
+
return fetch(`${apiPath}/${slug}/relations`, { credentials: "same-origin" })
|
|
163
|
+
.then((response) => (response.ok ? response.json() : { options: {} }))
|
|
164
|
+
.then((json: { options?: RelationOptionsMap }) => json.options ?? {})
|
|
165
|
+
.catch(() => ({}));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function CollectionPage({ collection }: { collection: CollectionDef }) {
|
|
169
|
+
const { api } = useCmsUrls();
|
|
170
|
+
const { t } = useI18n();
|
|
171
|
+
const [data, setData] = useState<
|
|
172
|
+
| { kind: "loading" }
|
|
173
|
+
| { kind: "error"; message: string }
|
|
174
|
+
| { kind: "single"; item: Item; relations: RelationOptionsMap }
|
|
175
|
+
| { kind: "list"; list: ListResult; relations: RelationOptionsMap }
|
|
176
|
+
>({ kind: "loading" });
|
|
177
|
+
|
|
178
|
+
useEffect(() => {
|
|
179
|
+
let active = true;
|
|
180
|
+
setData({ kind: "loading" });
|
|
181
|
+
|
|
182
|
+
const load = async () => {
|
|
183
|
+
try {
|
|
184
|
+
const relations = await relationsFor(api(""), collection.slug);
|
|
185
|
+
if (!active) return;
|
|
186
|
+
if (collection.standalone) {
|
|
187
|
+
const response = await fetch(api(collection.slug), { credentials: "same-origin" });
|
|
188
|
+
if (!response.ok) throw new Error(t("collection.loadError"));
|
|
189
|
+
const json = (await response.json()) as { item: Item };
|
|
190
|
+
if (active) setData({ kind: "single", item: json.item, relations });
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const params = new URLSearchParams({ page: "1", limit: String(LIMIT), q: "" });
|
|
194
|
+
const response = await fetch(`${api(collection.slug)}?${params}`, {
|
|
195
|
+
credentials: "same-origin",
|
|
196
|
+
});
|
|
197
|
+
if (!response.ok) throw new Error(t("collection.loadError"));
|
|
198
|
+
const list = (await response.json()) as ListResult;
|
|
199
|
+
if (active) setData({ kind: "list", list, relations });
|
|
200
|
+
} catch (error) {
|
|
201
|
+
if (active) {
|
|
202
|
+
setData({
|
|
203
|
+
kind: "error",
|
|
204
|
+
message: error instanceof Error ? error.message : t("app.unexpected"),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
void load();
|
|
211
|
+
return () => {
|
|
212
|
+
active = false;
|
|
213
|
+
};
|
|
214
|
+
}, [api, collection, t]);
|
|
215
|
+
|
|
216
|
+
if (data.kind === "loading") return <Loading />;
|
|
217
|
+
if (data.kind === "error") return <ErrorBox message={data.message} />;
|
|
218
|
+
if (data.kind === "single") {
|
|
219
|
+
return (
|
|
220
|
+
<StandaloneManager
|
|
221
|
+
key={collection.slug}
|
|
222
|
+
collection={collection}
|
|
223
|
+
initial={data.item}
|
|
224
|
+
relationOptions={data.relations}
|
|
225
|
+
/>
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
return (
|
|
229
|
+
<CollectionManager
|
|
230
|
+
key={collection.slug}
|
|
231
|
+
collection={collection}
|
|
232
|
+
initial={data.list}
|
|
233
|
+
relationOptions={data.relations}
|
|
234
|
+
/>
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/* -------------------------------------------------------------------------- */
|
|
239
|
+
/* Users / backup */
|
|
240
|
+
/* -------------------------------------------------------------------------- */
|
|
241
|
+
|
|
242
|
+
function UsersPage({ currentUserId }: { currentUserId: number }) {
|
|
243
|
+
const { auth } = useCmsUrls();
|
|
244
|
+
const { t } = useI18n();
|
|
245
|
+
const [users, setUsers] = useState<UserSummary[] | null>(null);
|
|
246
|
+
const [error, setError] = useState<string | null>(null);
|
|
247
|
+
|
|
248
|
+
useEffect(() => {
|
|
249
|
+
let active = true;
|
|
250
|
+
void fetch(auth("users"), { credentials: "same-origin" })
|
|
251
|
+
.then(async (response) => {
|
|
252
|
+
if (!response.ok) throw new Error(t("users.empty"));
|
|
253
|
+
return (await response.json()) as { users?: UserSummary[] };
|
|
254
|
+
})
|
|
255
|
+
.then((json) => {
|
|
256
|
+
if (active) setUsers(json.users ?? []);
|
|
257
|
+
})
|
|
258
|
+
.catch((cause: unknown) => {
|
|
259
|
+
if (active) setError(cause instanceof Error ? cause.message : t("app.unexpected"));
|
|
260
|
+
});
|
|
261
|
+
return () => {
|
|
262
|
+
active = false;
|
|
263
|
+
};
|
|
264
|
+
}, [auth, t]);
|
|
265
|
+
|
|
266
|
+
if (error) return <ErrorBox message={error} />;
|
|
267
|
+
if (!users) return <Loading />;
|
|
268
|
+
return <UsersManager initial={users} currentUserId={currentUserId} />;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function BackupPage() {
|
|
272
|
+
const { backup } = useCmsUrls();
|
|
273
|
+
const { t } = useI18n();
|
|
274
|
+
const [backups, setBackups] = useState<BackupInfo[] | null>(null);
|
|
275
|
+
const [error, setError] = useState<string | null>(null);
|
|
276
|
+
|
|
277
|
+
useEffect(() => {
|
|
278
|
+
let active = true;
|
|
279
|
+
void fetch(backup(), { credentials: "same-origin" })
|
|
280
|
+
.then(async (response) => {
|
|
281
|
+
if (!response.ok) throw new Error(t("backup.empty"));
|
|
282
|
+
return (await response.json()) as { backups?: BackupInfo[] };
|
|
283
|
+
})
|
|
284
|
+
.then((json) => {
|
|
285
|
+
if (active) setBackups(json.backups ?? []);
|
|
286
|
+
})
|
|
287
|
+
.catch((cause: unknown) => {
|
|
288
|
+
if (active) setError(cause instanceof Error ? cause.message : t("app.unexpected"));
|
|
289
|
+
});
|
|
290
|
+
return () => {
|
|
291
|
+
active = false;
|
|
292
|
+
};
|
|
293
|
+
}, [backup, t]);
|
|
294
|
+
|
|
295
|
+
if (error) return <ErrorBox message={error} />;
|
|
296
|
+
if (!backups) return <Loading />;
|
|
297
|
+
return <BackupManager initial={backups} />;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/* -------------------------------------------------------------------------- */
|
|
301
|
+
/* App */
|
|
302
|
+
/* -------------------------------------------------------------------------- */
|
|
303
|
+
|
|
304
|
+
export function AdminApp() {
|
|
305
|
+
const config = useCms();
|
|
306
|
+
const { path, navigate } = useRouter();
|
|
307
|
+
const { auth } = useCmsUrls();
|
|
308
|
+
const { t } = useI18n();
|
|
309
|
+
const [user, setUser] = useState<AuthUser | null | undefined>(undefined);
|
|
310
|
+
|
|
311
|
+
useEffect(() => {
|
|
312
|
+
let active = true;
|
|
313
|
+
void fetch(auth("me"), { credentials: "same-origin" })
|
|
314
|
+
.then(async (response) => (response.ok ? ((await response.json()) as { user: AuthUser }) : null))
|
|
315
|
+
.then((json) => {
|
|
316
|
+
if (active) setUser(json?.user ?? null);
|
|
317
|
+
})
|
|
318
|
+
.catch(() => {
|
|
319
|
+
if (active) setUser(null);
|
|
320
|
+
});
|
|
321
|
+
return () => {
|
|
322
|
+
active = false;
|
|
323
|
+
};
|
|
324
|
+
}, [auth, t]);
|
|
325
|
+
|
|
326
|
+
const handleLogin = useCallback(
|
|
327
|
+
(nextUser: AuthUser) => {
|
|
328
|
+
setUser(nextUser);
|
|
329
|
+
navigate("/");
|
|
330
|
+
},
|
|
331
|
+
[navigate],
|
|
332
|
+
);
|
|
333
|
+
|
|
334
|
+
if (user === undefined) {
|
|
335
|
+
return (
|
|
336
|
+
<Box className="min-h-screen items-center justify-center bg-gray-50">
|
|
337
|
+
<Loading label={t("app.verifyingSession")} />
|
|
338
|
+
</Box>
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (!user) return <LoginPage onLogin={handleLogin} />;
|
|
343
|
+
|
|
344
|
+
const segments = path.split("/").filter(Boolean);
|
|
345
|
+
let content: React.ReactNode;
|
|
346
|
+
if (segments.length === 0 || segments[0] === "login") {
|
|
347
|
+
content = <Dashboard />;
|
|
348
|
+
} else if (segments[0] === "users") {
|
|
349
|
+
content = <UsersPage currentUserId={user.id} />;
|
|
350
|
+
} else if (segments[0] === "backup") {
|
|
351
|
+
content = <BackupPage />;
|
|
352
|
+
} else {
|
|
353
|
+
const collection = config.collections.find((entry) => entry.slug === segments[0]);
|
|
354
|
+
content = collection ? (
|
|
355
|
+
<CollectionPage collection={collection} />
|
|
356
|
+
) : (
|
|
357
|
+
<ErrorBox message={t("app.collectionNotFound", { slug: segments[0] ?? "" })} />
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
return (
|
|
362
|
+
<AdminShell
|
|
363
|
+
title={config.title}
|
|
364
|
+
collections={config.collections}
|
|
365
|
+
user={user}
|
|
366
|
+
onLoggedOut={() => setUser(null)}
|
|
367
|
+
>
|
|
368
|
+
{content}
|
|
369
|
+
</AdminShell>
|
|
370
|
+
);
|
|
371
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="pt-BR">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<meta name="robots" content="noindex, nofollow" />
|
|
7
|
+
<title>%SERENOJS_TITLE%</title>
|
|
8
|
+
</head>
|
|
9
|
+
<body class="bg-gray-50 text-gray-900 antialiased">
|
|
10
|
+
<div id="root"></div>
|
|
11
|
+
<script>
|
|
12
|
+
window.__SERENOJS__ = __SERENOJS_CONFIG__;
|
|
13
|
+
</script>
|
|
14
|
+
<script type="module" src="./main.tsx"></script>
|
|
15
|
+
</body>
|
|
16
|
+
</html>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { StrictMode } from "react";
|
|
4
|
+
import { createRoot } from "react-dom/client";
|
|
5
|
+
|
|
6
|
+
import "./styles.css";
|
|
7
|
+
import type { AdminRuntimeConfig } from "../ui/admin/runtime.js";
|
|
8
|
+
import { AdminRuntimeProvider } from "../ui/admin/runtime.js";
|
|
9
|
+
import { setLocaleLabels } from "../ui/admin/locales.js";
|
|
10
|
+
import { setDefaultLocale } from "../locale.js";
|
|
11
|
+
import { DEFAULT_ADMIN_LOCALE, I18nProvider, translate } from "../ui/admin/i18n.js";
|
|
12
|
+
import { RouterProvider } from "../ui/admin/router.js";
|
|
13
|
+
import { AdminApp } from "./app.js";
|
|
14
|
+
|
|
15
|
+
declare global {
|
|
16
|
+
interface Window {
|
|
17
|
+
__SERENOJS__?: AdminRuntimeConfig;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const config = window.__SERENOJS__;
|
|
22
|
+
const container = document.getElementById("root");
|
|
23
|
+
|
|
24
|
+
if (!config) {
|
|
25
|
+
if (container) {
|
|
26
|
+
container.textContent = translate(DEFAULT_ADMIN_LOCALE, "app.missingRuntime");
|
|
27
|
+
}
|
|
28
|
+
} else if (container) {
|
|
29
|
+
setLocaleLabels(config.locales);
|
|
30
|
+
setDefaultLocale(config.defaultLocale);
|
|
31
|
+
createRoot(container).render(
|
|
32
|
+
<StrictMode>
|
|
33
|
+
<AdminRuntimeProvider value={config}>
|
|
34
|
+
<I18nProvider>
|
|
35
|
+
<RouterProvider basePath={config.adminPath}>
|
|
36
|
+
<AdminApp />
|
|
37
|
+
</RouterProvider>
|
|
38
|
+
</I18nProvider>
|
|
39
|
+
</AdminRuntimeProvider>
|
|
40
|
+
</StrictMode>,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth core: users and opaque, database-backed session tokens.
|
|
3
|
+
*
|
|
4
|
+
* Design (see AGENTS.md):
|
|
5
|
+
* - Tokens are random 256-bit strings; only their SHA-256 hash is stored.
|
|
6
|
+
* - An access token (30 min) and a refresh token (24 h) are issued together.
|
|
7
|
+
* - Every login or refresh deletes *all* of a user's tokens before issuing a
|
|
8
|
+
* new pair, so a user effectively has a single active session at a time.
|
|
9
|
+
*/
|
|
10
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
11
|
+
|
|
12
|
+
import { getDb } from "../core/db.server.js";
|
|
13
|
+
import {
|
|
14
|
+
ACCESS_TTL_MS,
|
|
15
|
+
REFRESH_TTL_MS,
|
|
16
|
+
} from "./config.js";
|
|
17
|
+
import type { AuthUser } from "./config.js";
|
|
18
|
+
import { verifyPassword } from "./password.server.js";
|
|
19
|
+
|
|
20
|
+
export type SessionTokens = {
|
|
21
|
+
accessToken: string;
|
|
22
|
+
refreshToken: string;
|
|
23
|
+
accessExpiresAt: string;
|
|
24
|
+
refreshExpiresAt: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type UserRow = {
|
|
28
|
+
id: number;
|
|
29
|
+
email: string;
|
|
30
|
+
password_hash: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
function generateToken(): string {
|
|
34
|
+
return randomBytes(32).toString("base64url");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function hashToken(token: string): string {
|
|
38
|
+
return createHash("sha256").update(token).digest("hex");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function toAuthUser(row: { id: number; email: string }): AuthUser {
|
|
42
|
+
return { id: Number(row.id), email: String(row.email) };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function futureIso(ms: number): string {
|
|
46
|
+
return new Date(Date.now() + ms).toISOString();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isExpired(iso: string): boolean {
|
|
50
|
+
const parsed = Date.parse(iso);
|
|
51
|
+
return Number.isNaN(parsed) || parsed <= Date.now();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Run `fn` inside an IMMEDIATE transaction (serializes rotations). */
|
|
55
|
+
function withWriteLock<T>(fn: () => T): T {
|
|
56
|
+
const db = getDb();
|
|
57
|
+
db.exec("BEGIN IMMEDIATE");
|
|
58
|
+
try {
|
|
59
|
+
const result = fn();
|
|
60
|
+
db.exec("COMMIT");
|
|
61
|
+
return result;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
db.exec("ROLLBACK");
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function findUserByEmail(email: string): UserRow | null {
|
|
69
|
+
const row = getDb()
|
|
70
|
+
.prepare(`SELECT id, email, password_hash FROM auth_users WHERE email = ?`)
|
|
71
|
+
.get(email) as UserRow | undefined;
|
|
72
|
+
return row ?? null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function verifyCredentials(
|
|
76
|
+
email: string,
|
|
77
|
+
password: string,
|
|
78
|
+
): AuthUser | null {
|
|
79
|
+
const row = findUserByEmail(email);
|
|
80
|
+
if (!row) return null;
|
|
81
|
+
if (!verifyPassword(password, row.password_hash)) return null;
|
|
82
|
+
return toAuthUser(row);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Replace every token for the user with a fresh access/refresh pair. */
|
|
86
|
+
export function createSession(userId: number): SessionTokens {
|
|
87
|
+
return withWriteLock(() => {
|
|
88
|
+
const db = getDb();
|
|
89
|
+
db.prepare(`DELETE FROM auth_tokens WHERE user_id = ?`).run(userId);
|
|
90
|
+
|
|
91
|
+
const accessToken = generateToken();
|
|
92
|
+
const refreshToken = generateToken();
|
|
93
|
+
const accessExpiresAt = futureIso(ACCESS_TTL_MS);
|
|
94
|
+
const refreshExpiresAt = futureIso(REFRESH_TTL_MS);
|
|
95
|
+
|
|
96
|
+
db.prepare(
|
|
97
|
+
`INSERT INTO auth_tokens (
|
|
98
|
+
user_id, access_token_hash, refresh_token_hash,
|
|
99
|
+
access_expires_at, refresh_expires_at
|
|
100
|
+
) VALUES (?, ?, ?, ?, ?)`,
|
|
101
|
+
).run(
|
|
102
|
+
userId,
|
|
103
|
+
hashToken(accessToken),
|
|
104
|
+
hashToken(refreshToken),
|
|
105
|
+
accessExpiresAt,
|
|
106
|
+
refreshExpiresAt,
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
return { accessToken, refreshToken, accessExpiresAt, refreshExpiresAt };
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Resolve the user for a still-valid access token, or null. */
|
|
114
|
+
export function getUserByAccessToken(accessToken: string): AuthUser | null {
|
|
115
|
+
if (!accessToken) return null;
|
|
116
|
+
const row = getDb()
|
|
117
|
+
.prepare(
|
|
118
|
+
`SELECT u.id AS id, u.email AS email, t.access_expires_at AS access_expires_at
|
|
119
|
+
FROM auth_tokens t
|
|
120
|
+
JOIN auth_users u ON u.id = t.user_id
|
|
121
|
+
WHERE t.access_token_hash = ?`,
|
|
122
|
+
)
|
|
123
|
+
.get(hashToken(accessToken)) as
|
|
124
|
+
| { id: number; email: string; access_expires_at: string }
|
|
125
|
+
| undefined;
|
|
126
|
+
|
|
127
|
+
if (!row) return null;
|
|
128
|
+
if (isExpired(row.access_expires_at)) return null;
|
|
129
|
+
return toAuthUser(row);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Validate a refresh token and rotate the session: all of the user's tokens
|
|
134
|
+
* are replaced before the new pair is returned.
|
|
135
|
+
*/
|
|
136
|
+
export function rotateSession(
|
|
137
|
+
refreshToken: string,
|
|
138
|
+
): { user: AuthUser; tokens: SessionTokens } | null {
|
|
139
|
+
if (!refreshToken) return null;
|
|
140
|
+
const hash = hashToken(refreshToken);
|
|
141
|
+
|
|
142
|
+
return withWriteLock(() => {
|
|
143
|
+
const db = getDb();
|
|
144
|
+
const row = db
|
|
145
|
+
.prepare(
|
|
146
|
+
`SELECT u.id AS id, u.email AS email, t.refresh_expires_at AS refresh_expires_at
|
|
147
|
+
FROM auth_tokens t
|
|
148
|
+
JOIN auth_users u ON u.id = t.user_id
|
|
149
|
+
WHERE t.refresh_token_hash = ?`,
|
|
150
|
+
)
|
|
151
|
+
.get(hash) as
|
|
152
|
+
| { id: number; email: string; refresh_expires_at: string }
|
|
153
|
+
| undefined;
|
|
154
|
+
|
|
155
|
+
if (!row || isExpired(row.refresh_expires_at)) return null;
|
|
156
|
+
|
|
157
|
+
const user = toAuthUser(row);
|
|
158
|
+
const userId = user.id;
|
|
159
|
+
|
|
160
|
+
db.prepare(`DELETE FROM auth_tokens WHERE user_id = ?`).run(userId);
|
|
161
|
+
|
|
162
|
+
const accessToken = generateToken();
|
|
163
|
+
const newRefreshToken = generateToken();
|
|
164
|
+
const accessExpiresAt = futureIso(ACCESS_TTL_MS);
|
|
165
|
+
const refreshExpiresAt = futureIso(REFRESH_TTL_MS);
|
|
166
|
+
|
|
167
|
+
db.prepare(
|
|
168
|
+
`INSERT INTO auth_tokens (
|
|
169
|
+
user_id, access_token_hash, refresh_token_hash,
|
|
170
|
+
access_expires_at, refresh_expires_at
|
|
171
|
+
) VALUES (?, ?, ?, ?, ?)`,
|
|
172
|
+
).run(
|
|
173
|
+
userId,
|
|
174
|
+
hashToken(accessToken),
|
|
175
|
+
hashToken(newRefreshToken),
|
|
176
|
+
accessExpiresAt,
|
|
177
|
+
refreshExpiresAt,
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
user,
|
|
182
|
+
tokens: {
|
|
183
|
+
accessToken,
|
|
184
|
+
refreshToken: newRefreshToken,
|
|
185
|
+
accessExpiresAt,
|
|
186
|
+
refreshExpiresAt,
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Revoke every session of a user (logout from all devices). */
|
|
193
|
+
export function revokeUserSessions(userId: number): void {
|
|
194
|
+
getDb().prepare(`DELETE FROM auth_tokens WHERE user_id = ?`).run(userId);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Revoke the session identified by an access token. */
|
|
198
|
+
export function revokeSessionByAccessToken(accessToken: string): void {
|
|
199
|
+
if (!accessToken) return;
|
|
200
|
+
getDb()
|
|
201
|
+
.prepare(`DELETE FROM auth_tokens WHERE access_token_hash = ?`)
|
|
202
|
+
.run(hashToken(accessToken));
|
|
203
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth constants shared by server code and middleware.
|
|
3
|
+
*
|
|
4
|
+
* Tokens are opaque random strings stored (hashed) in SQLite — no JWT. They
|
|
5
|
+
* travel as httpOnly cookies and are rotated by `src/middleware/auth.ts`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const ACCESS_COOKIE = "serenojs_access";
|
|
9
|
+
export const REFRESH_COOKIE = "serenojs_refresh";
|
|
10
|
+
|
|
11
|
+
/** Authenticated user shape, safe to import from client components (type-only). */
|
|
12
|
+
export type AuthUser = {
|
|
13
|
+
id: number;
|
|
14
|
+
email: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Admin-facing user record (no password/hash). */
|
|
18
|
+
export type UserSummary = {
|
|
19
|
+
id: number;
|
|
20
|
+
email: string;
|
|
21
|
+
createdAt: string;
|
|
22
|
+
updatedAt: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Access token lifetime: 30 minutes. */
|
|
26
|
+
export const ACCESS_TTL_SECONDS = 30 * 60;
|
|
27
|
+
/** Refresh token lifetime: 24 hours. */
|
|
28
|
+
export const REFRESH_TTL_SECONDS = 24 * 60 * 60;
|
|
29
|
+
|
|
30
|
+
export const ACCESS_TTL_MS = ACCESS_TTL_SECONDS * 1000;
|
|
31
|
+
export const REFRESH_TTL_MS = REFRESH_TTL_SECONDS * 1000;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether cookies get the `Secure` flag. Enabled in production, but can be
|
|
35
|
+
* forced off (e.g. testing a production build over plain http).
|
|
36
|
+
*/
|
|
37
|
+
export const COOKIE_SECURE =
|
|
38
|
+
process.env.AUTH_COOKIE_SECURE === "true" ||
|
|
39
|
+
(process.env.AUTH_COOKIE_SECURE !== "false" &&
|
|
40
|
+
process.env.NODE_ENV === "production");
|
|
41
|
+
|
|
42
|
+
const SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS", "TRACE"]);
|
|
43
|
+
|
|
44
|
+
export function isSafeMethod(method: string): boolean {
|
|
45
|
+
return SAFE_METHODS.has(method.toUpperCase());
|
|
46
|
+
}
|