Communitygithub.com

ComeOnOliver/skillshub

一个用于在Next.js中构建处理JWT令牌的API客户端的概念性技能。

skillshub 是什么?

该技能描述了一个概念性框架,用于在Next.js应用中实现一个健壮的、集中化的API客户端,专门用于处理基于JWT(JSON Web Token)的身份验证。它涵盖了从自动附加授权头、处理令牌过期和自动刷新,到标准化错误响应解析和集中化所有API调用的关键能力。该技能旨在帮助开发者避免在各处重复编写API调用逻辑,从而创建更安全、更一致且更易于维护的前端-后端通信层。它支持Next.js的客户端和服务端集成模式,并提供了与React组件(如hooks)的整合指导。此外,还详细讨论了安全考量(如安全的令牌存储和HTTPS)、性能优化(如请求缓存和去重)以及测试策略,以确保API客户端在不同场景下的可靠性和安全性。

兼容平台✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ComeOnOliver/skillshub/tree/HEAD/skills/aiskillstore/marketplace/abdul-haseeb360/frontend-api-client-with-jwt

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

前端API客户端与JWT技能

何时使用此技能

当您需要在Next.js中实现一个能妥善处理JWT令牌身份验证的健壮API客户端时,使用此概念性技能。此技能适用于:

  • 在Next.js应用中创建集中化的API通信层
  • 跨多个API端点管理基于JWT的身份验证
  • 处理令牌过期和刷新场景
  • 标准化错误响应解析和处理
  • 实现安全的API通信模式

此技能不应被用于:

  • 没有基于JWT身份验证的应用
  • 没有API通信需求的静态站点
  • 使用替代身份验证方法(API密钥、OAuth 2.0客户端凭证等)的应用
  • API交互较少的简单应用

前提条件

  • Next.js应用(App Router 或 Pages Router)
  • 理解JWT(JSON Web Token)概念
  • 了解HTTP头部和授权机制
  • 基本了解异步JavaScript操作
  • 知晓客户端与服务端执行上下文的区别

概念性实现框架

授权头部附加能力

  • 自动将JWT令牌作为Authorization头部附加到API请求
  • 根据请求类型和端点决定何时包含令牌
  • 处理客户端和服务端请求的令牌附加
  • 在跨域请求中适当管理令牌包含
  • 确保正确的头部格式("Bearer ")

令牌过期处理能力

  • 在发出API请求前检测JWT令牌过期
  • 实现自动令牌刷新机制
  • 优雅地处理令牌刷新失败
  • 在令牌刷新期间维护会话连续性
  • 协调多个并发请求的令牌刷新
  • 刷新后安全地存储更新后的令牌

错误响应解析能力

  • 解析来自API端点的结构化错误响应
  • 识别身份验证相关错误(401, 403)以进行特殊处理
  • 提取有意义的错误信息以供用户反馈
  • 一致地处理不同的错误响应格式
  • 区分客户端错误、服务端错误和网络问题
  • 根据错误类型提供适当的用户反馈

API调用集中化能力

  • 为所有API通信创建统一接口
  • 标准化整个应用的请求和响应处理
  • 实现一致的错误处理和日志记录
  • 管理请求/响应拦截器以处理横切关注点
  • 提供类型安全的API调用模式(使用TypeScript时)
  • 在适当情况下启用请求缓存和去重

预期输入/输出

输入要求:

  1. JWT令牌管理:

    • 用于授权的有效JWT令牌
    • 令牌刷新端点配置
    • 令牌存储机制(localStorage, cookies等)
    • 令牌过期时间和刷新时机
  2. API配置:

    • 请求的基础API URL
    • 请求超时设置
    • 自定义头部和请求选项
    • 端点特定配置
  3. 请求参数:

    • HTTP方法(GET, POST, PUT, DELETE等)
    • 请求URL或端点标识符
    • POST/PUT操作的请求体
    • 查询参数和路径变量

输出格式:

  1. 成功的API响应:

    • HTTP 200-299 状态码
    • 解析后的响应数据,符合预期格式
    • 适用时的更新令牌信息
    • 所有端点一致的响应结构
  2. 身份验证错误响应:

    • HTTP 401 未授权,用于过期/无效令牌
    • 自动尝试令牌刷新
    • 刷新失败后重定向到登录页面
    • 清晰的身份验证问题错误消息
  3. 授权错误响应:

    • HTTP 403 禁止访问,用于权限不足
    • 根据权限级别的适当错误处理
    • 访问限制的用户反馈
  4. 一般错误响应:

    • 包含消息和代码的结构化错误对象
    • 适当的HTTP状态码
    • 用于调试的详细错误信息
    • 用于UI展示的用户友好错误消息

集成模式

客户端集成

  • 从客户端组件和客户端渲染处理API调用
  • 在浏览器上下文中管理令牌存储和检索
  • 实现用于头部附加的请求拦截器
  • 与身份验证状态管理协调

服务端集成(适用时)

  • 从服务端组件处理API调用
  • 在服务端和客户端之间安全地传输令牌
  • 实现服务端令牌验证
  • 适当地处理服务端错误响应

React组件集成

  • 提供用于在函数式组件中进行API通信的hooks
  • 启用基于上下文的API客户端访问
  • 支持函数式组件和类组件模式
  • 实现适当的清理和取消机制

安全考量

  1. 令牌存储:安全的JWT令牌存储以防止XSS攻击
  2. 头部传输:对所有API通信使用HTTPS
  3. 令牌刷新:实现安全的令牌刷新机制
  4. 错误信息:避免在错误消息中暴露敏感信息
  5. 请求验证:在发送前验证请求参数
  6. 响应验证:验证响应完整性和格式
  7. 跨站请求:实现适当的CORS处理

