Architecture
Supabase Auth consists of four major layers:- Client Layer: Your application code using Supabase SDKs or direct HTTP calls
- Kong API Gateway: Routes requests and validates JWTs
- Auth Service (GoTrue): Manages authentication logic and token issuance
- PostgreSQL Database: Stores user data in the
authschema
How Authentication Works
When a user signs in:- Client sends credentials to the Auth service via the API gateway
- Auth service validates credentials against the database
- Upon success, Auth service issues a JWT (JSON Web Token)
- Client stores the JWT and includes it in subsequent requests
- Kong gateway validates the JWT on each request
- PostgreSQL uses the JWT to enforce Row Level Security policies
Authentication Methods
Supabase Auth supports multiple authentication methods:Email and Password
The traditional username/password approach:Magic Links
Passwordless authentication via email:OAuth Providers
Supabase supports 20+ OAuth providers including:- GitHub
- Apple
- Microsoft
- Discord
- And many moreβ¦
Phone Authentication
SMS-based authentication:Anonymous Sign-In
Create temporary users without credentials:User Management
User Object
The user object contains authentication data:User Metadata
Store additional user information:Profile Tables
Create a public profile table linked to auth.users:Session Management
Automatic Session Refresh
Supabase SDKs automatically refresh expired sessions:Manual Session Management
Session Callbacks
Listen for auth state changes:Multi-Factor Authentication (MFA)
Add an extra layer of security with TOTP-based MFA:Server-Side Auth
Next.js (App Router)
API Routes
Authorization with RLS
Combine Auth with Row Level Security:Auth Hooks
Customize authentication flows with hooks:- Before User Created: Modify user data before creation
- Send Email: Customize email sending
- Send SMS: Customize SMS sending
- Password Verification: Custom password validation
- Custom Access Token: Add custom claims to JWTs
- MFA Verification: Custom MFA logic
Security Best Practices
Use RLS
Always enforce authorization with Row Level Security policies.
Enable MFA
Require multi-factor authentication for sensitive operations.
Validate Emails
Enable email confirmation to verify user email addresses.
Rate Limiting
Configure rate limits to prevent abuse and brute force attacks.
Next Steps
Social Auth
Set up OAuth providers for social login
Server-Side Auth
Implement authentication in server components
Auth Hooks
Customize authentication flows with hooks
RLS Policies
Secure your data with Row Level Security
