openadminjs 0.2.0 → 0.3.0-rc.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 +106 -151
- package/dist/adapt-package-manager.js +4 -1
- package/dist/create-project.d.ts +46 -0
- package/dist/create-project.js +417 -150
- package/dist/doctor.d.ts +13 -0
- package/dist/doctor.js +35 -6
- package/dist/index.d.ts +1 -0
- package/dist/index.js +257 -39
- package/dist/render-schema.d.ts +0 -17
- package/dist/render-schema.js +25 -29
- package/dist/resource-slug.d.ts +1 -0
- package/dist/resource-slug.js +7 -0
- package/dist/runtime-mode.d.ts +16 -0
- package/dist/runtime-mode.js +138 -0
- package/dist/upgrade.d.ts +10 -0
- package/dist/upgrade.js +133 -0
- package/package.json +14 -11
- package/template/.dockerignore +15 -0
- package/template/.env.production.example +5 -0
- package/template/Dockerfile +12 -0
- package/template/README.md +4 -3
- package/template/apps/admin/app/api/auth/login/route.ts +9 -9
- package/template/apps/admin/app/api/auth/logout/route.ts +11 -3
- package/template/apps/admin/app/api/auth/oidc/callback/route.ts +19 -0
- package/template/apps/admin/app/api/auth/oidc/start/route.ts +11 -0
- package/template/apps/admin/app/api/auth/oidc/status/route.ts +6 -0
- package/template/apps/admin/app/api/auth/refresh/route.ts +18 -11
- package/template/apps/admin/app/api/backend/[...path]/route.ts +48 -0
- package/template/apps/admin/app/api-tokens/page.tsx +39 -39
- package/template/apps/admin/app/assistant/page.tsx +11 -0
- package/template/apps/admin/app/dashboard/page.tsx +45 -462
- package/template/apps/admin/app/globals.css +52 -16
- package/template/apps/admin/app/layout.tsx +1 -1
- package/template/apps/admin/app/login/page.tsx +13 -145
- package/template/apps/admin/app/plugins/page.tsx +16 -402
- package/template/apps/admin/app/profile/page.tsx +5 -2
- package/template/apps/admin/app/settings/page.tsx +14 -265
- package/template/apps/admin/components/ai-assistant-widget.tsx +3 -6
- package/template/apps/admin/components/app-shell.tsx +25 -373
- package/template/apps/admin/components/auth-guard.tsx +8 -28
- package/template/apps/admin/components/logo.tsx +1 -30
- package/template/apps/admin/components/resource-form.tsx +3 -3
- package/template/apps/admin/components/resource-table.tsx +12 -8
- package/template/apps/admin/lib/api.ts +45 -107
- package/template/apps/admin/lib/resource-table-prefs.ts +1 -57
- package/template/apps/admin/lib/server-api.ts +7 -0
- package/template/apps/admin/lib/tokens.ts +9 -81
- package/template/apps/admin/middleware.ts +9 -50
- package/template/apps/admin/next.config.mjs +4 -0
- package/template/apps/admin/package.json +2 -1
- package/template/apps/admin/public/brand/favicon.svg +4 -1
- package/template/apps/admin/tailwind.config.ts +9 -8
- package/template/apps/api/package.json +10 -6
- package/template/apps/api/src/access/catalog.ts +4 -0
- package/template/apps/api/src/admin/admin-ai.controller.ts +15 -125
- package/template/apps/api/src/admin/admin-analytics.service.ts +16 -148
- package/template/apps/api/src/admin/admin-logs.controller.ts +4 -1
- package/template/apps/api/src/admin/admin-plugins.controller.ts +8 -8
- package/template/apps/api/src/admin/admin-plugins.service.ts +15 -259
- package/template/apps/api/src/admin/admin-resource.service.ts +105 -254
- package/template/apps/api/src/admin/admin.controller.ts +17 -4
- package/template/apps/api/src/admin/admin.module.ts +2 -2
- package/template/apps/api/src/admin/resource-policy.ts +1 -0
- package/template/apps/api/src/admin/resource-query.ts +1 -0
- package/template/apps/api/src/admin/safe-ai.service.ts +43 -0
- package/template/apps/api/src/admin/validate-resource-payload.ts +2 -0
- package/template/apps/api/src/auth/auth.controller.ts +4 -2
- package/template/apps/api/src/auth/auth.service.ts +68 -83
- package/template/apps/api/src/auth/sso-oidc.controller.ts +12 -74
- package/template/apps/api/src/auth/sso-oidc.service.ts +51 -268
- package/template/apps/api/src/common/api-exception.filter.ts +1 -0
- package/template/apps/api/src/common/auth.guard.ts +66 -39
- package/template/apps/api/src/common/json-field-codec.ts +12 -13
- package/template/apps/api/src/common/prisma.service.ts +9 -17
- package/template/apps/api/src/common/request-logging.interceptor.ts +8 -3
- package/template/apps/api/src/graphql/graphql.module.ts +5 -4
- package/template/apps/api/src/health/health.controller.ts +6 -0
- package/template/apps/api/src/main.ts +11 -2
- package/template/apps/api/src/plugins/plugin-loader.ts +5 -3
- package/template/apps/api/src/resources/commerce.resource.ts +3 -34
- package/template/apps/api/src/resources/operations.resource.ts +1 -0
- package/template/apps/api/src/store/payment/payment.controller.ts +15 -92
- package/template/apps/api/src/store/payment/payment.module.ts +2 -3
- package/template/apps/api/src/store/payment/payment.service.ts +121 -227
- package/template/apps/api/webpack.config.cjs +2 -1
- package/template/apps/web/next.config.mjs +4 -0
- package/template/apps/web/public/brand/favicon.svg +4 -1
- package/template/compose.yaml +56 -0
- package/template/examples/crm-admin/README.md +31 -0
- package/template/examples/crm-admin/package.json +17 -0
- package/template/examples/crm-admin/prisma/models.fragment.prisma +50 -0
- package/template/examples/crm-admin/src/index.ts +7 -0
- package/template/examples/crm-admin/src/resources/activities.resource.ts +29 -0
- package/template/examples/crm-admin/src/resources/companies.resource.ts +21 -0
- package/template/examples/crm-admin/src/resources/contacts.resource.ts +24 -0
- package/template/examples/crm-admin/src/resources/deals.resource.ts +30 -0
- package/template/examples/crm-admin/tsconfig.json +7 -0
- package/template/package.json +6 -1
- package/template/packages/admin-client/package.json +18 -0
- package/template/packages/admin-client/src/contracts.ts +29 -0
- package/template/packages/admin-client/src/index.ts +2 -0
- package/template/packages/admin-client/src/preferences.ts +57 -0
- package/template/packages/admin-client/tsconfig.json +12 -0
- package/template/packages/auth/package.json +1 -1
- package/template/packages/brand/site/assets/brand/favicon.svg +4 -1
- package/template/packages/brand/src/generate-assets.mjs +17 -186
- package/template/packages/core/package.json +1 -1
- package/template/packages/mail/package.json +1 -1
- package/template/packages/nest/package.json +22 -0
- package/template/packages/nest/src/api-exception.filter.ts +23 -0
- package/template/packages/nest/src/index.ts +3 -0
- package/template/packages/nest/src/resource-policy.ts +64 -0
- package/template/packages/nest/src/resource-query.ts +103 -0
- package/template/packages/nest/tsconfig.json +12 -0
- package/template/packages/notifications/package.json +1 -1
- package/template/packages/payments/package.json +1 -1
- package/template/packages/permissions/package.json +1 -1
- package/template/packages/permissions/src/index.ts +2 -2
- package/template/packages/plugin-sdk/package.json +1 -1
- package/template/packages/plugin-sdk/src/index.ts +2 -2
- package/template/packages/prisma/package.json +21 -0
- package/template/packages/prisma/schema/ai.prisma +36 -0
- package/template/packages/prisma/schema/audit.prisma +13 -0
- package/template/packages/prisma/schema/commerce.prisma +79 -0
- package/template/packages/prisma/schema/content.prisma +27 -0
- package/template/packages/prisma/schema/core.prisma +106 -0
- package/template/packages/prisma/schema/header.prisma +8 -0
- package/template/packages/prisma/schema/jobs.prisma +9 -0
- package/template/packages/prisma/schema/media.prisma +9 -0
- package/template/packages/prisma/schema/modules.json +47 -0
- package/template/packages/prisma/schema/notifications.prisma +9 -0
- package/template/packages/prisma/schema/settings.prisma +8 -0
- package/template/packages/prisma/schema/tokens.prisma +10 -0
- package/template/packages/prisma/src/index.d.ts +12 -0
- package/template/packages/prisma/src/index.mjs +138 -0
- package/template/packages/queue/package.json +1 -1
- package/template/packages/resource/package.json +1 -1
- package/template/packages/resource/src/index.ts +1 -0
- package/template/packages/storage/package.json +1 -1
- package/template/packages/storage/src/index.ts +37 -0
- package/template/prisma/migrations/20261126130000_add_sessions/migration.sql +26 -0
- package/template/prisma/migrations/20261126140000_api_token_hash/migration.sql +3 -0
- package/template/prisma/migrations/20261126150000_payment_idempotency/migration.sql +6 -0
- package/template/prisma/schema.prisma +82 -54
- package/template/prisma/seed.ts +14 -38
- package/template/runtime-versions.json +15 -0
- package/template/scripts/db-init.mjs +33 -0
- package/template/scripts/install-crm.mjs +50 -0
- package/template/scripts/migrate-existing.mjs +65 -0
- package/template/scripts/schema.mjs +48 -0
- package/template/tsconfig.base.json +23 -5
- package/LICENSE +0 -21
- package/dist/adapt-package-manager.test.d.ts +0 -1
- package/dist/adapt-package-manager.test.js +0 -76
- package/dist/cli.e2e.test.d.ts +0 -1
- package/dist/cli.e2e.test.js +0 -87
- package/dist/create-project.test.d.ts +0 -1
- package/dist/create-project.test.js +0 -275
- package/dist/database-providers.integration.test.d.ts +0 -1
- package/dist/database-providers.integration.test.js +0 -64
- package/dist/db-commands.test.d.ts +0 -1
- package/dist/db-commands.test.js +0 -27
- package/dist/doctor.test.d.ts +0 -1
- package/dist/doctor.test.js +0 -102
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -11
- package/dist/render-schema.test.d.ts +0 -1
- package/dist/render-schema.test.js +0 -57
- package/dist/resource-slug.test.d.ts +0 -1
- package/dist/resource-slug.test.js +0 -9
- package/dist/secrets.test.d.ts +0 -1
- package/dist/secrets.test.js +0 -36
- package/dist/security.test.d.ts +0 -1
- package/dist/security.test.js +0 -54
- package/dist/validate-name.test.d.ts +0 -1
- package/dist/validate-name.test.js +0 -63
- package/template/apps/admin/.storybook/main.ts +0 -12
- package/template/apps/admin/.storybook/preview.ts +0 -10
- package/template/apps/admin/app/login/page.test.tsx +0 -18
- package/template/apps/admin/components/auth-guard.test.tsx +0 -55
- package/template/apps/admin/components/logo.stories.tsx +0 -24
- package/template/apps/admin/lib/api.test.ts +0 -118
- package/template/apps/admin/lib/auth-flow.e2e.test.ts +0 -79
- package/template/apps/admin/vitest.config.ts +0 -7
- package/template/apps/api/src/access/derive-permissions.test.ts +0 -26
- package/template/apps/api/src/admin/admin-resource.service.test.ts +0 -118
- package/template/apps/api/src/admin/validate-resource-payload.test.ts +0 -44
- package/template/apps/api/src/app.module.test.ts +0 -12
- package/template/apps/api/src/auth/auth.service.test.ts +0 -78
- package/template/apps/api/src/auth/sso-oidc.service.test.ts +0 -84
- package/template/apps/api/src/common/auth.guard.test.ts +0 -62
- package/template/apps/api/src/common/casl-ability.factory.test.ts +0 -33
- package/template/apps/api/src/common/json-field-codec.test.ts +0 -76
- package/template/apps/api/src/common/prisma.service.test.ts +0 -19
- package/template/apps/api/src/common/request-logging.interceptor.test.ts +0 -39
- package/template/apps/api/src/graphql/admin-resources.resolver.test.ts +0 -45
- package/template/apps/api/src/graphql/ensure-gql-paramtypes.test.ts +0 -44
- package/template/apps/api/src/graphql/graphql-apollo-config.test.ts +0 -26
- package/template/apps/api/src/graphql/graphql-schema.test.ts +0 -26
- package/template/apps/api/src/plugins/manifest.schema.test.ts +0 -36
- package/template/apps/api/src/plugins/plugin-runtime.test.ts +0 -24
- package/template/apps/api/src/queue/queue.service.test.ts +0 -33
- package/template/apps/api/src/resources/resource-hooks.registry.test.ts +0 -72
- package/template/apps/api/vitest.config.ts +0 -21
- package/template/apps/web/lib/seo.test.ts +0 -41
- package/template/apps/web/vitest.config.ts +0 -19
- package/template/packages/auth/src/index.test.ts +0 -10
- package/template/packages/brand/src/index.test.ts +0 -9
- package/template/packages/core/src/index.test.ts +0 -27
- package/template/packages/core/vitest.config.ts +0 -9
- package/template/packages/mail/src/index.test.ts +0 -23
- package/template/packages/notifications/src/index.test.ts +0 -9
- package/template/packages/permissions/src/blueprints.test.ts +0 -62
- package/template/packages/permissions/src/index.test.ts +0 -34
- package/template/packages/plugin-sdk/src/sdk.test.ts +0 -13
- package/template/packages/plugin-sdk/vitest.config.ts +0 -5
- package/template/packages/queue/src/index.test.ts +0 -12
- package/template/packages/resource/src/index.test.ts +0 -196
- package/template/packages/storage/src/index.test.ts +0 -10
|
@@ -1,15 +1,5 @@
|
|
|
1
1
|
import { getUiLocale } from "./locale";
|
|
2
|
-
|
|
3
|
-
token,
|
|
4
|
-
refreshToken,
|
|
5
|
-
setTokenCookies,
|
|
6
|
-
clearTokenCookies,
|
|
7
|
-
isTokenFresh
|
|
8
|
-
} from "./tokens";
|
|
9
|
-
|
|
10
|
-
const rawApi = process.env.NEXT_PUBLIC_API_URL;
|
|
11
|
-
export const API_URL =
|
|
12
|
-
typeof rawApi === "string" && rawApi.trim().length > 0 ? rawApi.trim() : "http://localhost:4000";
|
|
2
|
+
export const API_URL = "/api/backend";
|
|
13
3
|
|
|
14
4
|
export class ApiError extends Error {
|
|
15
5
|
status: number;
|
|
@@ -31,46 +21,7 @@ function emitApiError(error: ApiError): void {
|
|
|
31
21
|
window.dispatchEvent(new CustomEvent<ApiError>(API_ERROR_EVENT, { detail: error }));
|
|
32
22
|
}
|
|
33
23
|
|
|
34
|
-
export type ResourceMeta
|
|
35
|
-
name: string;
|
|
36
|
-
label: string;
|
|
37
|
-
icon?: string;
|
|
38
|
-
titleField?: string;
|
|
39
|
-
permissions: Record<string, string>;
|
|
40
|
-
fields: Record<
|
|
41
|
-
string,
|
|
42
|
-
{
|
|
43
|
-
type: string;
|
|
44
|
-
label?: string;
|
|
45
|
-
list?: boolean;
|
|
46
|
-
create?: boolean;
|
|
47
|
-
edit?: boolean;
|
|
48
|
-
required?: boolean;
|
|
49
|
-
sortable?: boolean;
|
|
50
|
-
searchable?: boolean;
|
|
51
|
-
filterable?: boolean;
|
|
52
|
-
sensitive?: boolean;
|
|
53
|
-
resource?: string;
|
|
54
|
-
displayField?: string;
|
|
55
|
-
options?: string[];
|
|
56
|
-
}
|
|
57
|
-
>;
|
|
58
|
-
actions?: Record<string, { label: string; variant?: string; confirm?: boolean; permission: string }>;
|
|
59
|
-
i18n?: { defaultLocale?: string; locales?: readonly string[] };
|
|
60
|
-
seo?: { public?: boolean; slugField?: string; pathPattern?: string; titleField?: string; descriptionField?: string };
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
// Re-export for backward compat and convenience
|
|
64
|
-
export { token, isTokenFresh as isAccessTokenFresh };
|
|
65
|
-
|
|
66
|
-
/** True when the stored access token exists and has not expired (with 5-second buffer). */
|
|
67
|
-
export function hasUsableAccessToken(): boolean {
|
|
68
|
-
return isTokenFresh(token());
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// ──────────────────────────────────────────────
|
|
72
|
-
// Locale helper
|
|
73
|
-
// ──────────────────────────────────────────────
|
|
24
|
+
export type { ResourceMeta } from "@openadminjs/admin-client";
|
|
74
25
|
|
|
75
26
|
function withAdminLocale(path: string): string {
|
|
76
27
|
if (!path.startsWith("/admin/resources")) return path;
|
|
@@ -80,36 +31,36 @@ function withAdminLocale(path: string): string {
|
|
|
80
31
|
}
|
|
81
32
|
|
|
82
33
|
// ──────────────────────────────────────────────
|
|
83
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
34
|
+
// Same-origin BFF refresh; per-tab single flight and cross-tab Web Locks.
|
|
35
|
+
type RefreshOutcome = "ready" | "denied" | "unavailable";
|
|
36
|
+
let refreshInFlight: Promise<RefreshOutcome> | undefined;
|
|
37
|
+
let authGeneration = 0;
|
|
38
|
+
function tryRefreshToken(): Promise<RefreshOutcome> {
|
|
39
|
+
if (typeof window === "undefined") return Promise.resolve("denied");
|
|
40
|
+
if (!refreshInFlight) {
|
|
41
|
+
const ready = (): RefreshOutcome => { authGeneration++; return "ready"; };
|
|
42
|
+
const refresh = async (): Promise<RefreshOutcome> => {
|
|
43
|
+
const response = await fetch("/api/auth/refresh", { method: "POST", credentials: "same-origin", cache: "no-store" });
|
|
44
|
+
return response.ok ? ready() : response.status === 401 ? "denied" : "unavailable";
|
|
45
|
+
};
|
|
46
|
+
const locks = typeof navigator !== "undefined" ? navigator.locks : undefined;
|
|
47
|
+
const run = async (): Promise<RefreshOutcome> => {
|
|
48
|
+
if (!locks) return refresh();
|
|
49
|
+
return locks.request("openadminjs-refresh", async () => {
|
|
50
|
+
const check = await fetch("/api/backend/auth/me", { credentials: "same-origin", cache: "no-store" });
|
|
51
|
+
if (check.ok) return ready();
|
|
52
|
+
if (check.status !== 401) return "unavailable";
|
|
53
|
+
return refresh();
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
refreshInFlight = run().catch((): RefreshOutcome => "unavailable").finally(() => { refreshInFlight = undefined; });
|
|
57
|
+
}
|
|
58
|
+
return refreshInFlight;
|
|
105
59
|
}
|
|
106
60
|
|
|
107
|
-
// ──────────────────────────────────────────────
|
|
108
|
-
// Core fetch wrapper
|
|
109
|
-
// ──────────────────────────────────────────────
|
|
110
|
-
|
|
111
61
|
export async function api<T>(path: string, options: RequestInit = {}): Promise<T> {
|
|
112
|
-
|
|
62
|
+
if (!path.startsWith("/") || path.startsWith("//") || path.includes("\\")) throw new Error("Invalid API path");
|
|
63
|
+
const url = path === "/auth/logout" ? "/api/auth/logout" : `${API_URL}${withAdminLocale(path)}`;
|
|
113
64
|
const response = await requestWithAuthRetry(url, options);
|
|
114
65
|
if (!response.ok) {
|
|
115
66
|
const retryAfterHeader = response.headers?.get?.("retry-after") ?? null;
|
|
@@ -143,36 +94,23 @@ export function onApiError(listener: (error: ApiError) => void): () => void {
|
|
|
143
94
|
|
|
144
95
|
async function requestWithAuthRetry(url: string, options: RequestInit): Promise<Response> {
|
|
145
96
|
const method = (options.method ?? "GET").toUpperCase();
|
|
146
|
-
const body =
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const makeRequest = (
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const res = await makeRequest(hasUsableAccessToken() ? token() : null);
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
res.status === 401 &&
|
|
165
|
-
typeof window !== "undefined" &&
|
|
166
|
-
!url.includes("/auth/login") &&
|
|
167
|
-
!url.includes("/auth/refresh")
|
|
168
|
-
) {
|
|
169
|
-
const refreshed = await tryRefreshToken();
|
|
170
|
-
if (refreshed) return makeRequest(token());
|
|
171
|
-
|
|
172
|
-
clearTokenCookies();
|
|
173
|
-
if (!window.location.pathname.startsWith("/login")) {
|
|
174
|
-
window.location.assign("/login");
|
|
97
|
+
const body = options.body ?? (method !== "GET" && method !== "HEAD" && method !== "DELETE" ? "{}" : undefined);
|
|
98
|
+
const headers = new Headers(options.headers);
|
|
99
|
+
headers.delete("authorization");
|
|
100
|
+
if (!headers.has("content-type")) headers.set("content-type", "application/json");
|
|
101
|
+
const makeRequest = () => fetch(url, { ...options, headers, credentials: "same-origin", ...(body !== undefined ? { body } : {}) });
|
|
102
|
+
const generation = authGeneration;
|
|
103
|
+
const res = await makeRequest();
|
|
104
|
+
if (res.status === 401 && typeof window !== "undefined") {
|
|
105
|
+
const outcome = authGeneration !== generation ? "ready" : await tryRefreshToken();
|
|
106
|
+
if (outcome === "unavailable") {
|
|
107
|
+
return new Response(JSON.stringify({ message: "Session refresh temporarily unavailable. Please retry." }), { status: 503, headers: { "content-type": "application/json" } });
|
|
108
|
+
}
|
|
109
|
+
if (outcome === "ready") {
|
|
110
|
+
const retry = await makeRequest();
|
|
111
|
+
if (retry.status !== 401) return retry;
|
|
175
112
|
}
|
|
113
|
+
if (!window.location.pathname.startsWith("/login")) window.location.assign("/login");
|
|
176
114
|
}
|
|
177
115
|
return res;
|
|
178
116
|
}
|
|
@@ -1,57 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
function keyColumnOrder(resourceName: string): string {
|
|
4
|
-
return `${PREFIX}${resourceName}.columnOrder`;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
function keyDensity(resourceName: string): string {
|
|
8
|
-
return `${PREFIX}${resourceName}.density`;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type TableDensity = "comfortable" | "compact";
|
|
12
|
-
|
|
13
|
-
export function loadColumnOrder(resourceName: string, allowed: string[]): string[] {
|
|
14
|
-
if (typeof window === "undefined") return allowed;
|
|
15
|
-
try {
|
|
16
|
-
const raw = window.localStorage.getItem(keyColumnOrder(resourceName));
|
|
17
|
-
if (!raw) return allowed;
|
|
18
|
-
const parsed = JSON.parse(raw) as unknown;
|
|
19
|
-
if (!Array.isArray(parsed)) return allowed;
|
|
20
|
-
const set = new Set(allowed);
|
|
21
|
-
const ordered = parsed.map(String).filter((n) => set.has(n));
|
|
22
|
-
for (const n of allowed) {
|
|
23
|
-
if (!ordered.includes(n)) ordered.push(n);
|
|
24
|
-
}
|
|
25
|
-
return ordered;
|
|
26
|
-
} catch {
|
|
27
|
-
return allowed;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function saveColumnOrder(resourceName: string, order: string[]): void {
|
|
32
|
-
if (typeof window === "undefined") return;
|
|
33
|
-
try {
|
|
34
|
-
window.localStorage.setItem(keyColumnOrder(resourceName), JSON.stringify(order));
|
|
35
|
-
} catch {
|
|
36
|
-
/* ignore */
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function loadDensity(resourceName: string): TableDensity {
|
|
41
|
-
if (typeof window === "undefined") return "comfortable";
|
|
42
|
-
try {
|
|
43
|
-
const v = window.localStorage.getItem(keyDensity(resourceName));
|
|
44
|
-
return v === "compact" ? "compact" : "comfortable";
|
|
45
|
-
} catch {
|
|
46
|
-
return "comfortable";
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function saveDensity(resourceName: string, density: TableDensity): void {
|
|
51
|
-
if (typeof window === "undefined") return;
|
|
52
|
-
try {
|
|
53
|
-
window.localStorage.setItem(keyDensity(resourceName), density);
|
|
54
|
-
} catch {
|
|
55
|
-
/* ignore */
|
|
56
|
-
}
|
|
57
|
-
}
|
|
1
|
+
export * from "@openadminjs/admin-client";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { NextRequest } from "next/server";
|
|
2
|
+
export const API_URL = (process.env.API_INTERNAL_URL || process.env.NEXT_PUBLIC_API_URL || "http://localhost:4000").replace(/\/$/, "");
|
|
3
|
+
export function isSameOrigin(req: NextRequest): boolean {
|
|
4
|
+
const origin = req.headers.get("origin");
|
|
5
|
+
const expected = (process.env.ADMIN_ORIGIN || req.nextUrl.origin).split(",").map(value => value.trim());
|
|
6
|
+
return Boolean(origin && expected.includes(origin));
|
|
7
|
+
}
|
|
@@ -1,93 +1,21 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Isomorphic token helpers.
|
|
3
|
-
*
|
|
4
|
-
* Server components / Route Handlers: use `getServerTokens()`.
|
|
5
|
-
* Client components / browser code: use `token()`, `setTokenCookies()`, `clearTokenCookies()`.
|
|
6
|
-
* Middleware: reads cookies via `request.cookies.get()` directly.
|
|
7
|
-
*
|
|
8
|
-
* Tokens are stored as non-HttpOnly Lax-SameSite cookies so they are:
|
|
9
|
-
* - Accessible from Next.js middleware (server-side SSR redirect)
|
|
10
|
-
* - Accessible from server components via `cookies()` from `next/headers`
|
|
11
|
-
* - Readable by client JS to build Authorization headers for cross-origin NestJS calls
|
|
12
|
-
*
|
|
13
|
-
* NOTE: If you switch to a same-origin proxy setup (all API calls through Next.js Route Handlers)
|
|
14
|
-
* you can safely upgrade to `HttpOnly` by removing `token()` from client code and reading
|
|
15
|
-
* from the cookie only server-side.
|
|
16
|
-
*/
|
|
17
|
-
|
|
1
|
+
/** Session cookies are server-only. Do not export browser token readers. */
|
|
18
2
|
export const TOKEN_COOKIE_AT = "oajs_at";
|
|
19
3
|
export const TOKEN_COOKIE_RT = "oajs_rt";
|
|
20
4
|
|
|
21
|
-
/** Cookie max-age in seconds — 7 days */
|
|
22
|
-
const MAX_AGE = 60 * 60 * 24 * 7;
|
|
23
|
-
|
|
24
|
-
// ──────────────────────────────────────────────
|
|
25
|
-
// Client-side helpers (browser only)
|
|
26
|
-
// ──────────────────────────────────────────────
|
|
27
|
-
|
|
28
|
-
function parseCookieValue(cookieStr: string, key: string): string | null {
|
|
29
|
-
const match = cookieStr.match(new RegExp(`(?:^|; )${key}=([^;]*)`));
|
|
30
|
-
const value = match?.[1];
|
|
31
|
-
return value ? decodeURIComponent(value) : null;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Read access token from cookie (client-side). */
|
|
35
|
-
export function token(): string | null {
|
|
36
|
-
if (typeof document === "undefined") return null;
|
|
37
|
-
return parseCookieValue(document.cookie, TOKEN_COOKIE_AT);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Read refresh token from cookie (client-side). */
|
|
41
|
-
export function refreshToken(): string | null {
|
|
42
|
-
if (typeof document === "undefined") return null;
|
|
43
|
-
return parseCookieValue(document.cookie, TOKEN_COOKIE_RT);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Persist access + refresh tokens to cookies (client-side, called after login/refresh). */
|
|
47
|
-
export function setTokenCookies(accessToken: string, refreshTokenValue: string): void {
|
|
48
|
-
if (typeof document === "undefined") return;
|
|
49
|
-
const opts = `path=/; max-age=${MAX_AGE}; SameSite=Lax`;
|
|
50
|
-
document.cookie = `${TOKEN_COOKIE_AT}=${encodeURIComponent(accessToken)}; ${opts}`;
|
|
51
|
-
document.cookie = `${TOKEN_COOKIE_RT}=${encodeURIComponent(refreshTokenValue)}; ${opts}`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** Remove tokens (client-side logout or failed refresh). */
|
|
55
|
-
export function clearTokenCookies(): void {
|
|
56
|
-
if (typeof document === "undefined") return;
|
|
57
|
-
document.cookie = `${TOKEN_COOKIE_AT}=; path=/; max-age=0; SameSite=Lax`;
|
|
58
|
-
document.cookie = `${TOKEN_COOKIE_RT}=; path=/; max-age=0; SameSite=Lax`;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// ──────────────────────────────────────────────
|
|
62
|
-
// Server-side helpers (Server Components / Route Handlers)
|
|
63
|
-
// ──────────────────────────────────────────────
|
|
64
|
-
|
|
65
|
-
/** Read tokens from cookies in Server Components or Route Handlers. */
|
|
66
5
|
export async function getServerTokens(): Promise<{ accessToken: string | null; refreshToken: string | null }> {
|
|
67
6
|
const { cookies } = await import("next/headers");
|
|
68
7
|
const jar = await cookies();
|
|
69
|
-
return {
|
|
70
|
-
accessToken: jar.get(TOKEN_COOKIE_AT)?.value ?? null,
|
|
71
|
-
refreshToken: jar.get(TOKEN_COOKIE_RT)?.value ?? null
|
|
72
|
-
};
|
|
8
|
+
return { accessToken: jar.get(TOKEN_COOKIE_AT)?.value ?? null, refreshToken: jar.get(TOKEN_COOKIE_RT)?.value ?? null };
|
|
73
9
|
}
|
|
74
10
|
|
|
75
|
-
|
|
76
|
-
// Shared JWT freshness check (used by both client and middleware Edge runtime)
|
|
77
|
-
// ──────────────────────────────────────────────
|
|
78
|
-
|
|
11
|
+
/** UI hint only; the API verifies signature and session on every request. */
|
|
79
12
|
export function isTokenFresh(value: string | null | undefined): boolean {
|
|
80
13
|
if (!value) return false;
|
|
81
|
-
const [,
|
|
82
|
-
if (!
|
|
14
|
+
const [, part] = value.split(".");
|
|
15
|
+
if (!part) return false;
|
|
83
16
|
try {
|
|
84
|
-
const base64 =
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (typeof payload.exp !== "number") return true;
|
|
89
|
-
return payload.exp > Math.floor(Date.now() / 1000) + 5;
|
|
90
|
-
} catch {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
17
|
+
const base64 = part.replace(/-/g, "+").replace(/_/g, "/");
|
|
18
|
+
const payload = JSON.parse(atob(base64.padEnd(Math.ceil(base64.length / 4) * 4, "="))) as { exp?: number };
|
|
19
|
+
return typeof payload.exp === "number" && payload.exp > Date.now() / 1000 + 5;
|
|
20
|
+
} catch { return false; }
|
|
93
21
|
}
|
|
@@ -1,58 +1,17 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from "next/server";
|
|
2
|
-
import { TOKEN_COOKIE_AT, isTokenFresh } from "./lib/tokens";
|
|
2
|
+
import { TOKEN_COOKIE_AT, TOKEN_COOKIE_RT, isTokenFresh } from "./lib/tokens";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function isPublic(pathname: string): boolean {
|
|
8
|
-
return PUBLIC_PREFIXES.some((p) => pathname.startsWith(p));
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export async function middleware(req: NextRequest): Promise<NextResponse> {
|
|
4
|
+
const PUBLIC_PREFIXES = ["/login", "/api", "/_next", "/brand", "/icons"];
|
|
5
|
+
export function middleware(req: NextRequest) {
|
|
12
6
|
const { pathname } = req.nextUrl;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const at = req.cookies.get(TOKEN_COOKIE_AT)?.value;
|
|
17
|
-
|
|
18
|
-
if (isTokenFresh(at)) return NextResponse.next();
|
|
19
|
-
|
|
20
|
-
// Access token missing or expired — try a server-side refresh before redirecting
|
|
21
|
-
const rtCookie = req.cookies.get("oajs_rt")?.value;
|
|
22
|
-
if (rtCookie) {
|
|
23
|
-
const refreshUrl = new URL("/api/auth/refresh", req.url);
|
|
24
|
-
const refreshRes = await fetch(refreshUrl.toString(), {
|
|
25
|
-
method: "POST",
|
|
26
|
-
headers: { cookie: req.headers.get("cookie") ?? "" }
|
|
27
|
-
}).catch(() => null);
|
|
28
|
-
|
|
29
|
-
if (refreshRes?.ok) {
|
|
30
|
-
// Forward the new Set-Cookie from the refresh route to the browser,
|
|
31
|
-
// then let the original request through (the browser will carry new cookies on next request)
|
|
32
|
-
const next = NextResponse.next();
|
|
33
|
-
refreshRes.headers.getSetCookie?.().forEach((c) => next.headers.append("set-cookie", c));
|
|
34
|
-
return next;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// No valid token — redirect to /login with the original path as `next`
|
|
7
|
+
if (PUBLIC_PREFIXES.some(p => pathname === p || pathname.startsWith(p + "/")) || pathname === "/favicon.ico") return NextResponse.next();
|
|
8
|
+
// Let the same-origin client perform a single-flight refresh; middleware must not race it.
|
|
9
|
+
if (isTokenFresh(req.cookies.get(TOKEN_COOKIE_AT)?.value) || req.cookies.get(TOKEN_COOKIE_RT)?.value) return NextResponse.next();
|
|
39
10
|
const loginUrl = req.nextUrl.clone();
|
|
40
11
|
loginUrl.pathname = "/login";
|
|
12
|
+
loginUrl.search = "";
|
|
41
13
|
loginUrl.searchParams.set("next", pathname);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
// Clear stale cookies on the way out
|
|
45
|
-
redirect.cookies.set(TOKEN_COOKIE_AT, "", { path: "/", maxAge: 0 });
|
|
46
|
-
redirect.cookies.set("oajs_rt", "", { path: "/", maxAge: 0 });
|
|
47
|
-
return redirect;
|
|
14
|
+
return NextResponse.redirect(loginUrl);
|
|
48
15
|
}
|
|
49
16
|
|
|
50
|
-
export const config = {
|
|
51
|
-
/**
|
|
52
|
-
* Run middleware on all routes except:
|
|
53
|
-
* - Static assets (_next/static, _next/image)
|
|
54
|
-
* - Public brand/favicon files
|
|
55
|
-
* - API routes are handled separately
|
|
56
|
-
*/
|
|
57
|
-
matcher: ["/((?!_next/static|_next/image|favicon\\.ico|brand/|icons/).*)"]
|
|
58
|
-
};
|
|
17
|
+
export const config = { matcher: ["/((?!_next/static|_next/image|favicon\\.ico|brand/|icons/).*)"] };
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
<svg xmlns="http://www.w3.org/2000/svg"
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" role="img" aria-label="OpenAdminJS">
|
|
2
|
+
<rect width="64" height="64" rx="13" fill="#101820"/>
|
|
3
|
+
<path fill="#B9F56D" d="M29 13a19 19 0 1 0 0 38h9l11-23 7 15H45l-4 8h18L42 13h-8L21 42a11 11 0 1 1 8-21l3-7z"/>
|
|
4
|
+
</svg>
|
|
@@ -9,21 +9,22 @@ const config: Config = {
|
|
|
9
9
|
sans: ["Inter", "ui-sans-serif", "system-ui", "-apple-system", "sans-serif"]
|
|
10
10
|
},
|
|
11
11
|
colors: {
|
|
12
|
+
slate: {50:'#F5F7F2',100:'#e9ede6',200:'#dce2d9',300:'#c4cec3',400:'#78887f',500:'#53615b',600:'#46554d',700:'#34443f',800:'#25352f',900:'#172329',950:'#101820'},
|
|
12
13
|
brand: {
|
|
13
|
-
DEFAULT: "#
|
|
14
|
-
hover: "#
|
|
15
|
-
soft: "#
|
|
14
|
+
DEFAULT: "#426b1d",
|
|
15
|
+
hover: "#365d1b",
|
|
16
|
+
soft: "#e6f6d4",
|
|
16
17
|
border: "#C7D2FE",
|
|
17
|
-
teal: "#
|
|
18
|
-
"teal-soft": "#
|
|
18
|
+
teal: "#426b1d",
|
|
19
|
+
"teal-soft": "#edf4e8"
|
|
19
20
|
}
|
|
20
21
|
},
|
|
21
22
|
boxShadow: {
|
|
22
23
|
card: "0 1px 3px rgba(0,0,0,0.04), 0 8px 24px rgba(0,0,0,0.04)",
|
|
23
24
|
"card-hover": "0 4px 8px rgba(0,0,0,0.06), 0 16px 40px rgba(0,0,0,0.08)",
|
|
24
|
-
"btn-blue": "0 4px 14px rgba(
|
|
25
|
-
"btn-blue-hover": "0 6px 20px rgba(
|
|
26
|
-
"focus-ring": "0 0 0 3px rgba(
|
|
25
|
+
"btn-blue": "0 4px 14px rgba(66,107,29,0.28)",
|
|
26
|
+
"btn-blue-hover": "0 6px 20px rgba(66,107,29,0.4)",
|
|
27
|
+
"focus-ring": "0 0 0 3px rgba(66,107,29,0.12)"
|
|
27
28
|
},
|
|
28
29
|
borderRadius: {
|
|
29
30
|
"2xl": "1rem",
|
|
@@ -3,21 +3,23 @@
|
|
|
3
3
|
"version": "0.1.0",
|
|
4
4
|
"private": true,
|
|
5
5
|
"scripts": {
|
|
6
|
-
"dev": "tsx watch src/main.ts",
|
|
7
6
|
"prisma:generate": "prisma generate --schema ../../prisma/schema.prisma",
|
|
8
|
-
"
|
|
7
|
+
"predev": "prisma generate --schema ../../prisma/schema.prisma",
|
|
8
|
+
"dev": "tsx watch src/main.ts",
|
|
9
|
+
"prebuild": "prisma generate --schema ../../prisma/schema.prisma",
|
|
9
10
|
"build": "nest build",
|
|
10
11
|
"start": "node dist/main.js",
|
|
11
|
-
"prelint": "
|
|
12
|
+
"prelint": "prisma generate --schema ../../prisma/schema.prisma",
|
|
12
13
|
"lint": "tsc -p tsconfig.json --noEmit",
|
|
13
|
-
"pretypecheck": "
|
|
14
|
+
"pretypecheck": "prisma generate --schema ../../prisma/schema.prisma",
|
|
14
15
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
15
|
-
"pretest": "
|
|
16
|
+
"pretest": "prisma generate --schema ../../prisma/schema.prisma",
|
|
16
17
|
"test": "vitest run",
|
|
17
18
|
"db:migrate": "prisma migrate dev --schema ../../prisma/schema.prisma",
|
|
18
19
|
"db:migrate:deploy": "prisma migrate deploy --schema ../../prisma/schema.prisma",
|
|
19
20
|
"db:studio": "prisma studio --schema ../../prisma/schema.prisma",
|
|
20
21
|
"db:reset": "prisma migrate reset --schema ../../prisma/schema.prisma",
|
|
22
|
+
"predb:seed": "prisma generate --schema ../../prisma/schema.prisma",
|
|
21
23
|
"db:seed": "tsx ../../prisma/seed.ts"
|
|
22
24
|
},
|
|
23
25
|
"dependencies": {
|
|
@@ -56,7 +58,9 @@
|
|
|
56
58
|
"stripe": "^17.7.0",
|
|
57
59
|
"winston": "^3.19.0",
|
|
58
60
|
"winston-daily-rotate-file": "^5.0.0",
|
|
59
|
-
"zod": "^3.24.1"
|
|
61
|
+
"zod": "^3.24.1",
|
|
62
|
+
"openid-client": "6.8.2",
|
|
63
|
+
"@openadminjs/nest": "workspace:*"
|
|
60
64
|
},
|
|
61
65
|
"devDependencies": {
|
|
62
66
|
"@nestjs/cli": "11.0.21",
|
|
@@ -37,6 +37,10 @@ const PERMISSION_LABEL_OVERRIDES: Record<string, { en: string; ru: string }> = {
|
|
|
37
37
|
"notifications.delete": { en: "Delete notifications", ru: "Удаление уведомлений" },
|
|
38
38
|
"jobs.read": { en: "View jobs", ru: "Задачи" },
|
|
39
39
|
"jobs.dispatch": { en: "Dispatch jobs", ru: "Запуск задач" },
|
|
40
|
+
"orders.refund": { en: "Refund orders", ru: "Возврат средств" },
|
|
41
|
+
"ai.config.read": { en: "View AI configuration", ru: "Настройки AI (чтение)" },
|
|
42
|
+
"ai.config.update": { en: "Change AI configuration", ru: "Настройки AI (запись)" },
|
|
43
|
+
"system-logs.read": {en:"View system logs",ru:"Системные журналы"},
|
|
40
44
|
"ai.chat": { en: "Use AI consultant", ru: "AI-консультант" }
|
|
41
45
|
};
|
|
42
46
|
|