addons/dexie-cloud/TODO-SOCIALAUTH.md
This feature adds support for OAuth 2.0 social login providers (Google, GitHub, Microsoft, Apple, and custom OAuth2) as an alternative to the existing OTP (One-Time Password) email authentication in Dexie Cloud.
Key Design Principle: The Dexie Cloud server acts as an OAuth broker, handling all provider interactions including the OAuth callback. The client library (dexie-cloud-addon) never receives provider tokens - only Dexie Cloud authorization codes which are exchanged for Dexie Cloud tokens.
dexie-cloud-server repository
src/api/oauth/registerOAuthEndpoints.ts - OAuth endpointssrc/api/oauth/oauth-helpers.ts - Provider exchange logicsrc/api/registerTokenEndpoint.ts - Token endpoint (authorization_code grant)GET /auth-providersGET /oauth/login/:provider (full page redirect)/oauth/callback/:providerdxc-auth query parameter (base64url-encoded JSON)dxc-auth in db.cloud.configure(), exchanges code for tokens via POST /token| Method | Use Case | Delivery Mechanism |
|---|---|---|
| Full Page Redirect | Web SPAs (recommended) | HTTP redirect with dxc-auth query param |
| Custom URL Scheme | Capacitor/Native apps | Deep link redirect (e.g., myapp://) |
OAuthProviderConfig)GET /auth-providers endpoint - Returns enabled providers and OTP statusGET /oauth/login/:provider endpoint - Initiates OAuth flow with PKCEGET /oauth/callback/:provider endpoint - Handles provider callback, redirects with dxc-authPOST /token with grant_type: "authorization_code" - Exchanges Dexie auth code for tokensoauth-helpers.ts)OAuthProviderInfo type - Provider metadataAuthProvidersResponse type - Response from /auth-providersAuthorizationCodeTokenRequest type - Token request for OAuth codesLoginHints interface - Added provider, oauthCodeDXCProviderSelection interaction type - For provider selection UIDexieCloudOptions extension - Added socialAuth, oauthRedirectUrifetchAuthProviders() - Fetches available providers from serverstartOAuthRedirect() - Initiates OAuth via full page redirectparseOAuthCallback() - Parses dxc-auth query parametercleanupOAuthUrl() - Removes dxc-auth from URL via history.replaceState()exchangeOAuthCode() - Exchanges Dexie auth code for tokensconfigure() - Auto-detects dxc-auth on page loadonDbReady - Completes login when database is readylogin() function - Supports provider and oauthCode hintsProviderSelectionDialog - Renders provider selection screenAuthProviderButton - Renders individual provider buttons with iconsOtpButton - "Continue with email" optionLoginDialog.tsx - Handles OAuth redirect flowStyles.ts - Provider button stylesOAuthError class - Error codes: access_denied, invalid_state, email_not_verified, expired_code, provider_error, network_errorUnit tests for OAuth flow
parseOAuthCallback() with various dxc-auth payloadsIntegration tests
Manual testing
samples/dexie-cloud-todo-appUpdate README.md
db.cloud.login({ provider: 'google' })Update dexie.org docs
socialAuth and oauthRedirectUri options// Configure database
db.cloud.configure({
databaseUrl: 'https://mydb.dexie.cloud'
});
// OAuth callback is handled automatically!
// When page loads with ?dxc-auth=..., the addon:
// 1. Detects the parameter in configure()
// 2. Cleans up the URL immediately
// 3. Completes login in db.on('ready')
// To manually initiate OAuth (e.g., from custom UI):
await db.cloud.login({ provider: 'google' });
// Page redirects to OAuth provider, then back with auth code
// Configure with custom URL scheme
db.cloud.configure({
databaseUrl: 'https://mydb.dexie.cloud',
oauthRedirectUri: 'myapp://'
});
// Handle deep link in app
App.addListener('appUrlOpen', async ({ url }) => {
const callback = handleOAuthCallback(url);
if (callback) {
await db.cloud.login({
oauthCode: callback.code,
provider: callback.provider
});
}
});
// Initiate login (opens system browser)
await db.cloud.login({ provider: 'google' });
┌──────────────────────────────────────────────────────────────────────────────┐
│ CLIENT (dexie-cloud-addon) │
│ │
│ ┌─────────────────┐ ┌───────────────────┐ │
│ │ LoginDialog │───▶│ startOAuthRedirect│──▶ window.location.href = │
│ │ (default UI) │ │ () │ /oauth/login/:provider │
│ └─────────────────┘ └───────────────────┘ │
│ │
│ ... page navigates away, user authenticates ... │
│ │
│ ┌─────────────────┐ ┌───────────────────┐ │
│ │ Page loads with │───▶│ db.cloud. │──▶ Detects dxc-auth param │
│ │ ?dxc-auth=... │ │ configure() │ Cleans URL immediately │
│ └─────────────────┘ └───────────────────┘ Stores pending code │
│ │ │
│ ▼ │
│ ┌─────────────────┐ ┌─────────────────────────┐ │
│ │ db.on('ready') │───▶│ POST /token │ │
│ │ │ │ grant_type: │ │
│ │ │◀───│ authorization_code │ │
│ └─────────────────┘ └─────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ User logged in! │ │
│ └─────────────────┘ │
└──────────────────────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────────────────────┐
│ DEXIE CLOUD SERVER │
│ │
│ /oauth/login/:provider │
│ ├── Generate state, PKCE │
│ ├── Store in challenges table │
│ └── Redirect to OAuth provider │
│ │
│ /oauth/callback/:provider ◀── OAuth provider redirects here │
│ ├── Verify state │
│ ├── Exchange code for provider tokens (server-side!) │
│ ├── Fetch user info, verify email │
│ ├── Generate Dexie auth code (single-use, 5 min TTL) │
│ └── HTTP 302 redirect with ?dxc-auth=<base64url-json> │
│ │
│ POST /token (grant_type: authorization_code) │
│ ├── Validate Dexie auth code │
│ ├── Extract stored user claims │
│ └── Return Dexie Cloud access + refresh tokens │
└──────────────────────────────────────────────────────────────────────────────┘
dxc-auth Query ParameterThe OAuth callback uses a single dxc-auth query parameter containing base64url-encoded JSON to avoid collisions with app query parameters:
Success:
{ "code": "DEXIE_AUTH_CODE", "provider": "google", "state": "..." }
Error:
{ "error": "Error message", "provider": "google", "state": "..." }
Example URL:
https://myapp.com/?dxc-auth=eyJjb2RlIjoiLi4uIiwicHJvdmlkZXIiOiJnb29nbGUiLCJzdGF0ZSI6Ii4uLiJ9
dexie-cloud-common:
src/OAuthProviderInfo.tssrc/AuthProvidersResponse.tssrc/AuthorizationCodeTokenRequest.tsdexie-cloud-addon:
src/authentication/oauthLogin.ts - startOAuthRedirect(), mapOAuthError()src/authentication/handleOAuthCallback.ts - parseOAuthCallback(), cleanupOAuthUrl()src/authentication/exchangeOAuthCode.ts - Token exchangesrc/authentication/fetchAuthProviders.ts - Fetch available providerssrc/errors/OAuthError.ts - OAuth-specific errorssrc/default-ui/ProviderSelectionDialog.tsx - Provider selection UIsrc/default-ui/AuthProviderButton.tsx - Provider button componentsrc/dexie-cloud-client.ts - OAuth detection in configure(), processing in onDbReadysrc/DexieCloudOptions.ts - socialAuth, oauthRedirectUri options