Auth middleware

From berkcangumusisik/claude-code-practices · View interactive version

Auth middleware is an AI agent skill from the GitHub repository berkcangumusisik/claude-code-practices. Generates authentication and authorization middleware for Express/Fastify/Hono

How to use this skill

Skill file

Generate Auth Middleware

Strategy: $1 (jwt | session | api-key | oauth)

  1. Detect framework and existing auth patterns:
cat package.json | grep -E '"express"|"fastify"|"hono"|"jsonwebtoken"|"jose"|"passport"'
grep -rn "middleware\|authenticate\|authorize" src/ --include="*.ts" -l | head -5
  1. Read existing auth code if present.
  1. Generate the middleware for the requested strategy:

JWT example:

export async function authenticate(req: Request, res: Response, next: NextFunction) {
  const token = req.headers.authorization?.split(' ')[1]
  if (!token) return res.status(401).json({ error: 'Unauthorized' })
  
  try {
    const payload = await verifyJWT(token)
    req.user = payload
    next()
  } catch {
    return res.status(401).json({ error: 'Invalid token' })
  }
}

export function authorize(...roles: Role[]) {
  return (req: Request, res: Response, next: NextFunction) => {
    if (!roles.includes(req.user?.role)) return res.status(403).json({ error: 'Forbidden' })
    next()
  }
}
  1. Include:
  1. Generate tests for all auth scenarios.
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.