Authentication Flow
Relevant source files
The following files were used as context for generating this wiki page:
The Authentication Flow in OS Chat is built on a hybrid model supporting both Google OAuth and anonymous sessions through Convex Auth. It provides a secure mechanism for user identification, session management, and data isolation, ensuring that users can only access their own data. The system is designed to handle rate limiting and secure storage of sensitive credentials like third-party API keys using encryption.
Sources: src/routes/security.tsx:40-45, convex/auth.ts:153-170
Authentication Architecture
The architecture utilizes Convex Auth as the central provider, managing both authenticated and anonymous user states. For authenticated users, Google OAuth is the primary provider, while anonymous users are assigned temporary identities to enforce base rate limits.
Components and Mechanisms
- Convex Auth: Manages the lifecycle of user accounts and sessions.
- Google OAuth: Handles primary identity verification and fetches user profile details such as name, email, and profile images.
- Anonymous Provider: Allows users to interact with the application without a permanent account, though with lower usage limits.
- Data Isolation: User-specific encryption keys ensure that data remains isolated and accessible only to the owner.
Sources: convex/auth.ts:153-175, src/routes/security.tsx:32-34, src/routes/security.tsx:45-50
The diagram shows the decision path for a user choosing between Google OAuth and anonymous access, both converging in the Convex Auth engine for database persistence and session management.
User Creation and Profile Synchronization
When a user signs in, the system either retrieves an existing record or creates a new one. For OAuth sign-ins, profile data (name, email, picture) is automatically kept fresh during every login session.
User Initialization Logic
For new users, the system initializes default preferences and rate limits:
- Default Model: Sets
preferredModelto a system default. - Model Configuration: Initializes empty
disabledModelsand sets recommended models as favorites. - Rate Limiting: Initializes counters for daily, monthly, and premium usage based on the user type (Anonymous vs. Authenticated).
Sources: convex/auth.ts:15-40, convex/auth.ts:175-200
User Field Initialization
| Field | Purpose | Default / Logic |
|---|---|---|
isAnonymous | Identifies user account type | Boolean based on auth provider |
preferredModel | Default AI model for the user | System constant MODEL_DEFAULT |
favoriteModels | Models marked for quick access | RECOMMENDED_MODELS list |
emailVerificationTime | Timestamp of email verification | Date.now() (Automatic for OAuth) |
Sources: convex/auth.ts:15-20, convex/auth.ts:184-210
Server-Side Middleware and Verification
To secure server-side routes in TanStack Start, the application employs an authMiddleware. This middleware extracts tokens from headers and validates the session with the backend.
Request Processing
- Token Extraction: The middleware looks for a
Bearertoken in theAuthorizationheader. - Client Creation: If found, it creates an authenticated
ConvexHttpClient. - Context Injection: The token and client are injected into the request context for subsequent route handlers.
Sources: src/lib/middleware/auth.ts:40-105
The sequence diagram illustrates the token extraction and validation process performed by the server-side middleware for ogni request.
OAuth Callback Handling
The callback route (/auth/callback) is responsible for finalizing connections, particularly for external service connectors.
Callback Workflow
- Status Tracking: Tracks the state as
checking,success, orerror. - Verification: Uses a
connectionRequestId(stored insessionStorage) to verify the connection status via a server API. - Persistence: Once verified, the connection ID is saved to the database via the
saveConnectionmutation. - Redirection: Automatically redirects the user back to settings or the home page based on the outcome.
Sources: src/routes/auth/callback.tsx:35-100
Security and Encryption of Credentials
For users providing their own API keys (e.g., OpenAI, Anthropic), the application implements a multi-layered security approach.
Encryption Process
- Algorithm: AES-GCM 256-bit encryption.
- Key Derivation: PBKDF2 with 100,000 iterations.
- Salt Management: A unique salt is generated per user by hashing the
userId. - Isolation: Keys are encrypted using the derived user-specific key, ensuring that even if the database is compromised, keys cannot be decrypted without the application's secret and the specific user's context.
Sources: convex/api_keys.ts:75-100, src/routes/security.tsx:23-28
The diagram represents the cryptographic pipeline for securing user API keys, highlighting the use of user-specific salts for key derivation.
Sources: convex/api_keys.ts:75-105
Conclusion
The authentication flow provides a robust foundation for user security by combining industry-standard OAuth 2.0 with the real-time capabilities of Convex. By managing both authenticated and anonymous states, and employing rigorous encryption for third-party credentials, the system balances ease of use with professional-grade data protection.
Sources: src/routes/security.tsx:10-15, src/routes/privacy.tsx:110-120