|
| 1 | +# 🔐 Authentication Setup Guide |
| 2 | + |
| 3 | +## ✅ What I Fixed: |
| 4 | + |
| 5 | +1. **Added proper Supabase auth configuration** |
| 6 | + - Auto token refresh |
| 7 | + - Session persistence |
| 8 | + - URL session detection |
| 9 | + |
| 10 | +2. **Better error handling** |
| 11 | + - Clear error messages for wrong credentials |
| 12 | + - Email already registered detection |
| 13 | + - Automatic redirect on successful login |
| 14 | + |
| 15 | +3. **Email confirmation flow** |
| 16 | + - Detects if email confirmation is required |
| 17 | + - Shows appropriate messages |
| 18 | + - Auto-login if confirmation is disabled |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 🚀 Quick Setup Steps |
| 23 | + |
| 24 | +### **Step 1: Disable Email Confirmation (Recommended for Testing)** |
| 25 | + |
| 26 | +1. Go to: https://supabase.com/dashboard/project/banyhroaktppyqtspznt |
| 27 | +2. Click **Authentication** → **Providers** → **Email** |
| 28 | +3. Find **"Confirm email"** toggle |
| 29 | +4. **Turn it OFF** (disabled) |
| 30 | +5. Click **Save** |
| 31 | + |
| 32 | +✅ This allows users to sign up and log in immediately without email verification. |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +### **Step 2: Configure Google OAuth Redirect URIs** |
| 37 | + |
| 38 | +1. Go to: https://console.cloud.google.com/apis/credentials |
| 39 | +2. Click on your **OAuth 2.0 Client ID** |
| 40 | +3. Under **Authorized redirect URIs**, add: |
| 41 | + ``` |
| 42 | + https://banyhroaktppyqtspznt.supabase.co/auth/v1/callback |
| 43 | + ``` |
| 44 | +4. Click **Save** |
| 45 | + |
| 46 | +✅ This allows Google OAuth to redirect back to your app. |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +### **Step 3: Add Environment Variables to Vercel** |
| 51 | + |
| 52 | +1. Go to: https://vercel.com/dashboard |
| 53 | +2. Select your **bb84-simulation** project |
| 54 | +3. Go to **Settings** → **Environment Variables** |
| 55 | +4. Add these two variables: |
| 56 | + ``` |
| 57 | + VITE_SUPABASE_URL=https://banyhroaktppyqtspznt.supabase.co |
| 58 | + VITE_SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImJhbnlocm9ha3RwcHlxdHNwem50Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjI5MjE2NjEsImV4cCI6MjA3ODQ5NzY2MX0.L_I7N-gUo6BSLvBEzJX-VGEYtsMmhTQImBrqdy7OYl8 |
| 59 | + ``` |
| 60 | +5. Click **Save** |
| 61 | +6. **Redeploy** your site |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +### **Step 4: Update Leaderboard Table (Run SQL in Supabase)** |
| 66 | + |
| 67 | +1. Go to: https://supabase.com/dashboard/project/banyhroaktppyqtspznt/editor |
| 68 | +2. Click **SQL Editor** → **New Query** |
| 69 | +3. Paste this SQL: |
| 70 | + |
| 71 | +```sql |
| 72 | +-- Add user_id column if it doesn't exist |
| 73 | +ALTER TABLE leaderboard |
| 74 | +ADD COLUMN IF NOT EXISTS user_id UUID REFERENCES auth.users(id); |
| 75 | + |
| 76 | +-- Drop existing policies if they exist |
| 77 | +DROP POLICY IF EXISTS "Authenticated users can submit score" ON leaderboard; |
| 78 | +DROP POLICY IF EXISTS "Anyone can view leaderboard" ON leaderboard; |
| 79 | + |
| 80 | +-- Enable Row Level Security |
| 81 | +ALTER TABLE leaderboard ENABLE ROW LEVEL SECURITY; |
| 82 | + |
| 83 | +-- Allow authenticated users to insert scores |
| 84 | +CREATE POLICY "Authenticated users can submit score" |
| 85 | +ON leaderboard FOR INSERT |
| 86 | +TO authenticated |
| 87 | +WITH CHECK (auth.uid() = user_id); |
| 88 | + |
| 89 | +-- Allow everyone to view the leaderboard |
| 90 | +CREATE POLICY "Anyone can view leaderboard" |
| 91 | +ON leaderboard FOR SELECT |
| 92 | +TO public |
| 93 | +USING (true); |
| 94 | +``` |
| 95 | + |
| 96 | +4. Click **Run** (or Ctrl+Enter) |
| 97 | + |
| 98 | +✅ This updates your database to work with authenticated users. |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## 🎯 How Authentication Works Now: |
| 103 | + |
| 104 | +### **Sign Up (Email/Password)** |
| 105 | +1. User enters: email, password, username |
| 106 | +2. If **email confirmation disabled**: User is logged in immediately → redirected to quiz |
| 107 | +3. If **email confirmation enabled**: User gets email → clicks link → can login |
| 108 | + |
| 109 | +### **Sign In (Email/Password)** |
| 110 | +1. User enters: email, password |
| 111 | +2. If credentials correct: Logged in → redirected to quiz |
| 112 | +3. If wrong: Shows clear error message |
| 113 | + |
| 114 | +### **Sign In (Google)** |
| 115 | +1. User clicks "Continue with Google" |
| 116 | +2. Selects Google account |
| 117 | +3. Redirected back to app → logged in → redirected to quiz |
| 118 | + |
| 119 | +### **Quiz Access** |
| 120 | +- **Without login**: Redirected to `/auth` page |
| 121 | +- **With login**: Can take quiz, scores saved with user_id |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## ❌ No Need for Additional Tables! |
| 126 | + |
| 127 | +You **DO NOT** need to create a new table for authentication. Supabase already provides: |
| 128 | + |
| 129 | +- `auth.users` - Stores all user data (email, password, OAuth) |
| 130 | +- User metadata (username stored in `user_metadata`) |
| 131 | +- Your `leaderboard` table uses `user_id` to reference these users |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## 🧪 Testing Steps: |
| 136 | + |
| 137 | +1. **Start local dev server:** |
| 138 | + ```powershell |
| 139 | + npm run dev |
| 140 | + ``` |
| 141 | + |
| 142 | +2. **Test Email Signup:** |
| 143 | + - Go to http://localhost:8080/#/auth |
| 144 | + - Click "Sign Up" |
| 145 | + - Enter email, password, username |
| 146 | + - Should login immediately (if confirmation disabled) |
| 147 | + |
| 148 | +3. **Test Email Login:** |
| 149 | + - Use same email/password |
| 150 | + - Should redirect to quiz |
| 151 | + |
| 152 | +4. **Test Google OAuth:** |
| 153 | + - Click "Continue with Google" |
| 154 | + - Select account |
| 155 | + - Should redirect back and login |
| 156 | + |
| 157 | +5. **Test Quiz Access:** |
| 158 | + - Try to visit `/quiz` without login |
| 159 | + - Should redirect to `/auth` |
| 160 | + - Login, then can access quiz |
| 161 | + |
| 162 | +--- |
| 163 | + |
| 164 | +## 🐛 Common Issues & Solutions: |
| 165 | + |
| 166 | +### **"Invalid login credentials"** |
| 167 | +→ Wrong email/password. User needs to sign up first. |
| 168 | + |
| 169 | +### **"Email confirmation required"** |
| 170 | +→ Disable email confirmation in Supabase settings (Step 1 above). |
| 171 | + |
| 172 | +### **Google OAuth stuck on account selection** |
| 173 | +→ Add redirect URI to Google Cloud Console (Step 2 above). |
| 174 | + |
| 175 | +### **"Missing environment variables"** |
| 176 | +→ Check `.env.local` file exists in project root. |
| 177 | + |
| 178 | +### **Email confirmation not sent** |
| 179 | +→ Either disable confirmation OR configure SMTP in Supabase → Project Settings → Auth → SMTP Settings. |
| 180 | + |
| 181 | +--- |
| 182 | + |
| 183 | +## 📊 What Data is Stored: |
| 184 | + |
| 185 | +### **In `auth.users` table (automatic):** |
| 186 | +- User ID (UUID) |
| 187 | +- Email |
| 188 | +- Encrypted password |
| 189 | +- OAuth provider (if Google login) |
| 190 | +- Username (in `raw_user_meta_data`) |
| 191 | +- Created timestamp |
| 192 | + |
| 193 | +### **In `leaderboard` table (your table):** |
| 194 | +- Score |
| 195 | +- Total questions |
| 196 | +- Time taken |
| 197 | +- User ID (links to auth.users) |
| 198 | +- Username (for display) |
| 199 | + |
| 200 | +--- |
| 201 | + |
| 202 | +## 🎉 You're All Set! |
| 203 | + |
| 204 | +After completing Steps 1-4, your authentication should work perfectly on both: |
| 205 | +- **Localhost**: http://localhost:8080 |
| 206 | +- **Production**: https://bb84-simulation.vercel.app |
| 207 | + |
| 208 | +Questions? Check the Supabase docs: https://supabase.com/docs/guides/auth |
0 commit comments