Communitygithub.com

OAuth Providers (OAuth 提供者)

實作 Google、GitHub 與 Discord 的 OAuth 流程,包含 PKCE、Token 刷新與 Session 管理。

OAuth Providers (OAuth 提供者) 是什麼?

此技能提供完整的 OAuth 整合方案,支援 Google、GitHub 與 Discord 三大平台。它遵循最新安全最佳實務,例如對公開客戶端(SPA、行動應用)使用授權碼流程搭配 PKCE,並確保機密客戶端密鑰絕不暴露於前端。技能強調 CSRF 防護,透過生成隨機 `state` 參數並在回呼時驗證來達成。此外,它涵蓋 Token 刷新機制(伺服器端儲存 `refresh_token`)與 Session 管理(成功 OAuth 回呼後建立應用程式 Session,而非將 OAuth Token 存入 Cookie)。帳號連結功能允許使用者將多個提供者連接到同一個帳號。支援的技術平臺包括 Node.js 環境(如 Next.js 或其他 Web 框架),並提供了可直接套用的程式碼範例,例如 OAuth 回呼處理函式。

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

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

OAuth 提供者

規則

  • 使用授權碼流程搭配 PKCE 處理公開客戶端(SPA、行動應用)—— 絕不使用隱含流程
  • 網頁應用程式應在伺服器端進行程式碼交換 —— 客戶端密鑰絕不能離開伺服器
  • state 參數:產生隨機 state,儲存在 Session/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,Token 端點位於 https://discord.com/api/oauth2/token
  • Token 刷新:在伺服器端儲存 refresh_token,在 access_token 過期前重新整理
  • Session 管理:在成功 OAuth 回呼後建立應用程式 Session,不要將 OAuth Token 儲存在 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("Invalid state", { status: 400 });
  }

  // 交換程式碼以取得 Token
  const tokens = await exchangeCodeForTokens(code);
  const userInfo = await fetchUserInfo(tokens.access_token);

  // 建立或連結帳號、建立 Session
  const user = await upsertUser(userInfo);
  await createSession(user.id);

  return redirect("/dashboard");
}

避免

  • 在前端程式碼中儲存客戶端密鑰 —— 務必在伺服器端交換 Token
  • 略過 state 參數 —— CSRF 攻擊是真實存在的威脅
  • 將原始 OAuth Token 儲存在 Cookie 中 —— 請使用伺服器端 Session
  • 在連結提供者時,忘記處理「該電子郵件已有使用者」的情況

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.

相關技能