性能影响

  • 优化令牌检索和附加以最小化开销
  • 实现高效的令牌刷新以避免阻塞请求
  • 考虑请求缓存策略以提升性能
  • 通过恰当的状态管理减少冗余API调用
  • 在适当情况下实现请求批处理
  • 监控和优化网络请求时序

错误处理与验证

  • 在请求前验证JWT令牌格式和过期时间
  • 优雅地处理网络连接问题
  • 为瞬时故障实现重试机制
  • 为关键API失败提供回退行为
  • 在不暴露敏感信息的前提下,为调试适当地记录错误
  • 为实现服务韧性,实现断路器模式

测试考量

  • 使用有效/无效令牌测试令牌附加功能
  • 验证令牌刷新机制是否正常工作
  • 跨不同错误类型验证错误响应解析
  • 在客户端和服务端上下文中测试API客户端行为
  • 验证请求的适当清理和取消
  • 测试并发请求处理和令牌刷新协调

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

ComeOnOliver/skillshub

Create stunning, animation-rich HTML presentations from scratch or by converting PowerPoint files. Use when the user wants to build a presentation, convert a PPT/PPTX to web, or create slides for a talk/pitch. Helps non-designers discover their aesthetic through visual exploration rather than abstract choices.

ComeOnOliver/skillshub

Next.js 16+ 和 Turbopack — 增量打包、文件系统缓存、开发速度,以及何时使用 Turbopack 与 webpack。

ComeOnOliver/skillshub

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

ComeOnOliver/skillshub

See, Understand, Act on video and audio. See- ingest from local files, URLs, RTSP/live feeds, or live record desktop; return realtime context and playable stream links. Understand- extract frames, build visual/semantic/temporal indexes, and search moments with timestamps and auto-clips. Act- transcode and normalize (codec, fps, resolution, aspect ratio), perform timeline edits (subtitles, text/image overlays, branding, audio overlays, dubbing, translation), generate media assets (image, audio, video), and create real time alerts for events from live streams or desktop capture.

ComeOnOliver/skillshub

AI-assisted video editing workflows for cutting, structuring, and augmenting real footage. Covers the full pipeline from raw capture through FFmpeg, Remotion, ElevenLabs, fal.ai, and final polish in Descript or CapCut. Use when the user wants to edit video, cut footage, create vlogs, or build video content.

ComeOnOliver/skillshub

Next.js 15 애플리케이션을 위한 프론트엔드 개발 가이드라인. React 19, TypeScript, Shadcn/ui, Tailwind CSS를 사용한 모던 패턴. Server Components, Client Components, App Router, 파일 구조, Shadcn/ui 컴포넌트, 성능 최적화, TypeScript 모범 사례 포함. 컴포넌트, 페이지, 기능 생성, 데이터 페칭, 스타일링, 라우팅, 프론트엔드 코드 작업 시 사용.

ComeOnOliver/skillshub

Provides Tamagui patterns for config v4, compiler optimization, styled context, and cross-platform styling. Must use when working with Tamagui projects (tamagui.config.ts, @tamagui imports).

ComeOnOliver/skillshub

Create distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.

ComeOnOliver/skillshub

Create high-converting, visually distinctive landing pages. Use when building marketing pages, product launches, SaaS homepages, or any single-page conversion-focused website. Guides section-by-section composition with anti-AI-slop principles.

ComeOnOliver/skillshub

Auto-loaded context for Portfolio Buddy 2 development. Use for ANY task involving: React 19 development, TypeScript, portfolio analysis features, metrics calculations, trading strategy comparison, or working with the Portfolio Buddy 2 codebase. Contains tech stack, known issues, and architectural constraints.

ComeOnOliver/skillshub

Next.js development tooling via MCP. Inspect routes, components, build info, and debug Next.js apps. Use when working on Next.js applications, debugging routing, or inspecting app structure. NOT for general React or non-Next.js projects.

ComeOnOliver/skillshub

Implement UI using Shadcn MCP (atoms/theme) and 21st.dev MCP (complex sections). Use when adding buttons, layouts, or generating landing pages.

ComeOnOliver/skillshub

Best practices and patterns for Next.js App Router, Server Actions, and Routing in this project.

ComeOnOliver/skillshub

The Design System, Theme, and UX rules for the Physical AI Hub.

ComeOnOliver/skillshub

Comprehensive frontend development skill for building modern, performant web applications using ReactJS, NextJS, TypeScript, Tailwind CSS. Includes component scaffolding, performance optimization, bundle analysis, and UI best practices. Use when developing frontend features, optimizing performance, implementing UI/UX designs, managing state, or reviewing frontend code.

ComeOnOliver/skillshub

This document captures learnings from fixing keyboard navigation issues when floating components (Select, DropdownMenu, Popover) have CSS open/close animations.

ComeOnOliver/skillshub

Build Next.js 16 applications with correct patterns and distinctive design. Use when creating pages, layouts, dynamic routes, upgrading from Next.js 15, or implementing proxy.ts. Covers breaking changes (async params/searchParams, Turbopack, cacheComponents) and frontend aesthetics. NOT when building non-React or backend-only applications.

ComeOnOliver/skillshub

Comprehensive Framer Motion animation library for React. Covers motion components, variants, gestures, page transitions, and scroll animations. Use when adding animations to React/Next.js applications.

ComeOnOliver/skillshub

Next.js 16+ uses App Router with Server Components by default. Client Components are only used when interactivity is needed (hooks, event handlers, browser APIs).

ComeOnOliver/skillshub

Build interfaces that resonate, not just render. This skill enforces bold, intentional

相关技能