DocHub
AI token authentication system and credit-based usage limits

Token Authentication & Credits

AI Token Authentication

Before recording can begin, the app must obtain a valid AI token for server authentication.

Token Endpoint

Field Value
URL https://kagtryxgjwavupzlmlzv.supabase.co/functions/v1/generate_ai_token
Method POST
Authorization User Supabase JWT (Bearer token)

Token Request

{
  "request_type": "ai_voice_chat_token"
}

Token Response

{
  "ai_token": "eyJhbGciOiJIUzI1NiIs...",
  "expires_in": 600
}

The token expires in 600 seconds (10 minutes).

Token Validation Rules

  • Valid: Token exists AND expires in more than 3 minutes
  • Invalid: No token OR token expires within 3 minutes
  • Token is stored in memory only (not persisted to storage)
  • New token requested automatically when invalid

Code Location

  • _isAITokenValid() at line ~1750 in ai_voice_tab.dart
  • _requestAIToken() at line ~1772 in ai_voice_tab.dart

Credit System

Users have a credit-based system that limits AI voice chat usage.

Credit Types

Type Description Expiry
Weekly Credits Reset periodically (tracked by weekly_reset_date) Resets on schedule
Extra/Bank Credits Purchased credits Never expire

Credit Check Logic

A user can record if:

  1. Weekly credits not exhausted (weekly_used_credits < weekly_limit_credits), OR
  2. Bank credits available (bank_used_credits < bank_limit_credits)

Credit Update Flow

  1. Server returns ai_usage_in_seconds in response
  2. App rounds up to nearest integer for credits used
  3. If weekly limit is 0: credits added directly to bank usage
  4. If weekly credits available: deduct from weekly first
  5. If weekly exhausted: deduct from bank credits
  6. Updated credits saved to user_profile.json

Source Data

  • user_profile.json -> ai_usage object
  • _hasAvailableCredits() at line ~1669
  • _updateAIUsageCredits() at line ~1581