Add the admin panel for managing site content

Server-rendered CMS at /admin, reachable only with an authenticated admin.
There was no way to change anything on the site without a database session,
while every mutating endpoint used to be anonymous, so the site was either
unmanageable or, before the API lock-down, wide open.

Everything runs through Server Actions, so the httpOnly
parsaaghayi_admin_session cookie set by the backend is read server-side and
forwarded as a bearer token. No token ever reaches client JavaScript and the
browser never talks to the backend cross-origin, so CORS and CSRF do not
apply. Unauthenticated visits to any panel route redirect to /admin/login.

Resources are declared once in a registry (src/app/lib/admin/resources.ts)
and the list, create and edit screens are generated from it, so adding a
content type is a data change rather than three new pages. Covered: posts,
categories, tags, projects, about-me.

Records are read from GET /api/admin/resources/{resource}, which returns
stored rows including every locale; the public endpoints only expose
translated display payloads and cannot populate an editing form.

Verified: tsc --noEmit clean, eslint clean, next build compiles and registers
all five admin routes as dynamic, and /admin, /admin/posts redirect 307 to
/admin/login while /admin/login renders.

Post bodies are rendered as text nodes, never as HTML.
This commit is contained in:
root committed 2026-10-01 16:01:40 -04:00
1 parent 76cf4da118
commit 2b7eda656a
16 files changed
+1242

No files matched your search

