Communitygithub.com

OAuth Providers

Google/GitHub/Discord OAuth 인증 흐름, PKCE, 토큰 갱신 및 세션 관리

OAuth Providers란 무엇인가요?

이 스킬은 Google, GitHub, Discord OAuth 제공자를 통한 인증 흐름을 구현하는 방법을 설명합니다. SPA나 모바일 앱과 같은 공개 클라이언트용 Authorization Code + PKCE 흐름과 서버 측 코드 교환 방식을 지원하며, CSRF 방지를 위해 state 매개변수를 사용합니다. 토큰 갱신(refresh_token), 세션 관리(서버 측 세션), 계정 연동(여러 제공자를 하나의 계정에 연결) 기능을 포함합니다. Next.js와 같은 현대적인 웹 프레임워크에서 사용하기 적합하며, OAuth 콜백 핸들러 예제 코드를 제공합니다. 보안 모범 사례를 강조하여 클라이언트 시크릿을 프론트엔드에 저장하지 않고, OAuth 토큰을 쿠키에 직접 저장하지 않도록 합니다.

지원 대상~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/oauth-providers

즐겨 사용하는 AI에게 물어보기

이 에이전트 스킬이 미리 로드된 새 채팅을 엽니다.

문서

OAuth 제공자

규칙

  • 공개 클라이언트(SPA, 모바일)에서는 Authorization Code + PKCE 흐름 사용 — Implicit 흐름 사용 금지
  • 웹 앱의 경우 서버 측에서 코드 교환 — 클라이언트 시크릿이 서버를 벗어나지 않음
  • State 매개변수: 무작위 state를 생성하여 세션/쿠키에 저장하고, 콜백 시 확인 — 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 토큰을 쿠키에 저장하지 않음
  • 계정 연동: 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 토큰을 쿠키에 저장하지 않음 — 서버 측 세션 사용
  • 제공자 연결 시 "해당 이메일을 가진 사용자가 이미 존재함" 케이스를 처리하는 것 잊지 않기

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.

관련 스킬