Authentication Methods
The API supports four authentication token types, each identified by prefix:1. JWT Tokens (Standard Authentication)
The primary authentication method for user-facing applications. Token Prefixes:- No specific prefix for JWT tokens
 - Detected by JWT structure (header.payload.signature)
 
access_token: Valid for 6 hoursrefresh_token: Valid for 1 day
2. Workspace API Keys (RWAK)
Programmatic access scoped to a specific workspace. Token Prefix:RWAK
Use Case: Server-to-server integrations, automation scripts, CI/CD pipelines
3. User API Keys (RUAK)
Programmatic access scoped to a specific user within a workspace. Token Prefix:RUAK
Use Case: User-specific automation, personal integrations
4. God Keys (GODK)
Super admin system access for platform operations. Token Prefix:GODK
Use Case: Platform administration, system maintenance (restricted)
God Keys (GODK) provide unrestricted system access and should only be used by authorized platform administrators. Never share or expose God Keys.
JWT Authentication Flow
The standard authentication flow for most applications.Step 1: Login
Exchange credentials for a refresh token:The refresh token is stored in Redis cache for validation. Keep it secure and use it only to obtain access tokens.
Step 2: Get Access Token
Exchange the refresh token and workspace selection for an access token:Step 3: Use Access Token
Include the access token in the Authorization header for all API requests:OAuth Social Authentication
The API supports OAuth login via Google, LinkedIn, and Facebook using Passport.js.Supported Providers
- Google OAuth - 
/auth/google - LinkedIn OAuth - 
/auth/linkedin - Facebook OAuth - 
/auth/facebook 
OAuth Flow
1
Initiate OAuth
Redirect users to the provider’s OAuth endpoint:Users will be redirected to the provider’s login page.
2
OAuth Callback
After authentication, the provider redirects to our callback URL:The callback exchanges the OAuth code for a session.
3
Session Cookie
OAuth flows use cookie-based refresh token storage for session management.
Provider Account Linking
Users can link or unlink social providers to their existing account: Get Connected Providers:{provider} is one of: google, facebook, linkedin
Provider Storage
Social provider IDs are stored in the Users table:googleId- Google account identifierlinkedinId- LinkedIn account identifierfacebookId- Facebook account identifier
API Key Authentication
For programmatic access without user interaction.Workspace API Keys (RWAK)
Provide access to all resources within a specific workspace. Token Format:RWAK_xxxxxxxxxxxxxxxx
Usage:
User API Keys (RUAK)
Provide access scoped to a specific user’s permissions within a workspace. Token Format:RUAK_xxxxxxxxxxxxxxxx
Usage:
API keys don’t expire but can be revoked. Store them securely like passwords. Contact support to generate API keys for your account.
Token Management
Token Validation
The authentication middleware validates tokens in this order:- Check for 
Authorization: Bearer <token>header - Detect token type by prefix (RWAK, RUAK, GODK) or structure (JWT)
 - Validate against Redis cache (for JWT) or database (for API keys)
 - Decode token and lookup associated TenantUser
 - Populate 
req.userwith user context 
Workspace Switching
Users can switch between workspaces without re-authenticating:Token Refresh
When an access token expires (after 6 hours):Logout
Invalidate your tokens and end the session:Multi-Tenant Architecture
All data in Royalti.io is isolated by tenant (workspace).Tenant Context
- Every resource has a 
TenantIdforeign key - JWT tokens carry 
activeWorkspacefor context - All queries are automatically scoped to the active tenant
 - Users can belong to multiple tenants with different roles
 
Custom Domains
Workspaces can use custom domains via Cloudflare SaaS integration:- Custom domain detected in request
 - Authentication middleware maps domain to workspace
 - Automatic tenant context without explicit workspace parameter
 
Password Management
Forgot Password
Request a password reset:/auth/resetpassword.
Reset Password
Reset password with verification code:Set Password
For new users who verified their email:Magic Link Authentication
Request a passwordless login link:Best Practices
Security
- 
Use HTTPS Only
- All API requests must use HTTPS
 - Verify SSL certificates in production
 - Never transmit tokens over HTTP
 
 - 
Secure Token Storage
- Store tokens securely (environment variables, secure vaults)
 - Never commit tokens to version control
 - Use separate tokens for development and production
 
 - 
Token Rotation
- Implement token refresh logic before expiration
 - Handle 401 errors by refreshing tokens
 - Re-authenticate if refresh token expires
 
 - 
API Key Management
- Treat API keys like passwords
 - Rotate API keys periodically
 - Revoke unused or compromised keys immediately
 
 
Error Handling
Handle authentication errors gracefully:Rate Limiting
The login endpoint is rate limited:- 20 requests per 3 minutes per IP address
 
Authentication Middleware
Protected routes use two middleware layers:- basicAuth - JWT validation, populates 
req.user - auditMiddleware - Captures user context (userId, role, IP, user agent)
 
- enhancedGodminAuth - Supports both GODK and JWT with isAdmin=true
 - subscriptionMiddleware - Validates plan features and limits
 
Troubleshooting
Common Issues
401 Unauthorized- Token expired - refresh your access token
 - Invalid token - verify token is correct and not corrupted
 - Token not in Redis - re-authenticate to get new tokens
 
- Insufficient permissions - check user role and workspace access
 - Subscription limitation - verify plan includes requested feature
 
- Workspace ID doesn’t exist
 - User not member of workspace
 - Workspace is inactive
 
Debug Tips
- Verify token format and structure
 - Check token expiration timestamps
 - Confirm workspace ID is correct
 - Validate user has required permissions
 - Check Redis connection for JWT validation
 
Support
For authentication issues or API key requests:- Email: api@royalti.io
 - Support Center: support.royalti.io
 - API Status: status.royalti.io