Shell
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_tokencookie - 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 OAuthlogout()- 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
sessionTokenexists
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
- Dependencies - Install @upstash/redis, nanoid
- Server client -
lib/supabase/server.ts - Auth callback -
/auth/callback/route.ts(core OAuth flow) - API routes -
/api/auth/sessionand/api/auth/logout - Auth store - Zustand store for auth state
- Auth hook -
useAuth.tsfor login/logout actions - UI components - LoginButton, SpectatorBadge
- WebSocket integration - Auto-authenticate on connect
- Layout update - Add auth UI to CanvasLayout
- 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
- Go to https://developer.twitter.com/en/portal/dashboard
- Create a new Project/App (or use existing)
- Under "User authentication settings", click "Set up"
- 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
- Save and copy Client ID and Client Secret
Part B: Supabase Dashboard
- Go to Supabase Dashboard → Authentication → Providers
- Find "Twitter" and click to expand
- Toggle Enable Sign in with Twitter
- Paste your X Client ID and Client Secret
- 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.