Communitygithub.com

YepAPI/skills

Google/GitHub/Discord OAuth 认证流程,支持 PKCE、令牌刷新和会话管理。

skills 是什么?

此技能提供了一套完整的 OAuth 2.0 集成方案,涵盖 Google、GitHub 和 Discord 三大主流身份提供商。它强制执行最佳安全实践,包括对公共客户端(如 SPA 和移动应用)使用授权码流程(Authorization Code)配合 PKCE(代码交换密钥证明),而不是隐式流程(Implicit flow)。对于 Web 应用,客户端密钥永远不会离开服务器,确保安全。关键特性包括:使用 `state` 参数防止跨站请求伪造(CSRF)、令牌刷新机制、服务端会话管理,以及支持用户通过 `accounts` 表关联多个提供商。它提供了清晰的模式和待避免的陷阱,帮助开发者安全高效地集成 OAuth 认证。该技能兼容基于标准 Web API 构建的 AI 代理(如 Claude Code、Coder、Codex 等),并提供详细的 TypeScript 代码示例。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/oauth-providers

在你喜欢的 AI 中提问

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

文档

OAuth 提供商

规则

  • 对公共客户端(SPA、移动应用)使用授权码流程(Authorization Code)配合 PKCE —— 绝不要使用隐式流程(Implicit flow)
  • Web 应用的服务端代码交换 —— 客户端密钥永远不离开服务器
  • state 参数:生成随机 state,存储在会话/Cookie 中,在回调时验证 —— 防止 CSRF
  • Google OAuth:作用域 openid email profile,使用 id_token 获取用户信息
  • GitHub OAuth:作用域 read:user user:email,在 https://github.com/login/oauth/access_token 交换代码
  • Discord OAuth:作用域 identify email,令牌端点为 https://discord.com/api/oauth2/token
  • 令牌刷新:服务端存储 refresh_token,在 access_token 过期前刷新
  • 会话管理:在 OAuth 回调成功时创建应用会话,不要将 OAuth 令牌存储在 Cookie 中
  • 关联账户:允许用户通过 accounts 表将多个提供商关联到一个账户

模式

// OAuth 回调处理器
export async function GET(req: Request) {
  const { searchParams } = new URL(req.url);
  const code = searchParams.get("code");
  const state = searchParams.get("state");

  // 验证 state 是否与存储值匹配
  if (state !== cookies().get("oauth_state")?.value) {
    return new Response("无效的 state", { status: 400 });
  }

  // 用代码交换令牌
  const tokens = await exchangeCodeForTokens(code);
  const userInfo = await fetchUserInfo(tokens.access_token);

  // 创建或关联账户,创建会话
  const user = await upsertUser(userInfo);
  await createSession(user.id);

  return redirect("/dashboard");
}

避免

  • 将客户端密钥存储在前端代码中 —— 始终在服务端交换令牌
  • 跳过 state 参数 —— CSRF 攻击是真实存在的
  • 将原始 OAuth 令牌存储在 Cookie 中 —— 使用服务端会话
  • 在关联提供商时忘记处理“该邮箱的用户已存在”的情况

Individual skills in this repo

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

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相关技能