AeThex Discord System - Complete Flow Documentation
Overview
FLOW 1: Discord OAuth Login
Step-by-Step
┌─ User on /login page
│
├─ Clicks "Continue with Discord" button
│ └─ code/client/pages/Login.tsx line ~180-185
│
├─ Browser → /api/discord/oauth/start (GET)
│ └─ code/api/discord/oauth/start.ts
│ • Builds Discord OAuth URL
│ • No state needed for login flow
│ • Redirects to: https://discord.com/api/oauth2/authorize?client_id=...
│
├─ User authorizes on Discord
│ └─ Discord redirects to: /api/discord/oauth/callback?code=XXX&state=...
│
├─ Backend processes callback (GET)
│ └─ code/api/discord/oauth/callback.ts
│
│ A) Parse state (action will be undefined or "login")
│ B) Exchange code for Discord access token
│ C) Fetch Discord user profile (id, username, email, avatar)
│ D) Check if email already in Supabase auth
│ └─ If YES: Just link to existing user (don't create)
│ └─ If NO: Create new auth user with email
│ E) Create/update user_profiles record (upsert)
│ F) Create discord_links record (links discord_id → user_id)
│ G) Create session (login user automatically)
�� H) Redirect to /dashboard or /onboarding based on profile_complete
│
└─ ✅ User logged in with Discord account linkedDatabase Operations (Login Flow)
Success Path
Failure Paths
FLOW 2: Discord Account Linking (from Dashboard)
Step-by-Step
Database Operations (Linking Flow)
Success Path
Failure Paths
Critical: Session Persistence
FLOW 3: Discord Verification Code (Bot /verify Command)
Step-by-Step
Database Operations (Verification Flow)
Success Path
Failure Paths
Why This Flow Exists
FLOW 4: Discord Activity (Standalone SPA in Discord)
Step-by-Step
Database Operations (Activity Flow)
Success Path
Failure Paths
Key Difference from Other Flows
FLOW 5: Discord Bot Commands
Available Commands
/verify
/set-realm [arm]
/profile
/unlink
/verify-role
Database Schema
discord_links
discord_linking_sessions
discord_verifications
discord_role_mappings
discord_user_roles
Environment Variables
Quick Comparison
Flow
Entry
Goal
Auth Type
Endpoints
Status
Common Issues & Solutions
Session Lost During OAuth Linking
Forced Onboarding After Email Login
Discord Already Linked Error
Verification Code Expired
Testing Checklist
Architecture Summary
PreviousDiscord Bot Token Invalid - Troubleshooting & FixNextDiscord Linking Fixes - Summary of Changes
Last updated
