بنقرة واحدة
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: