หน้าตัวอย่าง
ไม่มีสิทธิ์ 403
ใช้เมื่อผู้ใช้เข้าสู่ระบบแล้ว แต่บัญชีของเขาไม่มีสิทธิ์ที่หน้านั้นต้องใช้ หน้าเรียก forbidden() ของ Next.js แล้ว forbidden.tsx ที่ใกล้ที่สุดแสดงผล ถ้ายังไม่เข้าสู่ระบบ ให้พาไปหน้าเข้าสู่ระบบแทน ไม่ใช่ 403 ถ้าสิ่งที่ขอไม่มีอยู่ ใช้หน้าไม่พบหน้า 404
คอมโพเนนต์ที่ใช้
- StatusPageA page for an outcome instead of the content: the page wasn't found, the person may not open it, the server failed, or the system is closed for maintenance.
- ButtonA button, or a link that looks like one with href.
- AppShellThe application frame: the left menu, which collapses to icons on wide screens and slides in on phones, and the top bar with the breadcrumb, global search, language switch, notifications and account menu.
import { StatusPage, Button, AppShell } from "@bsru/ui";กฎของหน้านี้
ถ้อยคำ
- บอกว่าทำอะไรไม่ได้ และต้องมีสิทธิ์อะไร โดยใช้ชื่อที่ผู้ใช้รู้จัก เช่น ผู้อนุมัติของหน่วยงาน ไม่ใช่ชื่อสิทธิ์ในระบบ
- บอกว่าติดต่อใครเพื่อขอสิทธิ์ ระบบยังไม่มีการขอสิทธิ์ในหน้าจอ
ปุ่มและทางไปต่อ
- ปุ่มหลักหนึ่งปุ่ม: กลับหน้าแรก
- เมื่อผู้ใช้อาจเข้าผิดบัญชี เช่น ผู้ใช้ ERP เข้าคอนโซล ให้ปุ่มเข้าสู่ระบบด้วยบัญชีอื่น และลิงก์ไประบบที่ถูกต้อง
ห้ามแสดง
- ชื่อสิทธิ์ภายใน บทบาท หรือกฎที่ตรวจไม่ผ่าน
- ข้อมูลใดของหน้าที่ถูกปฏิเสธ รวมถึงชื่อหรือจำนวนของระเบียน
ในแอป
- ตรวจสิทธิ์ก่อนโหลดข้อมูล และตอบด้วยสถานะ HTTP 403 ซึ่ง forbidden() ทำให้ สิทธิ์จริงตรวจที่ API หลักเสมอ หน้านี้เพียงบอกผู้ใช้
- ในโครงแอปส่ง fill={false} นอกโครงแอปใช้แบบเต็มหน้าจอ พร้อม brand และ notice ในระบบจำลอง
ตัวอย่างทั้งหน้า
แต่ละตัวอย่างเป็นหน้าเต็ม แสดงในกรอบจอคอมพิวเตอร์ เลือกโทรศัพท์หรือแท็บเล็ตที่แถบบนเพื่อดูบนจอนั้น ข้อมูลทั้งหมดเป็นข้อมูลสมมติ
ERP: ไม่มีสิทธิ์ ในโครงแอป
ผู้ใช้ ERP เปิดหน้าอนุมัติโดยไม่ได้เป็นผู้อนุมัติ forbidden.tsx แสดงใน AppShell บอกสิทธิ์ที่ต้องมีและติดต่อใคร
// next.config.ts: forbidden() and forbidden.tsx need authInterrupts.
const nextConfig: NextConfig = { experimental: { authInterrupts: true } };
// app/(erp)/vouchers/approve/page.tsx: the page checks the right before it loads anything.
import { forbidden } from "next/navigation";
const user = await requireUser();
if (!user.can("voucher.approve")) forbidden();
// app/(erp)/forbidden.tsx: shown inside the ERP's AppShell.
import { Button, StatusPage } from "@bsru/ui";
import { House } from "lucide-react";
import { getTranslations } from "next-intl/server";
export default async function Forbidden() {
const t = await getTranslations();
return (
<StatusPage
kind="forbidden"
fill={false}
title={t("vouchers.forbidden.title")}
description={t("vouchers.forbidden.body")}
actions={<Button tone="primary" icon={House} label={t("status.home")} href="/" />}
/>
);
}| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "forbidden" | StatusKind ค่าที่ใช้ได้: "notFound""forbidden""serverError""maintenance" | notFound for 404, forbidden for 403, serverError for 500 and the other 5xx, maintenance for a planned closure. |
| fill | false | boolean | true fills the screen as a page of its own, with its own main landmark. false sits in AppShell's content area, in the app's main. |
| title | "คุณไม่มีสิทธิ์อนุมัติใบขอเบิก" | string | What happened, the page's level 1 heading. Defaults to a title for the kind in the screen language. |
| description | "หน้านี้เปิดได้เฉพาะผู้อนุมัติของหน่วยงาน ถ้าต้องใช้งาน ติดต่อผู้ดูแลระบบของหน่วยงาน" | string | What to do next, in a sentence or two. Defaults to a sentence for the kind in the screen language. |
| actions | <Button tone="primary" icon={House} label="กลับหน้าแรก" href="/" /> | ReactNode | The ways forward, as Button components, with the primary one first, such as Back to the home page. |
คอนโซล: เข้าด้วยบัญชีผู้ใช้ ERP
คอนโซลเปิดได้เฉพาะผู้ดูแลระบบ หน้าเต็มจอนี้ให้เข้าสู่ระบบด้วยบัญชีอื่น หรือไปที่ BSRU ERP
// app/forbidden.tsx: an ERP user who signs in to the console, before the console's frame.
import { Button, StatusPage } from "@bsru/ui";
import { ExternalLink, LogIn } from "lucide-react";
import { getTranslations } from "next-intl/server";
import { erpUrl, isDemo } from "@/server/env";
export default async function Forbidden() {
const t = await getTranslations();
return (
<StatusPage
kind="forbidden"
brand={{ name: t("brand.console"), caption: t("brand.university"), logo: "/bsru-logo.png", alt: t("brand.seal") }}
notice={isDemo() ? t("shell.demo") : undefined}
title={t("status.wrongUserType.title")}
description={t("status.wrongUserType.body")}
actions={
<>
<Button tone="primary" icon={LogIn} label={t("auth.switchAccount")} href="/auth/login?prompt=login" />
<Button icon={ExternalLink} label={t("status.openErp")} href={erpUrl()} />
</>
}
/>
);
}| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| kind* จำเป็น | "forbidden" | StatusKind ค่าที่ใช้ได้: "notFound""forbidden""serverError""maintenance" | notFound for 404, forbidden for 403, serverError for 500 and the other 5xx, maintenance for a planned closure. |
| brand | ออบเจกต์ 4 ฟิลด์{
name: "BSRU ERP คอนโซลผู้ดูแลระบบ",
caption: "มหาวิทยาลัยราชภัฏบ้านสมเด็จเจ้าพระยา",
logo: "/bsru-logo.png",
alt: "ตรามหาวิทยาลัยราชภัฏบ้านสมเด็จเจ้าพระยา",
} | PageBrand | The logo and system name at the top of a whole page. Leave it out inside AppShell, which shows its own. |
| notice | "ระบบจำลอง ใช้ข้อมูลทดสอบเท่านั้น" | string | The gold strip across the top of a whole page, for demo and test systems, such as ระบบจำลอง. |
| title | "บัญชีนี้ใช้คอนโซลไม่ได้" | string | What happened, the page's level 1 heading. Defaults to a title for the kind in the screen language. |
| description | "คอนโซลเปิดได้เฉพาะผู้ดูแลระบบ ถ้าคุณใช้งาน BSRU ERP ให้เข้าสู่ระบบที่นั่น หรือเข้าสู่ระบบด้วยบัญชีผู้ดูแลระบบ" | string | What to do next, in a sentence or two. Defaults to a sentence for the kind in the screen language. |