Overview
Vitae uses Better-Auth for authentication, providing a secure, flexible authentication system with support for multiple social providers and session management.Authentication System
Better-Auth Configuration
The authentication system is configured in the@vitaes/auth package:
The auth instance is a singleton that’s shared across the application.
Supported Providers
Vitae supports the following authentication providers:Sign in with Google using OAuth 2.0
Sign in with GitHub using OAuth 2.0
Session Management
How Sessions Work
When a user authenticates, Better-Auth creates a session and stores it in the database. The session is identified by a cookie sent with each request.Session Context
The API creates a context for each request that includes the user’s session:Session Object Structure
When authenticated, the session object contains:Protected vs Public Procedures
Vitae defines two types of API procedures:Public Procedures
Public procedures can be called without authentication:healthCheck: Server health statushelloWorld: Test endpointgetResumeBySlug: View public resumes (with access control)setDownloadCount: Track downloads on public resumes
Protected Procedures
Protected procedures require authentication. They use middleware to verify the session:listResumes: Get all user’s resumescreateResume: Create a new resumeupdateResume: Update resume contentdeleteResume: Delete a resumeuploadThumbnail: Upload resume thumbnail
Authentication Headers & Cookies
Cookie-Based Authentication
Vitae uses HTTP-only cookies for session management. This is more secure than token-based authentication as the cookies cannot be accessed by JavaScript.Cookie Attributes
Set to
true - prevents JavaScript access to the cookieSet to
true - cookie only sent over HTTPSSet to
'none' - allows cross-origin requests with credentialsCORS Configuration
For cookie-based authentication to work across origins, the server is configured with CORS:Client Configuration
When making API requests, you must include credentials:The
credentials: 'include' option ensures cookies are sent with every request.Authentication Flow
Here’s the complete authentication flow in Vitae:1. User Initiates Login
User clicks “Sign in with Google” or “Sign in with GitHub” in the web app.2. OAuth Redirect
The user is redirected to the provider’s OAuth consent screen:3. OAuth Callback
After the user approves, the provider redirects back with an authorization code:4. Session Creation
Better-Auth:- Exchanges the code for user information
- Creates or updates the user in the database
- Creates a new session
- Sets an HTTP-only session cookie
5. Authenticated Requests
All subsequent API calls include the session cookie automatically:6. Session Validation
For each protected procedure call:- The
createContextfunction extracts the session from cookies - The
requireAuthmiddleware checks if a valid session exists - If valid, the request proceeds with user context
- If invalid, an
UNAUTHORIZEDerror is thrown
Example: Making Authenticated Requests
Setup
Create a Resume (Protected)
Access Control Example
Some procedures implement additional access control beyond authentication:Security Best Practices
Environment Variables
Always store sensitive credentials in environment variables:HTTPS in Production
Trusted Origins
Configure trusted origins to prevent CSRF attacks:Troubleshooting
”UNAUTHORIZED” Error
Cause: No valid session found Solutions:- Verify the user is logged in
- Check that
credentials: 'include'is set in fetch options - Ensure cookies are not blocked by browser settings
- Verify CORS origin is correctly configured
Cookies Not Being Sent
Cause: CORS misconfiguration Solutions:- Set
credentials: truein CORS config - Set
credentials: 'include'in fetch options - Ensure
sameSite: 'none'for cross-origin requests - Use HTTPS in production for
secure: truecookies
Session Not Persisting
Cause: Cookie not being saved Solutions:- Check browser developer tools for cookie creation
- Verify domain matches between client and server
- Ensure
httpOnlycookies are supported in your environment
Next Steps
API Overview
Learn about the oRPC-based API architecture
Endpoints
Explore all available API endpoints