- 最終更新
- 2026/10/21
3秒でわかる
ログイン済みの相手に、その操作を許すかどうかを判定する仕組み。誰であるかを確かめる認証の次に来る、何をしてよいかを決める工程です。
30秒図解
もう少し詳しく
どういうものか
認可は、
判定の
なぜ必要か
認証だけ通してしまうと、
URL の
具体例
// 認証 — トークンから「誰か」を確定する
function authenticate(req, res, next) {
const token = req.headers.authorization?.replace("Bearer ", "");
const payload = verifyJwt(token);
if (!payload) return res.status(401).json({ error: "unauthenticated" });
req.user = payload;
next();
}
// 認可 — その人がこの注文を触ってよいか判定する
async function canEditOrder(req, res, next) {
const order = await db.order.findUnique({ where: { id: req.params.id } });
if (!order) return res.status(404).json({ error: "not found" });
const isOwner = order.userId === req.user.id;
const isAdmin = req.user.role === "admin";
if (!isOwner && !isAdmin) {
return res.status(403).json({ error: "forbidden" });
}
req.order = order;
next();
}
app.patch("/orders/:id", authenticate, canEditOrder, updateOrder);401 は
つまずきやすいところ
似た用語との違い
| 用語 | 問い |
|---|---|
| 認証 (authentication) | あなたは誰か |
| 認可 (authorization) | あなたは |
| 監査ログ | 実際に |
覚え方
空港でたとえると、


