Authentication Methods
Sparklytics supports multiple authentication modes for different deployment scenarios.Auth Modes
Configure viaSPARKLYTICS_AUTH environment variable:
Authentication mode:
none, password, or localLocal Mode (Recommended)
First-run experience:- Navigate to dashboard → redirected to
/setup - Create admin password (one-time)
- Login with password
- Admin password stored as Argon2id hash in database
- Password change endpoint available
- Session cookie authentication
- API key generation
Password Mode
Single shared password for all users, configured via environment variable. Features:- No first-run setup page
- Password cannot be changed via API (env-only)
- Session cookie authentication
- API key generation
None Mode
No authentication required (development/private networks only). Features:- Dashboard accessible without login
- Auth endpoints return
404 Not Found - No API keys
Session Authentication
Login Flow
Request:Cookie Details
JWT token containing session claims
HttpOnly- Prevents JavaScript access (XSS protection)SameSite=Strict- CSRF protectionPath=/- Valid for all endpointsMax-Age- Calculated fromSPARKLYTICS_SESSION_DAYS(default: 7 days = 604,800 seconds)Secure- Only sent over HTTPS (controlled bySPARKLYTICS_HTTPS, defaults totrue)
JWT Token Structure
Claims:Subject (always
admin in self-hosted mode)Expiration timestamp (Unix seconds)
Issued-at timestamp (Unix seconds)
- Algorithm: HS256 (HMAC with SHA-256)
- Secret: 64-character hex string (auto-generated, stored in database)
- Secret rotation: automatic on password change (invalidates all sessions)
Session Management
Check Authentication Status
Auth mode:
local or password (returns 404 in none mode)true if admin password not configured (local mode only)true if valid session cookie presentGet Session Details
Logout
Rate Limiting (Login)
Protection: 5 failed login attempts per 15 minutes per IP address Response (429 Too Many Requests):Password Management
Setup Password (Local Mode Only)
First-run only. Returns410 Gone if already configured.
- Minimum 12 characters
- Must contain: uppercase, lowercase, number, special character
Change Password (Local Mode Only)
Requires authentication. Invalidates all existing sessions.- JWT secret rotated (new 64-char hex string)
- All sessions invalidated (users must re-login)
Argon2id Parameters
Password hashing uses Argon2id with configurable memory cost:- Time cost: 3 iterations
- Parallelism: 1 thread
- Output length: 32 bytes
API Key Authentication
API keys provide programmatic access to query endpoints without session cookies.API Key Format
API keys use mode-aware prefixes:Self-Hosted Mode
- Prefix:
spk_selfhosted_ - Random part: 32 hex characters
- Total length: 47 characters
Cloud Mode
- Prefix:
spk_live_ - Random part: 32 hex characters
- Total length: 40 characters
Storage
API keys are hashed before storage:- Hash algorithm: SHA-256
- Stored fields:
key_hash- Full SHA-256 hash (64 hex chars)key_prefix- First 20 characters of raw key (for display)name- User-provided labelcreated_at- Timestamp
Create API Key
Requires session authentication.Key identifier (format:
key_ + 10 alphanumeric chars)Full API key - Save this immediately, you cannot retrieve it later
Display-safe prefix (first 20 characters)
List API Keys
Requires session authentication.Number of keys to return (1-100)
Pagination offset
Revoke API Key
Requires session authentication.Using API Keys
Send API keys in theAuthorization header with Bearer scheme:
GET /api/websites- List websitesGET /api/websites/:id/stats- Website statisticsGET /api/websites/:id/pageviews- Pageview breakdownGET /api/websites/:id/events- Event log- All other query endpoints (sessions, funnels, retention, etc.)
- Event collection (
POST /api/collectis unauthenticated) - Auth management endpoints (require session cookies)
- Website creation/deletion (require session cookies)
Event Collection (Unauthenticated)
ThePOST /api/collect endpoint does not require authentication.
Why?
- Client-side JavaScript must be able to send events
- CORS restrictions prevent cookie-based auth from all origins
- Rate limiting (60 req/min per IP) prevents abuse
- Unknown
website_idvalues are rejected with404 Not Found - Events for deleted websites are rejected
Security Best Practices
HTTPS Only (Production)
Always deploy with HTTPS in production:Secure cookie flag, preventing session cookies from being sent over plain HTTP.
Local development only:
Reverse Proxy Recommendations
When deploying behind a reverse proxy (Nginx, Caddy, Traefik):- Terminate TLS at the proxy (use automatic certificates with Caddy)
- Configure trusted proxies:
- Ensure proxy forwards client IP:
API Key Rotation
Best practices:- Rotate API keys every 90 days
- Use separate keys for different environments (production, staging)
- Revoke compromised keys immediately
- Store keys in environment variables or secrets manager (never commit to git)
Password Requirements
Enforced in local mode setup:- Minimum 12 characters
- At least one uppercase letter
- At least one lowercase letter
- At least one number
- At least one special character
Multi-Tenancy (Cloud Mode)
Cloud deployments use Clerk for authentication:- Organizations map to
tenant_id - Org-scoped JWT claims enforce data isolation
- PostgreSQL stores user/org metadata
- ClickHouse analytics tables include
tenant_idcolumn
Self-hosted mode always stores
tenant_id as NULL (single-tenant).