Overview
Grain Analytics supports three authentication strategies to fit different security needs. Choosing the right one depends on where your app runs and who can access it.Starting out? Use
NONE for development and demos. Switch to SERVER_SIDE or JWT for production.Strategy Comparison
| Strategy | Use Case | Security | Complexity |
|---|---|---|---|
NONE | Public apps, demos | Low | Simple |
SERVER_SIDE | Backend/Node.js | High | Medium |
JWT | Client apps with auth | High | Medium |
NONE Authentication
No authentication required. Events and configs are accessed without credentials.- Public websites or landing pages
- Development and testing
- Apps without user authentication
SERVER_SIDE Authentication
Uses a secret key to authenticate requests. Best for backend applications where the secret can be kept secure.Authorization: Chase {SECRET} header. The server validates the secret before processing.
Getting Your Secret Key
- Go to grainql.com/dashboard
- Navigate to your tenant settings
- Generate or copy your secret key
- Store it securely (environment variable recommended)
- Node.js backends
- Server-side API routes
- Serverless functions
- Any environment where secrets can be kept secure
JWT Authentication
Uses JSON Web Tokens for authentication. Perfect for client-side apps that already have user authentication.getToken() to get a fresh JWT. The token is sent in the Authorization: Bearer {TOKEN} header.
Setting Up JWT Auth
Step 1: Configure JWT in Grain Dashboard Go to your tenant settings at grainql.com/dashboard and configure:- JWT issuer (e.g., your Auth0 domain)
- JWT audience
- Public key or JWKS endpoint
getToken method:
Common Auth Integrations
- Auth0
- NextAuth.js
- Custom
JWT User ID Requirements
When using JWT authentication, the user ID in events must match the JWT’ssub (subject) claim:
Choosing a Strategy
Use this decision tree:- Is this a public website? → Use
NONE - Does your app have user authentication?
- Yes, client-side → Use
JWT - Yes, server-side → Use
SERVER_SIDE
- Yes, client-side → Use
- Is this backend code? → Use
SERVER_SIDE - Still unsure? → Start with
NONE, upgrade later
Switching Strategies
You can change authentication strategies without code changes in most cases:Query API Authentication
When using the Query API to programmatically access your analytics data, you’ll need to create a separate API key with Query API permissions:- Go to Dashboard → Settings → Authentication
- Click Generate New Secret
- Enter a descriptive name (e.g., “Query API Key”)
- Check the “Query API” permission
- Copy the generated secret
X-API-Key header when making Query API requests:
Independent Authentication: Query API authentication is completely separate from SDK authentication strategies. You can use Query API keys regardless of whether your SDK uses NONE, SERVER_SIDE, or JWT authentication. The authentication strategies above are only for data ingestion (tracking events).
Dashboard Session Management
When using the Grain dashboard, your session is managed via Auth0 with automatic token refresh:- Session Duration: Access tokens are cached for ~4 minutes and automatically refreshed
- Token Expiry: If your session expires (e.g., after prolonged inactivity), you’ll see a friendly error message with a “Reload page” action
- Long-lived Tabs: Tabs open for extended periods will automatically redirect to login when the session expires