@@ -0,0 +1,33 @@
import { notFound } from 'next/navigation';
import { loadOptions, loadRows, optionSources } from '@/app/lib/admin/load';
import { RESOURCE_BY_SLUG, type AdminRow } from '@/app/lib/admin/resources';
import ResourceForm from '@/app/lib/admin/resource-form';
import { PageHeader } from '@/app/lib/admin/ui';
export const dynamic = 'force-dynamic';
export default async function EditResourcePage({
params,
}: {
params: Promise<{ resource: string; id: string }>;
}) {
const { resource: slug, id } = await params;
const resource = RESOURCE_BY_SLUG[slug];
if (!resource) notFound();
const rows = await loadRows(resource.slug);
const row: AdminRow | undefined = rows.find((entry) => String(entry.id) === id);
if (!row) notFound();
const options = await loadOptions(optionSources(resource));
return (
<>
<PageHeader title={`ویرایش ${resource.singular}`} description={resource.hint} />
<ResourceForm resource={resource} row={row} options={options} />
</>
);
}
@@ -0,0 +1,32 @@
import { notFound } from 'next/navigation';
import { loadOptions, optionSources } from '@/app/lib/admin/load';
import { RESOURCE_BY_SLUG } from '@/app/lib/admin/resources';
import ResourceForm from '@/app/lib/admin/resource-form';
import { Alert, PageHeader } from '@/app/lib/admin/ui';
export const dynamic = 'force-dynamic';
export default async function NewResourcePage({
params,
}: {
params: Promise<{ resource: string }>;
}) {
const { resource: slug } = await params;
const resource = RESOURCE_BY_SLUG[slug];
if (!resource || !resource.supportsCreate) notFound();
const options = await loadOptions(optionSources(resource));
return (
<>
<PageHeader title={`${resource.singular} جدید`} description={resource.hint} />
{Object.keys(options).length === 0 ? (
<Alert tone="info">هیچ گزینه‌ای برای انتخاب وجود ندارد.</Alert>
) : null}
<ResourceForm resource={resource} row={null} options={options} />
</>
);
}
@@ -0,0 +1,87 @@
import Link from 'next/link';
import { notFound } from 'next/navigation';
import { deleteResourceAction } from '@/app/lib/admin/actions';
import { cellValue, RESOURCE_BY_SLUG, type AdminRow } from '@/app/lib/admin/resources';
import { loadRows } from '@/app/lib/admin/load';
import { Card, EmptyState, PageHeader } from '@/app/lib/admin/ui';
export const dynamic = 'force-dynamic';
export default async function ResourceListPage({
params,
}: {
params: Promise<{ resource: string }>;
}) {
const { resource: slug } = await params;
const resource = RESOURCE_BY_SLUG[slug];
if (!resource) notFound();
let rows: AdminRow[] = [];
try {
rows = await loadRows(resource.slug);
} catch {
rows = [];
}
return (
<>
<PageHeader
title={resource.title}
description={resource.hint}
action={
resource.supportsCreate ? (
<Link
href={`/admin/${resource.slug}/new`}
className="inline-flex items-center justify-center rounded-lg bg-primary px-4 py-2 text-sm font-medium text-primary-foreground transition hover:opacity-90"
>
افزودن {resource.singular}
</Link>
) : undefined
}
/>
{rows.length === 0 ? (
<EmptyState message={`هنوز ${resource.singular} ثبت نشده است.`} />
) : (
<div className="flex flex-col gap-3">
{rows.map((row) => (
<Card key={row.id} className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div className="min-w-0">
{resource.listColumns.map((column) => (
<p key={column.key} className="truncate text-sm text-foreground">
<span className="text-muted-foreground">{column.label}: </span>
{cellValue(row, column.key) || '—'}
</p>
))}
</div>
<div className="flex shrink-0 items-center gap-4 text-sm">
<Link
href={`/admin/${resource.slug}/${row.id}`}
className="text-primary underline-offset-4 hover:underline"
>
ویرایش
</Link>
{resource.supportsDelete ? (
<form action={deleteResourceAction}>
<input type="hidden" name="__resource" value={resource.slug} />
<input type="hidden" name="__id" value={row.id} />
<button
type="submit"
className="text-destructive underline-offset-4 hover:underline"
>
حذف
</button>
</form>
) : null}
</div>
</Card>
))}
</div>
)}
</>
);
}
+18
View File
@@ -0,0 +1,18 @@
import { redirect } from 'next/navigation';
import { currentAdmin } from '@/app/lib/admin/api';
import AdminNav from './nav';
export const dynamic = 'force-dynamic';
export default async function PanelLayout({ children }: { children: React.ReactNode }) {
const admin = await currentAdmin();
if (!admin) redirect('/admin/login');
return (
<div className="mx-auto flex w-full max-w-[1200px] flex-col gap-8 px-5 py-10 lg:flex-row">
<AdminNav email={admin.email} />
<div className="min-w-0 flex-1">{children}</div>
</div>
);
}
+53
View File
@@ -0,0 +1,53 @@
import Link from 'next/link';
import { logoutAction } from '@/app/lib/admin/actions';
import { RESOURCES } from '@/app/lib/admin/resources';
export default function AdminNav({ email }: { email: string }) {
return (
<aside className="flex w-full shrink-0 flex-col gap-6 lg:sticky lg:top-10 lg:w-60">
<div>
<p className="text-sm font-bold text-foreground">پنل مدیریت</p>
<p className="mt-1 truncate text-xs text-muted-foreground" dir="ltr">
{email}
</p>
</div>
<nav className="flex flex-row flex-wrap gap-1 lg:flex-col">
<Link
href="/admin"
className="rounded-lg px-3 py-2 text-sm text-muted-foreground transition hover:bg-accent hover:text-foreground"
>
پیشخوان
</Link>
{RESOURCES.map((resource) => (
<Link
key={resource.slug}
href={`/admin/${resource.slug}`}
className="rounded-lg px-3 py-2 text-sm text-muted-foreground transition hover:bg-accent hover:text-foreground"
>
{resource.title}
</Link>
))}
</nav>
<div className="mt-auto flex flex-col gap-2">
<Link
href="/"
className="rounded-lg border border-border px-3 py-2 text-center text-sm text-foreground transition hover:bg-accent"
>
مشاهده سایت
</Link>
<form action={logoutAction}>
<button
type="submit"
className="w-full rounded-lg border border-border px-3 py-2 text-sm text-destructive transition hover:bg-destructive/10"
>
خروج
</button>
</form>
</div>
</aside>
);
}
+54
View File
@@ -0,0 +1,54 @@
import Link from 'next/link';
import { loadRows } from '@/app/lib/admin/load';
import { RESOURCES } from '@/app/lib/admin/resources';
import { Card, PageHeader } from '@/app/lib/admin/ui';
export const dynamic = 'force-dynamic';
export default async function AdminDashboardPage() {
const counts = await Promise.all(
RESOURCES.map(async (resource) => {
try {
const rows = await loadRows(resource.slug);
return { resource, total: rows.length };
} catch {
return { resource, total: -1 };
}
}),
);
const draftPosts = await (async () => {
try {
const rows = await loadRows('posts');
return rows.filter((row) => !row.published_at).length;
} catch {
return -1;
}
})();
return (
<>
<PageHeader title="پیشخوان" description="وضعیت محتوای سایت." />
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
{counts.map(({ resource, total }) => (
<Link key={resource.slug} href={`/admin/${resource.slug}`}>
<Card className="h-full transition hover:border-primary">
<p className="text-sm text-muted-foreground">{resource.title}</p>
<p className="mt-2 text-3xl font-bold text-foreground">
{total === -1 ? '—' : total.toLocaleString('fa-IR')}
</p>
</Card>
</Link>
))}
</div>
<Card className="mt-6">
<p className="text-sm text-muted-foreground">نوشته‌های پیش‌نویس</p>
<p className="mt-2 text-2xl font-bold text-foreground">
{draftPosts === -1 ? '—' : draftPosts.toLocaleString('fa-IR')}
</p>
</Card>
</>
);
}
+13
View File
@@ -0,0 +1,13 @@
import type { Metadata } from 'next';
import type { ReactNode } from 'react';
export const metadata: Metadata = {
title: 'پنل مدیریت',
robots: { index: false, follow: false, nocache: true },
};
export default function AdminLayout({ children }: { children: ReactNode }) {
return (
<div className="min-h-screen w-full bg-background text-foreground">{children}</div>
);
}
+25
View File
@@ -0,0 +1,25 @@
import { redirect } from 'next/navigation';
import { currentAdmin } from '@/app/lib/admin/api';
import LoginForm from '@/app/lib/admin/login-form';
import { Card } from '@/app/lib/admin/ui';
export const dynamic = 'force-dynamic';
export default async function AdminLoginPage() {
if (await currentAdmin()) redirect('/admin');
return (
<div className="flex min-h-screen items-center justify-center px-4 py-16">
<Card className="w-full max-w-sm">
<div className="mb-6">
<h1 className="text-xl font-bold text-foreground">پنل مدیریت</h1>
<p className="mt-2 text-sm text-muted-foreground">
برای مدیریت محتوای سایت وارد شوید.
</p>
</div>
<LoginForm />
</Card>
</div>
);
}