Overview
The Platform API uses a robust JWT-based authentication system with refresh token rotation, providing secure and scalable user authentication. This guide covers the complete authentication flow, token management, and security best practices.Authentication Flow
Token Types
Access Tokens (JWT)
Short-lived tokens used for authenticating API requests:- Type: JSON Web Token (JWT)
- Default Expiry: 15 minutes
- Storage: Client-side (memory, not localStorage)
- Transmission: Authorization header (
Bearerscheme) - Payload:
Refresh Tokens
Long-lived tokens used to obtain new access tokens:- Type: Cryptographically secure random string
- Default Expiry: 7 days
- Storage: Database (hashed with bcrypt)
- Transmission: Request body
- Security: One-time use with rotation
Registration
Create a new user account with email and password.Endpoint
Request Body
Validation Rules
Example Request
Response
Implementation Details
The registration process:- Email Uniqueness Check: Verifies email is not already registered
- Password Hashing: Uses bcrypt with 10 salt rounds
- User Creation: Creates user record in database
- Token Generation: Generates both access and refresh tokens
- Token Storage: Stores hashed refresh token in database
Login
Authenticate with email and password to receive tokens.Endpoint
Request Body
Example Request
Response
Implementation Details
The login process:- User Lookup: Finds user by email
- Account Status Check: Verifies account is not disabled
- Password Verification: Compares provided password with stored hash using bcrypt
- Activity Update: Updates lastLoginAt timestamp
- Token Generation: Creates new access and refresh tokens
Error Responses
Refresh Token
Obtain a new access token using a valid refresh token.Endpoint
Headers
The refresh endpoint requires both the expired/expiring access token in the Authorization header and the refresh token in the request body. This design prevents token theft scenarios.
Request Body
Example Request
Response
Token Rotation
The refresh process implements token rotation for enhanced security:- Verify Refresh Token: Checks token is valid and not expired
- User Validation: Ensures user exists and is not disabled
- Revoke Old Token: Marks the used refresh token as revoked
- Generate New Tokens: Creates both new access and refresh tokens
- Store New Token: Saves new refresh token hash in database
Get Current User
Retrieve the authenticated user’s profile, permissions, and company memberships.Endpoint
Headers
Example Request
Response
Data Structure
The/me endpoint returns comprehensive user information:
- User Profile: Basic user information
- Platform Admin Status: Boolean indicating super admin privileges
- Global Permissions: Platform-level permissions granted to the user
- Company Memberships: List of companies with:
- Company details (name, slug, logo, status)
- Membership information (ID, status)
- Assigned roles within the company
- Aggregated permissions from all roles
Logout
Revoke the refresh token and end the user session.Endpoint
Headers
Request Body
Example Request
Response
Token Lifecycle Management
Access Token Expiry
Access tokens expire after the configured duration (default: 15 minutes):Refresh Token Expiry
Refresh tokens expire after the configured duration (default: 7 days):Token Refresh Strategy
Security Best Practices
Token Storage
Access Tokens
Recommended: Store in memory (React state, Vuex store)Acceptable: SessionStorage (for page refresh persistence)Never: LocalStorage (XSS vulnerability)
Refresh Tokens
Recommended: Secure, httpOnly cookiesAcceptable: Encrypted localStorage with CSRF protectionNever: Plain localStorage without encryption
Password Requirements
Consider implementing additional password strength validation on the client side for better user experience.
HTTPS Only
Rate Limiting
Implement rate limiting to prevent brute force attacks:Token Revocation
Refresh tokens are automatically revoked on:- Logout: Explicit user logout
- Token Refresh: Old token revoked when new one is issued
- Account Disable: All tokens revoked when account is disabled
Database Schema
Refresh tokens are stored securely in the database:Error Handling
Common Authentication Errors
401 Unauthorized
401 Unauthorized
403 Forbidden
403 Forbidden
Causes:Solution: Contact administrator for account reactivation
- Account is disabled (isDisabled = true)
- Insufficient permissions
- Company membership suspended
409 Conflict
409 Conflict
Causes:Solution: Use a different email or attempt login
- Email already registered (during registration)
422 Unprocessable Entity
422 Unprocessable Entity
Causes:Solution: Fix validation errors and retry
- Invalid request body (Zod validation failure)
- Missing required fields
- Invalid email format
- Password too short
Testing Authentication
Next Steps
Role-Based Access Control
Learn about permissions and role management
Company Management
Manage multi-tenant company workspaces
Invitation System
Invite users and create companies
API Reference
Complete API endpoint documentation