ရိုးရိုးလေး ရှင်းပြမယ်
Authentication နဲ့ authorization ကို ရိုးရိုးလေး ပြောရရင်
လေဆိပ်ကို စဉ်းစားကြည့်ပါ။ ဝင်ပေါက်မှာ passport စစ်တာက authentication ပါ: 'ခင်ဗျား ဘယ်သူလဲ'။ Gate မှာ boarding pass စစ်တာက authorization ပါ: 'ဒီ လေယာဉ်ကို စီးခွင့် ရှိလား'။ Terminal ထဲ ရောက်နေတာက လေယာဉ်တိုင်းကို စီးခွင့် ပေးတာ မဟုတ်ပါဘူး။
Web app မှာလည်း အတူတူပါပဲ။ Login ဝင်ထားတာက authentication ပြီးပြီ ဆိုတာပဲ ပြပါတယ်။ Note တစ်ခုကို ဖျက်ခွင့် ရှိမရှိဆိုတာ သီးခြား မေးခွန်းပါ။ Delete button ကို ဖျောက်ထားတာက တံခါးပေါ်က ဆိုင်းဘုတ်ကို ဖြုတ်တာနဲ့ တူပြီး တံခါးကို သော့ခတ်တာ မဟုတ်ပါဘူး။
Problem → Why → Solution
ပြဿနာ: ဖျောက်ထားတဲ့ button တစ်ခုပဲ အကာအကွယ်
Note page က owner ကိုပဲ Delete button ပြပြီး layout က login မဝင်ထားသူကို /login ဆီ redirect လုပ်ပါတယ်။ Team က လုံခြုံပြီလို့ ထင်ပါတယ်။ ဒါပေမဲ့ deleteNote action ကိုယ်တိုင်က ဘယ် id ကိုမဆို လက်ခံပါတယ်။ Login ဝင်ထားတဲ့ member တစ်ယောက်က devtools နဲ့ request ကို ပြန်ပို့ပြီး id ကို တခြားသူ့ note id နဲ့ လဲလိုက်ရင် အဲဒီ note ပျောက်သွားပါတယ်။ Layout ကလည်း navigation တိုင်း ပြန် render မလုပ်လို့ data ကို ကာကွယ်ပေးတဲ့ နေရာ မဟုတ်ပါဘူး။
// the only "protection" is in the UI
{session?.user.id === note.ownerId && (
<form action={deleteNote.bind(null, note.id)}>
<button>Delete</button>
</form>
)}
// app/notes/actions.ts
"use server";
export async function deleteNote(noteId: string) {
await db.note.delete({ where: { id: noteId } }); // anyone can call this
}လုံခြုံရေးက data ရှိတဲ့နေရာမှာ ရှိရတယ်
Attacker က သင့် UI ကို သုံးစရာ မလိုပါဘူး၊ server ကို တိုက်ရိုက် request ပို့နိုင်ပါတယ်။ ဒါကြောင့် 'ဒီ user က ဒီ record ကို ဒီ action လုပ်ခွင့်ရှိလား' ဆိုတဲ့ မေးခွန်းကို data ကို ဖတ်တဲ့၊ ပြောင်းတဲ့ server code ထဲမှာ ဖြေရပါမယ်။ UI ဘက်က စစ်ဆေးချက်တွေက user experience အတွက်ပဲ ဖြစ်ပါတယ်။
Core concept: ဘယ်သူလဲ၊ ဘာလုပ်ခွင့်ရှိလဲ၊ ဘယ်မှာ စစ်မလဲ
Authentication က identity ကို အတည်ပြုပါတယ်။ ပုံမှန်အားဖြင့် login အောင်မြင်ရင် session တစ်ခု ဖန်တီးပြီး browser ကို cookie ပေးပါတယ်။ အဲဒီ cookie က encrypt/sign လုပ်ထားတဲ့ session data ကိုယ်တိုင် ဖြစ်နိုင်သလို database ထဲက session ကို ညွှန်းတဲ့ id ဖြစ်နိုင်ပါတယ်။ Authorization ကတော့ identity ရပြီးမှ မေးတဲ့ မေးခွန်းပါ: ဒီ user က ဒီ resource ပေါ်မှာ ဒီ action ကို လုပ်ခွင့်ရှိလား။ အဖြေက ownership, role ဒါမှမဟုတ် subscription plan ပေါ် မူတည်ပါတယ်။ Authentication fail ရင် 401 ဒါမှမဟုတ် login ဆီ redirect ဖြစ်ပြီး authorization fail ရင် 403 (ဒါမှမဟုတ် ရှိကြောင်း မသိစေချင်ရင် 404) ဖြစ်ပါတယ်။
Session cookie ကို flag တွေနဲ့ ကာကွယ်ပါ: httpOnly က JavaScript ဖတ်လို့ မရအောင် လုပ်လို့ XSS bug တစ်ခုက token ကို ခိုးလို့ မရပါဘူး။ secure က HTTPS ပေါ်မှာပဲ ပို့ပြီး sameSite: "lax" က အခြား site ကနေ စတဲ့ POST request တွေမှာ cookie မပါအောင် လုပ်ကာ CSRF ကို လျှော့ပေးပါတယ်။ Password hashing, OAuth, session rotation တွေကို ကိုယ်တိုင် မရေးဘဲ maintain လုပ်နေတဲ့ auth library ကို သုံးတာ ပိုလုံခြုံပါတယ်၊ Next.js authentication guide မှာ library အများအပြား စာရင်း ပါပါတယ်။
စစ်ဆေးချက်တွေကို အလွှာလိုက် ထားပါ။ Middleware (Next.js 16 မှာ proxy လို့ နာမည်ပြောင်းထားပါတယ်) က cookie ရှိမရှိ ကြည့်ပြီး optimistic redirect လုပ်နိုင်ပါတယ်။ Layout တွေက UX အတွက် စစ်နိုင်ပေမဲ့ partial rendering ကြောင့် navigation တိုင်း ပြန် run မှာ မဟုတ်ပါဘူး။ တကယ့် စစ်ဆေးချက်က data နဲ့ အနီးဆုံးမှာ ရှိရပါမယ်: verifySession() လို helper တစ်ခုကို data function, Server Action နဲ့ Route Handler တိုင်းက ခေါ်ပါတယ်။
Authorization rule ကို canDeleteNote(user, note) လို pure function အဖြစ် ရေးပါ: session, database, Next.js API တွေနဲ့ မဆိုင်ဘဲ data ကို ယူပြီး true/false ပြန်ပေးတာပါ။ ဒီလို ရေးရင် role တိုင်းအတွက် test လုပ်ရ လွယ်ပြီး button ပြမပြ ဆုံးဖြတ်တဲ့ component ကလည်း rule တူတူကို ပြန်သုံးနိုင်ပါတယ်။ Rule ကို နေရာနှစ်ခုမှာ ကော်ပီကူး မရေးရတော့ပါဘူး။
| မေးခွန်း | Fail ရင် | ဘယ်မှာ စစ် |
|---|---|---|
| ဘယ်သူလဲ? (authentication) | 401 / /login ဆီ redirect | verifySession helper |
| ခွင့်ရှိလား? (authorization) | 403 ဒါမှမဟုတ် 404 | Action နဲ့ data function |
| Button ပြမလား? | Button ကို ဖျောက် | Component (UX သက်သက်) |
- Request က cookie နဲ့ ရောက်လာ
- Authenticate: session ကို ဖတ်ပြီး verify
- Resource ကို database ကနေ load
- Authorize: owner လား၊ role ရှိလား?
- အဲဒီအခါမှ data ကို ဖတ် ဒါမှမဟုတ် ပြောင်း
Authorization ကို resource ကို load လုပ်ပြီးမှ စစ်လို့ ရပါတယ်၊ ownerId က database ထဲမှာ ရှိပြီး request ထဲမှာ မရှိလို့ပါ။
အထွေထွေ ပုံစံ
export async function protectedAction(resourceId: string) {
const user = await verifySession(); // who? (401 / redirect)
const resource = await loadResource(resourceId);
if (!resource) return { error: "Not found" }; // 404
if (!canModify(user, resource)) return { error: "Forbidden" }; // what? (403)
await changeResource(resource);
}Session ကို အရင်စစ်၊ resource ကို load လုပ်၊ permission rule (pure function) နဲ့ ဆုံးဖြတ်ပြီးမှ data ကို ပြောင်းပါ။
Worked example: verifySession နဲ့ ကာကွယ်ထားတဲ့ deleteNote
Session နဲ့ owner ကို စစ်တာက authorization ရဲ့ အဓိကပါ။ ဒါပေမဲ့ တကယ့် app တွေမှာ session ဘယ်ကလာလဲ သိဖို့နဲ့ admin လို role တွေကို support လုပ်ဖို့လည်း လိုပါတယ်။ ဒီမှာ auth ကို server-only helper file ထဲ ခွဲထားပြီး permission rule ကို သီးခြား test လုပ်လို့ရတဲ့ pure function အဖြစ် ထားကာ error ကို throw မလုပ်ဘဲ form က ပြလို့ရတဲ့ state အဖြစ် ပြန်ပေးပါတယ်။
// lib/auth.ts
import "server-only";
import { cache } from "react";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { readSession } from "@/lib/session"; // verifies and decodes the cookie
export type User = { id: string; role: "member" | "admin" };
export const verifySession = cache(async (): Promise<User> => {
const token = (await cookies()).get("session")?.value;
const user = token ? await readSession(token) : null;
if (!user) redirect("/login");
return user;
});
export function canDeleteNote(user: User, note: { ownerId: string }) {
return user.role === "admin" || note.ownerId === user.id;
}
// app/notes/actions.ts
"use server";
import { revalidatePath } from "next/cache";
import { canDeleteNote, verifySession } from "@/lib/auth";
import { db } from "@/lib/db";
export async function deleteNote(noteId: string) {
const user = await verifySession(); // authentication
const note = await db.note.findUnique({
where: { id: noteId },
select: { ownerId: true },
});
if (!note) return { error: "Note not found" };
if (!canDeleteNote(user, note)) return { error: "You can only delete your own notes" };
await db.note.delete({ where: { id: noteId } });
revalidatePath("/notes");
return { ok: true };
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Read the cookie on the server
const token = (await cookies()).get("session")?.value;Next.js 15 ကစပြီး cookies() က await လုပ်ရတဲ့ async function ဖြစ်ပါတယ်။ Token ကို readSession က verify လုပ်ပြီးမှ ယုံပါတယ်။
- No session, no entry
if (!user) redirect("/login");redirect() က never type ဖြစ်လို့ ဒီ line နောက်မှာ TypeScript က user ကို User လို့ သိပါတယ်။ cache() ကြောင့် request တစ်ခုအတွင်း ဘယ်နှခါ ခေါ်ခေါ် verify တစ်ခါပဲ run ပါတယ်။
- A pure permission rule
return user.role === "admin" || note.ownerId === user.id;Rule ကို function သီးသန့်ထဲ ထားလို့ UI (button ပြဖို့) နဲ့ action (တကယ် ကာကွယ်ဖို့) နှစ်ခုလုံးက မျှသုံးနိုင်ပြီး unit test ရေးရ လွယ်ပါတယ်။
- Authenticate inside the action
const user = await verifySession(); // authenticationPage က login ကို စစ်ပြီးသားဖြစ်ပါစေ action ထဲမှာ ထပ်စစ်ပါတယ်၊ action ကို တိုက်ရိုက် ခေါ်လို့ ရလို့ပါ။
- Authorize before deleting
if (!canDeleteNote(user, note)) return { error: "You can only delete your own notes" };Database ကနေ ownerId ကို ယူပြီး နှိုင်းယှဉ်ပါတယ်၊ client က ပို့တဲ့ ownerId ကို ဘယ်တော့မှ မယုံပါဘူး။
Login မဝင်ထားတဲ့ request ကို /login ဆီ redirect လုပ်ပါတယ်။ တခြားသူ့ note ကို ဖျက်ဖို့ ကြိုးစားတဲ့ member က "You can only delete your own notes" ကို ရပြီး ဘာမှ မပျက်ပါဘူး။ Owner ဒါမှမဟုတ် admin ကတော့ note ကို ဖျက်ပြီး /notes list က refresh ဖြစ်ပါတယ်။Session cookie ကို set လုပ်ခြင်း
Login အောင်မြင်တဲ့အခါ Server Action ဒါမှမဟုတ် Route Handler က cookie ကို set လုပ်ပါတယ်။ Auth library တစ်ခုခု သုံးရင် ဒါကို library က လုပ်ပေးပေမဲ့ flag တစ်ခုချင်းရဲ့ အဓိပ္ပာယ်ကို သိထားရင် configuration ကို review လုပ်ရ လွယ်ပါတယ်။ Token ကို localStorage ထဲ မထားပါနဲ့၊ page ပေါ်က script တိုင်း ဖတ်လို့ ရလို့ပါ။
export async function createSessionCookie(token: string, expiresAt: Date) {
(await cookies()).set("session", token, {
httpOnly: true, // invisible to page JavaScript
secure: process.env.NODE_ENV === "production", // HTTPS only in production
sameSite: "lax", // not sent on cross-site POSTs
path: "/",
expires: expiresAt,
});
}လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Production app အများစုမှာ authentication ကို library ဒါမှမဟုတ် hosted service က လုပ်ပေးပြီး authorization rule တွေကိုတော့ team ကိုယ်တိုင် ရေးရပါတယ်၊ business နဲ့ တိုက်ရိုက် ဆိုင်လို့ပါ။
- SaaS app: user က organization member ဖြစ်မှ အဲဒီ organization ရဲ့ project တွေကို မြင်ရပြီး query တိုင်းကို organizationId နဲ့ scope လုပ်ပါတယ်။
- Admin dashboard: role-based rule (admin, editor, viewer) တွေကို permission map တစ်ခုထဲမှာ ကြေညာပြီး action တိုင်းက can(user, "post:publish") လို helper ကို ခေါ်ပါတယ်။
- Content site: editor က draft တိုင်းကို ပြင်နိုင်ပေမဲ့ author ကတော့ ကိုယ့် draft ကိုပဲ ပြင်နိုင်ပါတယ်။
- Public API: Route Handler က Authorization header ထဲက API key ကို စစ်ပြီး key တစ်ခုချင်းရဲ့ scope အတိုင်းပဲ ခွင့်ပြုပါတယ်။
Security review လုပ်တဲ့အခါ reviewer တွေက 'ဒီ action ကို ဘယ်သူ ခေါ်နိုင်လဲ၊ ဘယ်မှာ စစ်ထားလဲ' ဆိုတာကို action တစ်ခုချင်း လိုက်မေးလေ့ရှိပါတယ်။ Helper တစ်ခုတည်းကို action တိုင်းရဲ့ ထိပ်မှာ ခေါ်ထားရင် အဲဒီ မေးခွန်းကို စက္ကန့်ပိုင်းအတွင်း ဖြေနိုင်ပါတယ်။
အမှားများ
Auth နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
ဖျောက်ထားတဲ့ button ကို authorization လို့ ထင်ခြင်း
Button မပေါ်တာက request ကို မတားပါဘူး။ Server Action နဲ့ Route Handler တိုင်းက permission ကို ကိုယ်တိုင် စစ်ရပါမယ်၊ UI က အဲဒီ rule တူတူကို button ပြဖို့ပဲ သုံးပါ။
Layout ဒါမှမဟုတ် middleware တစ်ခုတည်းမှာ စစ်ပြီး ပြီးပြီ ထင်ခြင်း
Layout က navigation တိုင်း ပြန် run မှာ မဟုတ်ဘဲ action တွေကို layout မဖြတ်ဘဲ ခေါ်လို့ ရပါတယ်။ 2025 မှာ crafted header တစ်ခုနဲ့ Next.js middleware ကို ကျော်လို့ရတဲ့ vulnerability တစ်ခုလည်း ထွက်ခဲ့ပါတယ်၊ middleware တစ်ခုတည်းကို အားကိုးရင် ဘယ်လို အန္တရာယ်ရှိလဲ ပြတဲ့ ဥပမာကောင်းပါ။
Client က ပို့တဲ့ userId ဒါမှမဟုတ် role ကို ယုံခြင်း
Form field, query string, localStorage ဒါမှမဟုတ် verify မလုပ်ရသေးတဲ့ token ထဲက role က user ပြင်လို့ရတဲ့ input ပါ။ Identity ကို verify လုပ်ထားတဲ့ session ကနေပဲ ယူပါ။
Session token ကို JavaScript ဖတ်လို့ရတဲ့ နေရာမှာ ထားခြင်း
httpOnly မပါတဲ့ cookie ဒါမှမဟုတ် localStorage ထဲက token ကို XSS bug တစ်ခုက ခိုးပြီး account ကို ယူသွားနိုင်ပါတယ်။
Best Practices
Best practices
- Permission rule တွေကို canDeleteNote လို pure function အဖြစ် ရေးပြီး role တစ်ခုချင်းအတွက် unit test ရေးပါ။
- Query ကို ownerId ဒါမှမဟုတ် organizationId နဲ့ scope လုပ်ပါ၊ ဒါမှ စစ်ဆေးချက် မေ့သွားရင်တောင် data ပေါက်ကြားမှု နည်းပါတယ်။
- Password နဲ့ OAuth ကို ကိုယ်တိုင် မရေးဘဲ maintain လုပ်ထားတဲ့ library ကို သုံးပြီး update ကို အမြဲ လုပ်ပါ။
Mini challenge
Mini challenge: permission matrix
Note update အတွက် authorization rule ကို pure function နှစ်ခုနဲ့ ရေးပြီး user အမျိုးမျိုးနဲ့ note နှစ်ခုအတွက် ရလဒ် ဇယားကို print ထုတ်ပါ။ Admin က အားလုံးကို ပြင်နိုင်ပြီး archive လုပ်ထားတဲ့ note ကို admin ကပဲ ပြင်နိုင်ပါတယ်။ Editor က archive မဟုတ်တဲ့ note တိုင်းကို ပြင်နိုင်ပြီး member က ကိုယ့် note ကိုပဲ ပြင်နိုင်ပါတယ်။
- canUpdateNote(user, note) က boolean ပြန်ပေးရမယ်
- authorizeUpdate(user | null, note) က "unauthenticated", "forbidden" ဒါမှမဟုတ် "ok" ပြန်ပေးရမယ်
- Guest, owner member, တခြား member, editor, admin အတွက် note တစ်ခုချင်းရဲ့ ရလဒ်ကို print ထုတ်ပါ
type Role = "member" | "editor" | "admin";
type User = { id: string; role: Role };
type Note = { id: string; ownerId: string; archived: boolean };
type Decision = "ok" | "unauthenticated" | "forbidden";
function canUpdateNote(user: User, note: Note): boolean {
// TODO: admin -> always
// TODO: archived -> nobody else
// TODO: editor -> any note; member -> only their own
}
function authorizeUpdate(user: User | null, note: Note): Decision {
// TODO: no user -> "unauthenticated"; otherwise use canUpdateNote
}
// TODO: two notes (one archived), five people (one of them a guest),
// print one line per note: name=decision ...ရလဒ်: n1 မှာ guest က unauthenticated၊ ada, eve, root က ok၊ bo က forbidden ဖြစ်ပါတယ်။ Archive လုပ်ထားတဲ့ n2 မှာတော့ root (admin) ကပဲ ok ဖြစ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
Rule တွေကို အစီအစဉ်တကျ စစ်ပါ: admin ကို အရင်၊ archived ကို နောက်၊ အဲဒီနောက်မှ editor နဲ့ owner။ Authentication (user ရှိမရှိ) နဲ့ authorization (rule) ကို function နှစ်ခုခွဲထားရင် 401 နဲ့ 403 ကွာခြားချက် ရှင်းနေပါတယ်။
အဖြေ ကြည့်ရန်
type Role = "member" | "editor" | "admin";
type User = { id: string; role: Role };
type Note = { id: string; ownerId: string; archived: boolean };
type Decision = "ok" | "unauthenticated" | "forbidden";
function canUpdateNote(user: User, note: Note): boolean {
if (user.role === "admin") return true;
if (note.archived) return false;
return user.role === "editor" || note.ownerId === user.id;
}
function authorizeUpdate(user: User | null, note: Note): Decision {
if (!user) return "unauthenticated"; // 401
return canUpdateNote(user, note) ? "ok" : "forbidden"; // 403
}
const people: [string, User | null][] = [
["guest", null],
["ada", { id: "u1", role: "member" }],
["bo", { id: "u2", role: "member" }],
["eve", { id: "u3", role: "editor" }],
["root", { id: "u4", role: "admin" }],
];
const notes: Note[] = [
{ id: "n1", ownerId: "u1", archived: false },
{ id: "n2", ownerId: "u1", archived: true },
];
for (const note of notes) {
const cells = people.map(([name, user]) => `${name}=${authorizeUpdate(user, note)}`);
console.log(`${note.id}${note.archived ? " (archived)" : ""}: ${cells.join(" ")}`);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Authentication က 'ဘယ်သူလဲ' (fail ရင် 401)၊ authorization က 'ဒီ resource ကို ဒီ action လုပ်ခွင့်ရှိလား' (fail ရင် 403) ဖြစ်ပါတယ်။
- Session cookie ကို httpOnly, secure, sameSite နဲ့ ထားပြီး token ကို localStorage ထဲ မထားပါနဲ့။
- Button ဖျောက်တာ၊ layout နဲ့ middleware redirect တွေက UX အတွက်ပဲ ဖြစ်ပြီး တကယ့် စစ်ဆေးချက်က action နဲ့ data function ထဲမှာ ရှိရပါတယ်။
- Permission rule ကို pure function အဖြစ် ရေးပြီး ownerId ကို database ကနေပဲ ယူပါ။
Next.js — Authentication — Next.js
Next.js — Authentication — Next.js