AUTHENTICATION
Authentication Setup Guide
Section titled “Authentication Setup Guide”This guide explains how to configure authentication for Supen, including OAuth providers and Cloudflare Turnstile bot protection.
Features
Section titled “Features”- ✅ Email/Password authentication
- ✅ OAuth providers (Google, GitHub, Microsoft)
- ✅ Cloudflare Turnstile bot protection
- ✅ Modern, clean UI matching Manus design
- ✅ Secure JWT-based authentication with Supabase
Quick Start
Section titled “Quick Start”1. Email/Password Authentication
Section titled “1. Email/Password Authentication”Email/password authentication works out of the box with Supabase. No additional configuration needed.
2. OAuth Providers Setup
Section titled “2. OAuth Providers Setup”Enable OAuth in Supabase Dashboard
Section titled “Enable OAuth in Supabase Dashboard”- Go to your Supabase project dashboard
- Navigate to Authentication → Providers
- Enable the providers you want to use:
Google OAuth
Section titled “Google OAuth”- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google+ API
- Go to Credentials → Create Credentials → OAuth 2.0 Client ID
- Add authorized redirect URI:
https://your-project-ref.supabase.co/auth/v1/callback
- Copy Client ID and Client Secret
- In Supabase dashboard, paste them into Google provider settings
GitHub OAuth
Section titled “GitHub OAuth”- Go to GitHub Developer Settings
- Click New OAuth App
- Fill in:
- Application name:
Supen - Homepage URL:
https://yourdomain.com - Authorization callback URL:
https://your-project-ref.supabase.co/auth/v1/callback
- Application name:
- Copy Client ID and generate a Client Secret
- In Supabase dashboard, paste them into GitHub provider settings
Microsoft OAuth
Section titled “Microsoft OAuth”- Go to Azure Portal
- Navigate to Azure Active Directory → App registrations
- Click New registration
- Add redirect URI:
https://your-project-ref.supabase.co/auth/v1/callback
- Go to Certificates & secrets → Create a new client secret
- Copy Application (client) ID and the secret value
- In Supabase dashboard, paste them into Microsoft provider settings
3. Cloudflare Turnstile Setup (Optional)
Section titled “3. Cloudflare Turnstile Setup (Optional)”Cloudflare Turnstile provides bot protection for login and registration forms.
- Go to Cloudflare Dashboard
- Navigate to Turnstile
- Click Add Site
- Configure:
- Site name: Supen
- Domain: Your domain (e.g.,
yourdomain.com) - Widget mode: Managed (recommended)
- Copy both keys:
- Site Key (public) - Used to render the widget in the browser
- Secret Key (private) - Used to verify tokens on the server
- Add to your
.env.local:Terminal window NEXT_PUBLIC_TURNSTILE_SITE_KEY=your_site_key_hereTURNSTILE_SECRET_KEY=your_secret_key_here
Important Notes:
NEXT_PUBLIC_TURNSTILE_SITE_KEYis public and sent to the clientTURNSTILE_SECRET_KEYis private and must stay on the server- If you don’t set these keys, authentication will work without bot protection
- Server-side verification happens automatically via server actions
4. Environment Variables
Section titled “4. Environment Variables”Update your .env.local file:
# Supabase (Required)NEXT_PUBLIC_SUPABASE_URL=https://your-project-ref.supabase.coNEXT_PUBLIC_SUPABASE_ANON_KEY=your_anon_key_here
# Cloudflare Turnstile (Optional)NEXT_PUBLIC_TURNSTILE_SITE_KEY=your_site_key_here # Public keyTURNSTILE_SECRET_KEY=your_secret_key_here # Private key5. Configure Redirect URLs
Section titled “5. Configure Redirect URLs”In your Supabase project settings (Authentication → URL Configuration):
-
Add your site URL:
https://yourdomain.com -
Add redirect URLs:
https://yourdomain.com/auth/callbackhttp://localhost:3333/auth/callback (for development)
Login Page
Section titled “Login Page”Users can:
- Sign in with Google, GitHub, or Microsoft (one click)
- Sign in with email and password
- Protected by Cloudflare Turnstile (if configured)
Route: /[locale]/login (e.g., /en/login, /zh/login)
Register Page
Section titled “Register Page”Users can:
- Sign up with Google, GitHub, or Microsoft (one click)
- Sign up with email and password
- Password confirmation validation
- Protected by Cloudflare Turnstile (if configured)
Route: /[locale]/register
OAuth Callback
Section titled “OAuth Callback”The OAuth callback handler is at /[locale]/auth/callback and automatically:
- Exchanges the OAuth code for a session
- Redirects to the intended page
- Shows error messages if authentication fails
Security Features
Section titled “Security Features”1. JWT Validation
Section titled “1. JWT Validation”- Uses
getClaims()instead of deprecatedgetUser()for secure authentication - Prevents CVE-2025-29927 vulnerability (x-middleware-subrequest header bypass)
- Validates JWT signature against Supabase’s published public keys
2. Bot Protection
Section titled “2. Bot Protection”- Cloudflare Turnstile provides invisible CAPTCHA
- Only activates when suspicious activity is detected
- User-friendly and accessible
3. Row Level Security (RLS)
Section titled “3. Row Level Security (RLS)”- All database tables have RLS policies
- Users can only access their own data
- Enforced at the database level
Development
Section titled “Development”Testing OAuth Locally
Section titled “Testing OAuth Locally”-
Update OAuth app redirect URLs to include:
http://localhost:3333/auth/callback -
Start the dev server:
Terminal window pnpm dev -
Visit
http://localhost:3333/en/login
Testing Without Turnstile
Section titled “Testing Without Turnstile”Simply don’t set NEXT_PUBLIC_TURNSTILE_SITE_KEY in your environment. The widget will not render and users can authenticate without CAPTCHA.
Troubleshooting
Section titled “Troubleshooting”OAuth Redirect Mismatch
Section titled “OAuth Redirect Mismatch”Error: “redirect_uri_mismatch”
Solution: Ensure the redirect URI in your OAuth provider settings exactly matches:
https://your-project-ref.supabase.co/auth/v1/callbackTurnstile Not Showing
Section titled “Turnstile Not Showing”Issue: Turnstile widget doesn’t appear
Possible causes:
NEXT_PUBLIC_TURNSTILE_SITE_KEYis not set (this is expected)- Domain doesn’t match the one configured in Cloudflare
- Check browser console for errors
Session Not Persisting
Section titled “Session Not Persisting”Issue: User gets logged out on page refresh
Solution: Check that cookies are being set properly. Ensure your app is served over HTTPS in production.
How Turnstile Verification Works
Section titled “How Turnstile Verification Works”Client-Side (Browser)
Section titled “Client-Side (Browser)”- User fills out login/register form
- Turnstile widget challenges the user (if suspicious activity detected)
- Widget returns a token to the client
- Token is sent to the server along with credentials
Server-Side (Verification)
Section titled “Server-Side (Verification)”- Server action receives the token from the client
- Server calls Cloudflare’s verification endpoint with:
- Secret key (from
TURNSTILE_SECRET_KEY) - User’s token
- User’s IP address (optional, for better fraud detection)
- Secret key (from
- Cloudflare responds with success/failure
- If verification fails, authentication is rejected
- If verification succeeds, authentication proceeds
Security Note: The secret key never leaves the server, preventing token forgery.
Architecture
Section titled “Architecture”Components
Section titled “Components”- oauth-buttons.tsx: OAuth provider buttons
- turnstile-widget.tsx: Cloudflare Turnstile client-side widget
- turnstile/verify.ts: Server-side Turnstile token verification
- actions.ts: Server actions for login/register with Turnstile
- login/page.tsx: Login page
- register/page.tsx: Registration page
- auth/callback/route.ts: OAuth callback handler
Auth Flow
Section titled “Auth Flow”┌─────────────┐│ User clicks││ OAuth button│└──────┬──────┘ │ v┌─────────────────────┐│ Supabase redirects ││ to OAuth provider │└──────────┬──────────┘ │ v┌─────────────────────┐│ User authenticates ││ with OAuth provider│└──────────┬──────────┘ │ v┌─────────────────────┐│ Provider redirects ││ to /auth/callback │└──────────┬──────────┘ │ v┌─────────────────────┐│ Exchange code for ││ session token │└──────────┬──────────┘ │ v┌─────────────────────┐│ Redirect to app ││ (authenticated) │└─────────────────────┘Design Philosophy
Section titled “Design Philosophy”The authentication UI follows the Manus design aesthetic:
- Clean, modern interface
- Centered layout with gradient background
- Card-based form design with rounded corners
- Clear visual hierarchy
- OAuth buttons with brand colors
- Subtle animations and transitions
- Accessible and responsive
Next Steps
Section titled “Next Steps”- Add password reset functionality
- Add email change functionality
- Add 2FA/MFA support
- Add account deletion
- Add session management (view active sessions)