CupThread React Native & Expo SDK - v0.1.0
    Preparing search index...

    Class UserTokenStore

    Manages device-level persistent anonymous user tokens for CupThread SDK interactions.

    Anonymous tokens allow users to submit feedback, upvote roadmap features, and participate in discussions without requiring an upfront sign-in. The token is generated as an RFC 4122 v4 UUID and can be stored across app restarts using custom storage adapters (e.g. AsyncStorage / SecureStore).

    import { UserTokenStore } from '@cupthread/react-native';

    // 1. Synchronous access to cached token
    const token = UserTokenStore.shared.token;

    // 2. Async retrieval with persistence lookup
    const persistentToken = await UserTokenStore.shared.getToken();

    // 3. Reset token (e.g. on user logout)
    const freshToken = await UserTokenStore.shared.resetToken();
    Index
    • get isPersistent(): boolean

      Indicates whether this store instance is backed by a persistent storage adapter.

      Returns boolean

      When false, user tokens and changelog seen sets exist in memory only and will not survive application restarts. Call UserTokenStore.configure or provide a custom TokenStorageAdapter to the constructor to enable durable cross-session persistence.

    • get token(): string

      Synchronously returns the currently cached user token. If not already initialized, generates and caches a new in-memory UUID.

      Returns string

      Warning — async storage adapters: with AsyncStorage / SecureStore the persisted token loads asynchronously, and accessing .token before that load finishes mints and caches a throwaway in-memory UUID that is never persisted and will be replaced once storage resolves. Any request sent in that window is attributed to the throwaway identity. With async adapters always await UserTokenStore.getToken instead (the SDK's <CupThreadProvider> does this for you and exposes readiness through useCupThreadTokenReadiness()). The synchronous contract is only safe for synchronous adapters or no-adapter setups.

      Explicit identities installed during the async load window via UserTokenStore.setToken or UserTokenStore.resetToken are authoritative and are never reverted by any storage re-read (the initial load or a later UserTokenStore.getToken).

      const userToken = UserTokenStore.shared.token;
      
    • Clears all recorded seen changelog entries from memory and storage.

      Returns Promise<void>

      await UserTokenStore.shared.clearSeenChangelogs();
      
    • Asynchronously retrieves the user token, prioritizing recovery of existing tokens from storage.

      Returns Promise<string>

      A promise resolving to the user token string.

      If storage already contains a token, it is restored into memory and returned. A new UUID is generated and persisted only when storage is verified to be empty.

      Explicit identities installed via UserTokenStore.setToken or UserTokenStore.resetToken are authoritative: getToken() never re-reads storage past one, and a storage read that was already in flight when the explicit identity was installed cannot revert it either. This also holds when the explicit identity's own persistence write failed (storage keeps an older token while the cache holds the explicit one).

      const token = await UserTokenStore.shared.getToken();
      
    • Checks whether a changelog release version or entry ID has already been marked as seen.

      Parameters

      • versionOrId: string

        Changelog version label (e.g. '1.2.0') or entry ID.

      Returns Promise<boolean>

      Promise resolving to true if seen, false otherwise.

      const hasSeen = await UserTokenStore.shared.hasSeenChangelog('1.2.0');
      
    • Marks a changelog release version or entry ID as seen and persists it.

      Parameters

      • versionOrId: string

        Changelog version label (e.g. '1.2.0') or entry ID.

      Returns Promise<void>

      await UserTokenStore.shared.markChangelogSeen('1.2.0');
      
    • Resets the active user token and generates a brand new UUID in memory and storage.

      Returns Promise<string>

      The newly generated user token.

      Like UserTokenStore.setToken, the reset is authoritative: neither an in-flight initial storage read nor a later UserTokenStore.getToken re-read (async adapters) can overwrite the cache or revert the identity.

      const newToken = await UserTokenStore.shared.resetToken();
      console.log(`New session token: ${newToken}`);
    • Overrides the current user token with an explicit identifier (e.g. from your auth service).

      Parameters

      • token: string

        Custom user identifier or token string.

      Returns Promise<void>

      The explicit token is authoritative: neither the constructor's initial storage read nor any later UserTokenStore.getToken re-read (async adapters) can overwrite the cache or revert the identity — including reads that were already in flight when setToken() was called, and including after a failed persistence write (storage may keep an older token; the in-memory identity still wins).

      await UserTokenStore.shared.setToken(user.id);
      
    • Subscribes a listener that is notified whenever the active identity changes.

      Parameters

      • listener: () => void

        Callback invoked with no arguments on identity changes.

      Returns () => void

      An unsubscribe function; safe to call multiple times.

      Listeners fire after UserTokenStore.setToken, UserTokenStore.resetToken, or an identity-changing UserTokenStore.getToken resolution completes (memory and storage both settled). Reactive consumers such as <CupThreadProvider> use this to follow documented login/logout flows.

      const unsubscribe = UserTokenStore.shared.subscribe(() => {
      console.log('identity changed:', UserTokenStore.shared.token);
      });
      unsubscribe();
    • Emits a one-time console warning in development mode if the store is not backed by a persistent storage adapter when changelog seen-status is inspected or marked.

      Returns void