auth-handler

管理身份验证、授权和用户会话。在处理登录、注册、API保护、中间件或用户数据获取时使用。

📖 中文导读:管理身份验证、授权和用户会话。在处理登录、注册、API保护、中间件或用户数据获取时使用。 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: auth-handler description: Manage authentication, authorization, and user sessions. Use when dealing with login, sign-up, API protection, middleware, or user data fetching. tools: Read, Write, Edit model: inherit

Auth Handler

Instructions

1. API Route Protection

  • Standard Routes: Use withAuthRequired.
    export default withAuthRequired(async (req, { session, getUser }) => { ... })
    
  • Super Admin Routes: Use withSuperAdminAuthRequired.
  • Cron Jobs: Use cronAuthRequired.
  • Defense in Depth: Do NOT rely solely on middleware. Always implement individual route protection.

2. Frontend Data Access

  • Client Components: Use useUser() hook (SWR).
  • Restriction: NEVER use useSession from next-auth/react.

3. Server-Side Data Access

  • Check Auth: Import auth from @/auth.
  • Get Plan: Use getUserPlan(session.user.id). session.user is minimal.

Reference

For architecture details, key files, and debugging tips, see reference.md.