Skip to content
Shell
4.9 KB
Raw Blame

X OAuth Login Implementation Plan

Overview

Implement X (Twitter) OAuth login flow for X-Place using Supabase Auth. Users authenticate with X, get a session token stored in Redis, and the WebSocket server validates it.

Current State

  • ✅ Supabase client exists (apps/web/src/lib/supabase/client.ts)
  • ✅ WebSocket server validates sessions via Redis (xplace:session:{token})
  • ✅ UserSession type defined (packages/shared/src/types/user.ts)
  • ✅ useWebSocket has authenticate(token) function ready
  • ❌ No OAuth flow implemented
  • ❌ No auth UI components
  • ❌ No session creation

Auth Flow

User clicks "Login with X"
       ↓
Supabase OAuth → X consent screen
       ↓
Redirect to /auth/callback?code=xxx
       ↓
Exchange code for Supabase session
       ↓
Extract X metadata (username, avatar, account age)
       ↓
Generate session token, store in Redis
       ↓
Set cookie, redirect home
       ↓
useAuth reads cookie → authStore updates
       ↓
useWebSocket auto-authenticates
       ↓
WebSocket server validates → User authenticated!

Files to Create

1. Server-side Supabase Client

apps/web/src/lib/supabase/server.ts

  • Cookie-based server client for Next.js App Router
  • Used by API routes and callbacks

2. OAuth Callback Route

apps/web/src/app/auth/callback/route.ts

  • Exchange OAuth code for session
  • Extract X user metadata (username, avatar, account age)
  • Calculate isSpectatorOnly (account < 30 days = spectator)
  • Generate session token with nanoid
  • Store UserSession in Redis with 24h TTL
  • Set xplace_session_token cookie
  • Redirect to home

3. Session API Route

apps/web/src/app/api/auth/session/route.ts

  • GET: Return user session from Redis (for client hydration)

4. Logout API Route

apps/web/src/app/api/auth/logout/route.ts

  • POST: Delete Redis session, clear cookie

5. Auth Store (Zustand)

apps/web/src/stores/auth-store.ts

  • State: user, sessionToken, isAuthenticated, isLoading
  • Actions: setUser, setSessionToken, logout
  • Computed: canPlacePixels() (authenticated + not spectator)
  • Persist sessionToken to localStorage

6. Auth Hook

apps/web/src/hooks/useAuth.ts

  • Initialize auth on mount (read cookie, fetch session)
  • loginWithX() - trigger Supabase OAuth
  • logout() - clear session everywhere

7. Login Button Component

apps/web/src/components/auth/LoginButton.tsx

  • Show "Login with X" or user profile + logout
  • X logo, compact mode for mobile

8. Spectator Badge Component

apps/web/src/components/auth/SpectatorBadge.tsx

  • Show warning for accounts < 30 days old

Files to Modify

1. useWebSocket Hook

apps/web/src/hooks/useWebSocket.ts

  • Import useAuthStore
  • On connection open, auto-authenticate if sessionToken exists

2. CanvasLayout

apps/web/src/components/layout/CanvasLayout.tsx

  • Add <LoginButton /> to top bar
  • Add <SpectatorBadge /> to sidebar

Dependencies to Install

pnpm add @upstash/redis nanoid --filter @x-place/web

Note: @supabase/ssr already installed


Implementation Order

  1. Dependencies - Install @upstash/redis, nanoid
  2. Server client - lib/supabase/server.ts
  3. Auth callback - /auth/callback/route.ts (core OAuth flow)
  4. API routes - /api/auth/session and /api/auth/logout
  5. Auth store - Zustand store for auth state
  6. Auth hook - useAuth.ts for login/logout actions
  7. UI components - LoginButton, SpectatorBadge
  8. WebSocket integration - Auto-authenticate on connect
  9. Layout update - Add auth UI to CanvasLayout
  10. Test - Full login flow with Playwright

Environment Variables Required

# Already configured:
NEXT_PUBLIC_SUPABASE_URL=xxx
NEXT_PUBLIC_SUPABASE_ANON_KEY=xxx

# Need to add (for Redis from Next.js):
UPSTASH_REDIS_REST_URL=xxx
UPSTASH_REDIS_REST_TOKEN=xxx

Step 0: X OAuth Setup (REQUIRED FIRST)

Part A: X Developer Portal

  1. Go to https://developer.twitter.com/en/portal/dashboard
  2. Create a new Project/App (or use existing)
  3. Under "User authentication settings", click "Set up"
  4. Select:
    • App permissions: Read (minimum)
    • Type of App: Web App
    • Callback URL: https://<your-supabase-project>.supabase.co/auth/v1/callback
    • Website URL: Your production URL or https://x-place-xxx.vercel.app
  5. Save and copy Client ID and Client Secret

Part B: Supabase Dashboard

  1. Go to Supabase Dashboard → Authentication → Providers
  2. Find "Twitter" and click to expand
  3. Toggle Enable Sign in with Twitter
  4. Paste your X Client ID and Client Secret
  5. Save

Part C: Environment Variables

Add to Vercel (for production) and .env.local (for local dev):

UPSTASH_REDIS_REST_URL=<from Upstash dashboard>
UPSTASH_REDIS_REST_TOKEN=<from Upstash dashboard>

Note: Once these are configured, we can implement the code.