Overview
Kuest Prediction Market uses a hybrid authentication system combining:- Better Auth: Modern authentication framework with session management
- Reown AppKit: Web3 wallet connection interface (formerly WalletConnect)
- SIWE (Sign-In with Ethereum): Cryptographic wallet authentication
- 2FA Support: TOTP-based two-factor authentication
Architecture
The authentication flow:Better Auth Configuration
Required Environment Variables
.env
Core Configuration
The auth instance is configured insrc/lib/auth.ts:
src/lib/auth.ts
Enabled Plugins
customSession - Enhanced User Data
customSession - Enhanced User Data
Extends the session with additional user information:Provides:
- Full-resolution image URLs
- Admin status based on
ADMIN_WALLETS - Sanitized trading settings (credentials removed)
siwe - Sign-In with Ethereum
siwe - Sign-In with Ethereum
Enables wallet-based authentication:Features:
- Anonymous mode (no email required initially)
- Multiple wallet support per user
- Primary wallet designation
- Automatic email generation:
{address}@{domain}
twoFactor - TOTP Authentication
twoFactor - TOTP Authentication
Optional two-factor authentication:Features:
- TOTP codes (Google Authenticator, Authy)
- Backup codes for recovery
- Trusted device cookies (30 days)
- Redirect flow for SIWE + 2FA
nextCookies - Next.js Integration
nextCookies - Next.js Integration
Reown AppKit Configuration
Required Environment Variables
.env
Create a Reown Project
- Go to https://dashboard.reown.com/
- Click New Project
- Configure:
- Name: Your prediction market name
- Type: Web3Modal v4
- Networks: Add Polygon (or your target networks)
- Copy the Project ID
Configuration
The AppKit is configured insrc/lib/appkit.ts:
src/lib/appkit.ts
Supported Networks
By default, the app uses Polygon Amoy (testnet). To change networks:src/lib/appkit.ts
Kuest CLOB currently operates on Polygon. Ensure your
defaultNetwork matches your Kuest credentials.Supported Wallets
Reown AppKit automatically includes:- MetaMask: Most popular Web3 wallet
- WalletConnect: 300+ wallets via WalletConnect protocol
- Coinbase Wallet: Coinbase’s self-custody wallet
- Trust Wallet: Mobile-first wallet
- Rainbow: Ethereum-focused wallet
- And many more…
Admin Configuration
Setting Admin Wallets
Add admin wallet addresses to your.env file:
.env
Admin Features
Admin users have access to:- Event Management: Create/edit market events
- User Management: View user stats and activity
- System Settings: Configure platform settings
- Analytics Dashboard: Advanced metrics and reports
Checking Admin Status
The application provides helper functions:src/lib/admin.ts
Database Schema
Better Auth creates these tables:users Table
wallets Table
- Users can connect multiple wallets
- One wallet is marked as primary
- Wallets are network-specific (chain_id)
sessions Table
two_factors Table
Authentication Flows
Standard Sign-In Flow
Sign-In with 2FA
Trusted Device Flow
When a user enables “Trust this device”:Affiliate System Integration
Authentication includes automatic affiliate tracking:- 30-day cookie expiration
- Automatic referral attribution
- Self-referral prevention
- Cookie cleared after registration
Session Management
Server Components
Client Components
API Routes
app/api/profile/route.ts
Troubleshooting
Wallet connection shows 'Invalid signature'
Wallet connection shows 'Invalid signature'
Check:
- Correct network: User must be on the network specified in
networksarray - REOWN_APPKIT_PROJECT_ID: Verify it’s correct and active
- SIWE domain: Must match your deployment domain
- Time sync: User’s device time must be accurate
Session expires immediately
Session expires immediately
Check
BETTER_AUTH_SECRET:- Ensure it’s set and is exactly 32 characters
- Verify it hasn’t changed between deployments
- Check for environment variable typos
2FA code always invalid
2FA code always invalid
Common issues:
- Time drift: Server and user device clocks out of sync
- Code expired: TOTP codes are valid for 30 seconds
- Wrong secret: User scanned incorrect QR code
- Sync server time:
ntpdate -u time.nist.gov - Use backup codes for recovery
- Disable 2FA via database if necessary
Admin features not showing
Admin features not showing
Verify admin configuration:
- Check
ADMIN_WALLETSis set correctly - Ensure wallet address matches exactly (case-insensitive)
- User must sign in AFTER being added to
ADMIN_WALLETS
Reown AppKit not loading
Reown AppKit not loading
Check:
- Project ID: Valid and not revoked
- Network connectivity: User can reach
relay.walletconnect.com - Browser console: Look for CORS or loading errors
- Project settings: Domain allowlist in Reown dashboard
Security Best Practices
- Rotate secrets: Change
BETTER_AUTH_SECRETperiodically (requires user re-login) - HTTPS only: Never use auth over HTTP in production
- Rate limiting: Limit sign-in attempts and 2FA verification
- Session timeout: Keep default 30-day expiration or reduce for sensitive apps
- Admin access: Regularly audit
ADMIN_WALLETSlist - Cookie security: Use
httpOnly,secure, andsameSiteflags - CSRF protection: Better Auth includes built-in CSRF tokens
Next Steps
Environment Variables
Complete variable reference
Database Setup
PostgreSQL configuration
Storage Configuration
File storage setup
Deploy to Vercel
Deploy your application