一键导入
auth-middleware
Implement authentication and authorization middleware — JWT validation, role-based
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Implement authentication and authorization middleware — JWT validation, role-based
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Consult and write the ARAYA postoffice — the operational-directive channel. Advisory, never a gate: read it at cycle start, append your entry at cycle end, consider its directives, never be blocked by it. Governance acts never travel the postoffice.
Audit and enforce brand compliance across all projects and platforms — logo,
Design REST and GraphQL APIs following OpenAPI 3.1 standards. Produce complete
Connect frontend to backend — type-safe API clients, request/response handling,
Design scalable component architectures — design systems, component libraries,
Create reusable, accessible, and well-typed UI components following design system
| name | auth-middleware |
| description | Implement authentication and authorization middleware — JWT validation, role-based |
| governance | Constitution ENG-004: Engineering Excellence & Software Craftsmanship Standard |
Implement authentication and authorization middleware — JWT validation, role-based access control, API key verification, and session management — securing every endpoint from unauthorized access.
Protecting routes with copy-pasted auth code leads to inconsistencies and security gaps. This skill produces centralized middleware that authenticates every request, verifies permissions, and fails securely — applied once, enforced everywhere.
When implementing authentication for any API. When adding role-based access control. When securing existing unprotected endpoints. Always before exposing any endpoint to a network.
Authentication strategy (JWT, API key, session), user roles, endpoint access matrix.
// src/middleware/auth.ts
import { Request, Response, NextFunction } from "express";
import jwt from "jsonwebtoken";
const JWT_SECRET = process.env.JWT_SECRET!;
export interface AuthUser {
userId: string;
role: "user" | "admin";
}
// Extend Express Request to include authenticated user
declare global {
namespace Express {
interface Request {
user?: AuthUser;
}
}
}
// --- Authenticate: verify JWT token ---
export function authenticate(req: Request, res: Response, next: NextFunction): void {
const authHeader = req.headers.authorization;
if (!authHeader?.startsWith("Bearer ")) {
res.status(401).json({
error: "Unauthorized",
message: "Missing or invalid authorization header",
});
return;
}
const token = authHeader.slice(7);
try {
const payload = jwt.verify(token, JWT_SECRET) as AuthUser;
req.user = payload;
next();
} catch (error) {
if (error instanceof jwt.TokenExpiredError) {
res.status(401).json({
error: "Unauthorized",
message: "Token has expired",
});
return;
}
res.status(401).json({
error: "Unauthorized",
message: "Invalid token",
});
return;
}
}
// --- Authorize: require specific role(s) ---
export function authorize(...roles: string[]) {
return (req: Request, res: Response, next: NextFunction): void => {
if (!req.user) {
res.status(401).json({
error: "Unauthorized",
message: "Authentication required",
});
return;
}
if (!roles.includes(req.user.role)) {
res.status(403).json({
error: "Forbidden",
message: `Role '${req.user.role}' is not authorized for this resource`,
});
return;
}
next();
};
}
// Usage in routes:
// router.get("/admin/users", authenticate, authorize("admin"), adminHandler);
// router.get("/profile", authenticate, profileHandler);
authenticate middleware:
authorize middleware: