Authentication
AFFiNE supports multiple authentication methods to access the API. Choose the method that best fits your use case.Authentication Methods
AFFiNE provides three authentication approaches:- Session Cookies - For web applications and browsers
- Access Tokens - For API integrations and automation
- OAuth 2.0 - For third-party applications
Session-Based Authentication
Session authentication uses HTTP cookies and is primarily designed for web applications.Sign In with Password
Authenticate using email and password:Magic Link Authentication
Request a sign-in link sent to the user’s email:-
Request Magic Link
- User receives email with a magic link containing a one-time token
-
Exchange token for session
Session Cookies
Successful authentication sets these cookies:affine_session- Session identifier (HttpOnly, Secure)affine_user_id- Current user ID (client-readable)affine_csrf_token- CSRF protection token
Check Current Session
Verify the current authenticated user:cURL
JavaScript
Access Token Authentication
Access tokens are ideal for API integrations, scripts, and automation tools.Generate Access Token
Create a new access token via GraphQL (requires existing authentication):A descriptive name for the token (e.g., “CI/CD Pipeline”, “Mobile App”)
Optional expiration date. If not provided, the token never expires.
Using Access Tokens
Include the token in theAuthorization header with the Bearer scheme:
List Access Tokens
View all active tokens for the current user:The
token field is only revealed when the token is first created. After that, only the token metadata is available.Revoke Access Token
Delete a token to immediately invalidate it:OAuth 2.0 Authentication
AFFiNE supports OAuth 2.0 for third-party application integrations.Supported Providers
- Google - Sign in with Google account
- GitHub - Sign in with GitHub account
- Apple - Sign in with Apple ID
- OIDC - Custom OpenID Connect providers
OAuth Sign-In Flow
-
Redirect user to OAuth provider
- User authorizes on provider’s site
-
Provider redirects back with code
- Session is established - User is now authenticated
Initiate OAuth Flow
JavaScript
Link OAuth Account
Connect an OAuth provider to an existing account:Authentication Errors
Handle these common authentication errors:| Error Code | Description | Resolution |
|---|---|---|
AUTHENTICATION_REQUIRED | No valid authentication provided | Sign in or provide access token |
WRONG_SIGN_IN_CREDENTIALS | Invalid email/password | Check credentials |
INVALID_EMAIL_TOKEN | Magic link expired or invalid | Request a new magic link |
EMAIL_VERIFICATION_REQUIRED | Email not verified | Verify email address |
UNSUPPORTED_CLIENT_VERSION | Client version too old | Update client version |
USER_NOT_FOUND | User account doesn’t exist | Create account or check email |
Example Error Response
Security Considerations
CSRF Protection
For session-based authentication, CSRF protection is automatically enforced:- The
affine_csrf_tokencookie must be sent as a header for state-changing operations - Header name:
x-affine-csrf-token
HTTPS Required
All authentication operations must use HTTPS in production:Token Storage
Best practices for storing access tokens: ✅ Do:- Store in environment variables
- Use secret management services (AWS Secrets Manager, HashiCorp Vault)
- Encrypt at rest
- Rotate periodically
- Hardcode in source code
- Commit to version control
- Store in client-side JavaScript
- Share across applications
Multi-Factor Authentication
AFFiNE supports email-based verification:Session Management
Session Duration
- Default TTL: Sessions are long-lived and refresh automatically
- Refresh: Sessions are refreshed on each authenticated request
- Expiration: Sessions expire after extended inactivity
Multiple Sessions
AFFiNE supports multiple concurrent sessions:Sign Out
Terminate the current session:cURL
JavaScript
Sign Out Specific User
Remove a specific user from a multi-user session:Testing Authentication
Verify your authentication setup with these queries:Check Authentication Status
Verify Token Permissions
AUTHENTICATION_REQUIRED error.
Next Steps
- Choose your authentication method based on your use case
- Implement authentication in your application
- Test with simple queries to verify authentication works
- Explore the API and build your integration
Additional Resources
- GraphQL Schema: See available auth mutations in the schema
- Security Guide: Best practices for secure API usage
- Rate Limits: Understand API rate limiting