Skip to content

[Bug] useAuth hook calls getAccessToken on every streaming connection — N token reads for N streams #635

Description

@RUKAYAT-CODER

Overview

src/hooks/useStreamingData.ts (and callers in video, quiz, and course modules) calls getAccessToken() from secureStorage.ts independently for each streaming connection. getAccessToken reads from expo-secure-store, which involves a native bridge call. Under normal usage with 3 concurrent streams (video + progress + notifications), this results in 3 sequential native bridge calls per token read cycle (token is read on reconnect, on retry, and on each new stream).

Specifications

Features:

  • Token cached in memory for 60-second window (refreshed when approaching expiry)
  • All streaming connections share the same in-memory token cache
  • Cache invalidated on token refresh or logout

Tasks:

  • Add in-memory token cache in src/services/secureStorage.ts: { token: string, expiresAt: number } | null
  • getAccessToken() returns cached value if Date.now() < expiresAt - 30000
  • Cache cleared in clearTokens() and saveTokens()
  • Add unit test confirming single SecureStore read for 3 rapid getAccessToken() calls
  • Cache TTL configurable via constant

Impacted Files:

  • src/services/secureStorage.ts

Acceptance Criteria

  • 3 calls to getAccessToken() within 1 second result in 1 native bridge call
  • Cache is invalidated after saveTokens() or clearTokens()
  • Cache TTL default is 60 seconds
  • Unit test confirms single SecureStore.getItemAsync invocation for 3 rapid calls

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programbugSomething isn't workingperformancePerformance issue or optimization

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions