Overview
Studley AI uses a hybrid authentication system combining:- Supabase Auth - OAuth providers (Google, GitHub, etc.)
- Custom JWT Sessions - Credential-based authentication
- Clever SSO - Education platform integration
Architecture
Studley AI implements a dual authentication system:Prerequisites
Supabase Project
Create a project at supabase.com
Database Setup
Complete database migrations
Supabase Authentication
Initial Configuration
OAuth Providers
- Google OAuth
- GitHub OAuth
- Other Providers
Create Google OAuth App
- Go to Google Cloud Console
- Create new project or select existing
- Navigate to APIs & Services → Credentials
- Click Create Credentials → OAuth 2.0 Client ID
- Application type: Web application
Configure Redirect URIs
Add authorized redirect URIs:Replace
xxxxx with your Supabase project reference.Add Credentials to Supabase
In Supabase Dashboard → Authentication → Providers → Google:
- Enable Google provider
- Client ID:
<your-client-id>.apps.googleusercontent.com - Client Secret:
<your-client-secret> - Click Save
Custom Credentials Authentication
Studley AI uses custom JWT-based sessions for email/password authentication.Session Configuration
lib/auth/session.ts
Environment Setup
Password Hashing
Passwords are hashed using bcrypt:Clever SSO Integration
For educational institutions using Clever.Register Clever Application
- Go to Clever Developer Portal
- Create new application
- Select District SSO application type
- Note your Client ID and Client Secret
Row Level Security (RLS)
Protect user data with Supabase RLS policies.Enable RLS
Additional Policies
Quiz Results
Quiz Results
Study Materials
Study Materials
Session Management
Server-Side Session Retrieval
Client-Side Supabase Session
Middleware Protection
Protect routes with Next.js middleware:middleware.ts
Testing Authentication
Troubleshooting
OAuth redirect not working
OAuth redirect not working
Error: OAuth returns to wrong URLSolutions:
- Verify redirect URL in provider settings matches exactly
- Check
NEXT_PUBLIC_APP_URLis set correctly - Ensure no trailing slashes in URLs
- For Supabase: Use
https://[project-ref].supabase.co/auth/v1/callback
Session not persisting
Session not persisting
Error: User logged out on page refreshSolutions:
- Check cookie settings:
httpOnly,secure,sameSite - Verify
SESSION_SECRETis set - In development: ensure
secure: falsefor HTTP - Check browser cookie settings
Invalid JWT token
Invalid JWT token
Error:
JWTExpired or JWTInvalidSolutions:- Check
SESSION_SECREThasn’t changed - Verify token expiration (7 days default)
- Clear cookies and re-login
- Check system time is synchronized
RLS policy blocks query
RLS policy blocks query
Error:
row-level security policy violationSolutions:- Verify user is authenticated:
auth.uid()returns value - Check policy conditions match query
- Use service role key for admin operations
- Review policy with
EXPLAINin psql
Security Best Practices
Strong Session Secrets
Generate cryptographically secure secrets:
HTTPS Only
Always use HTTPS in production:
httpOnly Cookies
Prevent XSS attacks:
CSRF Protection
Use SameSite cookies:
Next Steps
AI Configuration
Set up Groq AI for study features
File Storage
Configure file uploads with Vercel Blob
User Management
Learn about user roles and permissions
API Reference
View authentication API endpoints