@eutobias/serenojs 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +682 -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,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cookie helpers for the plain `Request`/`Response` world of API routes.
|
|
3
|
+
*
|
|
4
|
+
* Middleware runs on Hono and uses `hono/cookie`'s context helpers instead;
|
|
5
|
+
* both sides share the names/TTLs in `./config`.
|
|
6
|
+
*/
|
|
7
|
+
import { generateCookie } from "hono/cookie";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
ACCESS_COOKIE,
|
|
11
|
+
ACCESS_TTL_SECONDS,
|
|
12
|
+
COOKIE_SECURE,
|
|
13
|
+
REFRESH_COOKIE,
|
|
14
|
+
REFRESH_TTL_SECONDS,
|
|
15
|
+
} from "./config.js";
|
|
16
|
+
import type { SessionTokens } from "./auth.server.js";
|
|
17
|
+
|
|
18
|
+
export function parseCookies(header: string | null | undefined): Record<string, string> {
|
|
19
|
+
const jar: Record<string, string> = {};
|
|
20
|
+
if (!header) return jar;
|
|
21
|
+
for (const part of header.split(";")) {
|
|
22
|
+
const index = part.indexOf("=");
|
|
23
|
+
if (index === -1) continue;
|
|
24
|
+
const name = part.slice(0, index).trim();
|
|
25
|
+
if (!name) continue;
|
|
26
|
+
const value = part.slice(index + 1).trim();
|
|
27
|
+
try {
|
|
28
|
+
jar[name] = decodeURIComponent(value);
|
|
29
|
+
} catch {
|
|
30
|
+
jar[name] = value;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return jar;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function readCookie(request: Request, name: string): string | undefined {
|
|
37
|
+
return parseCookies(request.headers.get("cookie"))[name];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function readAccessToken(request: Request): string | undefined {
|
|
41
|
+
return readCookie(request, ACCESS_COOKIE);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function readRefreshToken(request: Request): string | undefined {
|
|
45
|
+
return readCookie(request, REFRESH_COOKIE);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function appendAuthCookies(headers: Headers, tokens: SessionTokens): void {
|
|
49
|
+
headers.append(
|
|
50
|
+
"Set-Cookie",
|
|
51
|
+
generateCookie(ACCESS_COOKIE, tokens.accessToken, {
|
|
52
|
+
httpOnly: true,
|
|
53
|
+
path: "/",
|
|
54
|
+
sameSite: "Lax",
|
|
55
|
+
secure: COOKIE_SECURE,
|
|
56
|
+
maxAge: ACCESS_TTL_SECONDS,
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
headers.append(
|
|
60
|
+
"Set-Cookie",
|
|
61
|
+
generateCookie(REFRESH_COOKIE, tokens.refreshToken, {
|
|
62
|
+
httpOnly: true,
|
|
63
|
+
path: "/",
|
|
64
|
+
sameSite: "Lax",
|
|
65
|
+
secure: COOKIE_SECURE,
|
|
66
|
+
maxAge: REFRESH_TTL_SECONDS,
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function appendClearedAuthCookies(headers: Headers): void {
|
|
72
|
+
for (const name of [ACCESS_COOKIE, REFRESH_COOKIE]) {
|
|
73
|
+
headers.append(
|
|
74
|
+
"Set-Cookie",
|
|
75
|
+
generateCookie(name, "", {
|
|
76
|
+
httpOnly: true,
|
|
77
|
+
path: "/",
|
|
78
|
+
sameSite: "Lax",
|
|
79
|
+
secure: COOKIE_SECURE,
|
|
80
|
+
maxAge: 0,
|
|
81
|
+
}),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Small helpers for auth API route handlers (standard Request/Response).
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export async function readBody(request: Request): Promise<Record<string, unknown>> {
|
|
6
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
7
|
+
try {
|
|
8
|
+
if (contentType.includes("application/json")) {
|
|
9
|
+
const data = await request.json();
|
|
10
|
+
return data && typeof data === "object"
|
|
11
|
+
? (data as Record<string, unknown>)
|
|
12
|
+
: {};
|
|
13
|
+
}
|
|
14
|
+
const form = await request.formData();
|
|
15
|
+
return Object.fromEntries(form.entries());
|
|
16
|
+
} catch {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Whether the caller expects a JSON response rather than a redirect. */
|
|
22
|
+
export function wantsJson(request: Request): boolean {
|
|
23
|
+
const contentType = request.headers.get("content-type") ?? "";
|
|
24
|
+
const accept = request.headers.get("accept") ?? "";
|
|
25
|
+
return contentType.includes("application/json") || accept.includes("application/json");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Build a redirect response carrying the given (cookie) headers. */
|
|
29
|
+
export function redirectWith(location: string, headers: Headers): Response {
|
|
30
|
+
headers.set("Location", location);
|
|
31
|
+
return new Response(null, { status: 303, headers });
|
|
32
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Password hashing built on Node's `crypto.scrypt` (no external dependency).
|
|
3
|
+
*
|
|
4
|
+
* Stored format: `<salt-hex>:<derived-key-hex>`.
|
|
5
|
+
*/
|
|
6
|
+
import { randomBytes, scryptSync, timingSafeEqual } from "node:crypto";
|
|
7
|
+
|
|
8
|
+
const KEY_LENGTH = 64;
|
|
9
|
+
|
|
10
|
+
export function hashPassword(password: string): string {
|
|
11
|
+
const salt = randomBytes(16);
|
|
12
|
+
const derived = scryptSync(password, salt, KEY_LENGTH);
|
|
13
|
+
return `${salt.toString("hex")}:${derived.toString("hex")}`;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function verifyPassword(password: string, stored: string): boolean {
|
|
17
|
+
const [saltHex, keyHex] = stored.split(":");
|
|
18
|
+
if (!saltHex || !keyHex) return false;
|
|
19
|
+
const salt = Buffer.from(saltHex, "hex");
|
|
20
|
+
const expected = Buffer.from(keyHex, "hex");
|
|
21
|
+
const derived = scryptSync(password, salt, expected.length);
|
|
22
|
+
return (
|
|
23
|
+
derived.length === expected.length && timingSafeEqual(derived, expected)
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { unstable_getHeaders as getHeaders } from "waku/router/server";
|
|
2
|
+
|
|
3
|
+
import { getUserByAccessToken } from "./auth.server.js";
|
|
4
|
+
import { ACCESS_COOKIE } from "./config.js";
|
|
5
|
+
import type { AuthUser } from "./config.js";
|
|
6
|
+
import { parseCookies } from "./cookies.server.js";
|
|
7
|
+
|
|
8
|
+
export function getCurrentUser(): AuthUser | null {
|
|
9
|
+
try {
|
|
10
|
+
const headers = getHeaders();
|
|
11
|
+
const token = parseCookies(headers["cookie"] ?? "")[ACCESS_COOKIE];
|
|
12
|
+
return token ? getUserByAccessToken(token) : null;
|
|
13
|
+
} catch {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User management (admin CRUD over `auth_users`).
|
|
3
|
+
*
|
|
4
|
+
* Passwords are hashed on write and never returned; `UserSummary` is the only
|
|
5
|
+
* shape that leaves the server. Deleting a user also revokes its sessions.
|
|
6
|
+
*/
|
|
7
|
+
import { getDb } from "../core/db.server.js";
|
|
8
|
+
import type { UserSummary } from "./config.js";
|
|
9
|
+
import { hashPassword } from "./password.server.js";
|
|
10
|
+
|
|
11
|
+
export type { UserSummary };
|
|
12
|
+
|
|
13
|
+
export type MutationResult<T> =
|
|
14
|
+
| { ok: true; value: T }
|
|
15
|
+
| { ok: false; errors: Record<string, string> };
|
|
16
|
+
|
|
17
|
+
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
18
|
+
const MIN_PASSWORD_LENGTH = 8;
|
|
19
|
+
|
|
20
|
+
type UserRow = {
|
|
21
|
+
id: number;
|
|
22
|
+
email: string;
|
|
23
|
+
created_at: string;
|
|
24
|
+
updated_at: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function toSummary(row: UserRow): UserSummary {
|
|
28
|
+
return {
|
|
29
|
+
id: Number(row.id),
|
|
30
|
+
email: String(row.email),
|
|
31
|
+
createdAt: String(row.created_at),
|
|
32
|
+
updatedAt: String(row.updated_at),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function listUsers(): UserSummary[] {
|
|
37
|
+
const rows = getDb()
|
|
38
|
+
.prepare(
|
|
39
|
+
`SELECT id, email, created_at, updated_at FROM auth_users ORDER BY id`,
|
|
40
|
+
)
|
|
41
|
+
.all() as UserRow[];
|
|
42
|
+
return rows.map(toSummary);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function getUser(id: number): UserSummary | null {
|
|
46
|
+
const row = getDb()
|
|
47
|
+
.prepare(
|
|
48
|
+
`SELECT id, email, created_at, updated_at FROM auth_users WHERE id = ?`,
|
|
49
|
+
)
|
|
50
|
+
.get(id) as UserRow | undefined;
|
|
51
|
+
return row ? toSummary(row) : null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function countUsers(): number {
|
|
55
|
+
const row = getDb()
|
|
56
|
+
.prepare(`SELECT COUNT(*) AS count FROM auth_users`)
|
|
57
|
+
.get() as { count: number };
|
|
58
|
+
return Number(row.count);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function emailTaken(email: string, exceptId?: number): boolean {
|
|
62
|
+
const row =
|
|
63
|
+
exceptId === undefined
|
|
64
|
+
? getDb()
|
|
65
|
+
.prepare(`SELECT 1 AS ok FROM auth_users WHERE email = ?`)
|
|
66
|
+
.get(email)
|
|
67
|
+
: getDb()
|
|
68
|
+
.prepare(`SELECT 1 AS ok FROM auth_users WHERE email = ? AND id != ?`)
|
|
69
|
+
.get(email, exceptId);
|
|
70
|
+
return Boolean(row);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function asString(value: unknown): string {
|
|
74
|
+
return value === null || value === undefined ? "" : String(value).trim();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createUser(
|
|
78
|
+
input: Record<string, unknown>,
|
|
79
|
+
): MutationResult<UserSummary> {
|
|
80
|
+
const email = asString(input.email).toLowerCase();
|
|
81
|
+
const password = String(input.password ?? "");
|
|
82
|
+
const errors: Record<string, string> = {};
|
|
83
|
+
|
|
84
|
+
if (!email) errors.email = "Campo obrigatório.";
|
|
85
|
+
else if (!EMAIL_RE.test(email)) errors.email = "E-mail inválido.";
|
|
86
|
+
else if (emailTaken(email)) errors.email = "E-mail já cadastrado.";
|
|
87
|
+
|
|
88
|
+
if (!password) errors.password = "Campo obrigatório.";
|
|
89
|
+
else if (password.length < MIN_PASSWORD_LENGTH) {
|
|
90
|
+
errors.password = `Use ao menos ${MIN_PASSWORD_LENGTH} caracteres.`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (Object.keys(errors).length > 0) return { ok: false, errors };
|
|
94
|
+
|
|
95
|
+
const db = getDb();
|
|
96
|
+
const result = db
|
|
97
|
+
.prepare(`INSERT INTO auth_users (email, password_hash) VALUES (?, ?)`)
|
|
98
|
+
.run(email, hashPassword(password));
|
|
99
|
+
|
|
100
|
+
const user = getUser(Number(result.lastInsertRowid));
|
|
101
|
+
if (!user) throw new Error("Falha ao ler o usuário recém-criado.");
|
|
102
|
+
return { ok: true, value: user };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function updateUser(
|
|
106
|
+
id: number,
|
|
107
|
+
input: Record<string, unknown>,
|
|
108
|
+
): MutationResult<UserSummary> | null {
|
|
109
|
+
const existing = getUser(id);
|
|
110
|
+
if (!existing) return null;
|
|
111
|
+
|
|
112
|
+
const hasEmail = Object.prototype.hasOwnProperty.call(input, "email");
|
|
113
|
+
const rawPassword = String(input.password ?? "");
|
|
114
|
+
const errors: Record<string, string> = {};
|
|
115
|
+
|
|
116
|
+
const email = hasEmail ? asString(input.email).toLowerCase() : existing.email;
|
|
117
|
+
if (hasEmail) {
|
|
118
|
+
if (!email) errors.email = "Campo obrigatório.";
|
|
119
|
+
else if (!EMAIL_RE.test(email)) errors.email = "E-mail inválido.";
|
|
120
|
+
else if (emailTaken(email, id)) errors.email = "E-mail já cadastrado.";
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Password is optional on update: empty means "keep current".
|
|
124
|
+
if (rawPassword && rawPassword.length < MIN_PASSWORD_LENGTH) {
|
|
125
|
+
errors.password = `Use ao menos ${MIN_PASSWORD_LENGTH} caracteres.`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (Object.keys(errors).length > 0) return { ok: false, errors };
|
|
129
|
+
|
|
130
|
+
const db = getDb();
|
|
131
|
+
if (rawPassword) {
|
|
132
|
+
db.prepare(
|
|
133
|
+
`UPDATE auth_users SET email = ?, password_hash = ?, updated_at = datetime('now') WHERE id = ?`,
|
|
134
|
+
).run(email, hashPassword(rawPassword), id);
|
|
135
|
+
// A password change invalidates every existing session.
|
|
136
|
+
db.prepare(`DELETE FROM auth_tokens WHERE user_id = ?`).run(id);
|
|
137
|
+
} else {
|
|
138
|
+
db.prepare(
|
|
139
|
+
`UPDATE auth_users SET email = ?, updated_at = datetime('now') WHERE id = ?`,
|
|
140
|
+
).run(email, id);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const user = getUser(id);
|
|
144
|
+
if (!user) throw new Error("Falha ao reler o usuário atualizado.");
|
|
145
|
+
return { ok: true, value: user };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export type DeleteUserResult = "ok" | "not_found" | "self" | "last";
|
|
149
|
+
|
|
150
|
+
export function deleteUser(id: number, requesterId: number): DeleteUserResult {
|
|
151
|
+
if (id === requesterId) return "self";
|
|
152
|
+
if (!getUser(id)) return "not_found";
|
|
153
|
+
if (countUsers() <= 1) return "last";
|
|
154
|
+
|
|
155
|
+
const db = getDb();
|
|
156
|
+
db.prepare(`DELETE FROM auth_tokens WHERE user_id = ?`).run(id);
|
|
157
|
+
db.prepare(`DELETE FROM auth_users WHERE id = ?`).run(id);
|
|
158
|
+
return "ok";
|
|
159
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public configuration surface of the CMS package.
|
|
3
|
+
*
|
|
4
|
+
* `serenojs.config.ts` in the host project is the single source of truth for
|
|
5
|
+
* collections, locales and every mount path. Nothing inside the package may
|
|
6
|
+
* import host code (`@/schemas`) directly — everything is read through
|
|
7
|
+
* `runtime.ts`, configured once by `createCmsMiddleware`.
|
|
8
|
+
*/
|
|
9
|
+
import type {
|
|
10
|
+
CollectionDef,
|
|
11
|
+
CollectionIconName,
|
|
12
|
+
FieldDef,
|
|
13
|
+
FieldValue,
|
|
14
|
+
Item,
|
|
15
|
+
ListResult,
|
|
16
|
+
MediaItem,
|
|
17
|
+
RelationOptionsMap,
|
|
18
|
+
} from "./core/types.js";
|
|
19
|
+
|
|
20
|
+
export type { CollectionDef, CollectionIconName, FieldDef, FieldValue, Item, ListResult, MediaItem, RelationOptionsMap };
|
|
21
|
+
|
|
22
|
+
/** A locale known to the admin UI. */
|
|
23
|
+
export type LocaleDef = {
|
|
24
|
+
code: string;
|
|
25
|
+
label: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type CmsAdminOptions = {
|
|
29
|
+
/** Mount path for the admin SPA. Default: `/admin`. */
|
|
30
|
+
path?: string;
|
|
31
|
+
/** When `false`, the admin SPA and its assets answer `404`. */
|
|
32
|
+
enabled?: boolean;
|
|
33
|
+
/** Emit `X-Robots-Tag: noindex, nofollow` on admin responses. */
|
|
34
|
+
noindex?: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type CmsConfig = {
|
|
38
|
+
/** Title shown in the admin UI. */
|
|
39
|
+
title?: string;
|
|
40
|
+
collections: CollectionDef[];
|
|
41
|
+
/** Locale registry shown in the admin tabs. */
|
|
42
|
+
locales?: LocaleDef[];
|
|
43
|
+
/** Fallback locale. Default: `pt`. */
|
|
44
|
+
defaultLocale?: string;
|
|
45
|
+
admin?: CmsAdminOptions;
|
|
46
|
+
api?: { path?: string };
|
|
47
|
+
auth?: { path?: string };
|
|
48
|
+
media?: { path?: string };
|
|
49
|
+
files?: { path?: string };
|
|
50
|
+
backup?: {
|
|
51
|
+
path?: string;
|
|
52
|
+
enabled?: boolean;
|
|
53
|
+
intervalHours?: number;
|
|
54
|
+
keep?: number;
|
|
55
|
+
};
|
|
56
|
+
db?: { path?: string };
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export type ResolvedCmsConfig = {
|
|
60
|
+
title: string;
|
|
61
|
+
collections: CollectionDef[];
|
|
62
|
+
locales: LocaleDef[];
|
|
63
|
+
defaultLocale: string;
|
|
64
|
+
admin: { path: string; enabled: boolean; noindex: boolean };
|
|
65
|
+
api: { path: string };
|
|
66
|
+
auth: { path: string };
|
|
67
|
+
media: { path: string };
|
|
68
|
+
files: { path: string };
|
|
69
|
+
backup: {
|
|
70
|
+
path: string;
|
|
71
|
+
enabled: boolean;
|
|
72
|
+
intervalHours: number;
|
|
73
|
+
keep: number;
|
|
74
|
+
};
|
|
75
|
+
db: { path: string };
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const DEFAULT_LOCALES: LocaleDef[] = [
|
|
79
|
+
{ code: "pt", label: "Português" },
|
|
80
|
+
{ code: "en", label: "English" },
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
function mount(value: string | undefined, fallback: string): string {
|
|
84
|
+
const raw = (value ?? fallback).trim() || fallback;
|
|
85
|
+
const withSlash = raw.startsWith("/") ? raw : `/${raw}`;
|
|
86
|
+
return withSlash.length > 1 ? withSlash.replace(/\/+$/, "") : withSlash;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Normalize a user-supplied config into the shape used at runtime. */
|
|
90
|
+
export function resolveCmsConfig(config: CmsConfig): ResolvedCmsConfig {
|
|
91
|
+
const locales =
|
|
92
|
+
config.locales && config.locales.length > 0 ? config.locales : DEFAULT_LOCALES;
|
|
93
|
+
return {
|
|
94
|
+
title: config.title ?? "SerenoJS",
|
|
95
|
+
collections: config.collections,
|
|
96
|
+
locales,
|
|
97
|
+
defaultLocale: config.defaultLocale ?? "pt",
|
|
98
|
+
admin: {
|
|
99
|
+
path: mount(config.admin?.path, "/admin"),
|
|
100
|
+
enabled: config.admin?.enabled !== false,
|
|
101
|
+
noindex: config.admin?.noindex !== false,
|
|
102
|
+
},
|
|
103
|
+
api: { path: mount(config.api?.path, "/api") },
|
|
104
|
+
auth: { path: mount(config.auth?.path, "/auth") },
|
|
105
|
+
media: { path: mount(config.media?.path, "/media") },
|
|
106
|
+
files: { path: mount(config.files?.path, "/files") },
|
|
107
|
+
backup: {
|
|
108
|
+
path: mount(config.backup?.path, "/backup"),
|
|
109
|
+
enabled: config.backup?.enabled !== false,
|
|
110
|
+
intervalHours: config.backup?.intervalHours ?? 24,
|
|
111
|
+
keep: config.backup?.keep ?? 10,
|
|
112
|
+
},
|
|
113
|
+
db: { path: config.db?.path ?? ".data/admin.db" },
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Identity helper for editor autocomplete in `serenojs.config.ts`. */
|
|
118
|
+
export function defineCmsConfig(config: CmsConfig): CmsConfig {
|
|
119
|
+
return config;
|
|
120
|
+
}
|