or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

authentication.mddevice-management.mdindex.mdmfa.mdsession-management.mdstorage.mduser-attributes.mduser-management.mduser-pool.md
tile.json

storage.mddocs/

Storage Options

Flexible storage abstractions supporting various persistence mechanisms for session and configuration data.

Capabilities

Storage Interface

Base interface for all storage implementations.

interface ICognitoStorage {
  /** Sets a key-value pair in storage */
  setItem(key: string, value: string): void;
  
  /** Gets a value by key from storage */
  getItem(key: string): string | null;
  
  /** Removes a key-value pair from storage */
  removeItem(key: string): void;
  
  /** Clears all data from storage */
  clear(): void;
}

CookieStorage Class

Cookie-based storage implementation for web browsers.

/**
 * Cookie-based storage implementation
 * @param data - Optional cookie configuration
 */
class CookieStorage implements ICognitoStorage {
  constructor(data?: ICookieStorageData);
  
  /** Sets a cookie */
  setItem(key: string, value: string): void;
  
  /** Gets a cookie value */
  getItem(key: string): string;
  
  /** Removes a cookie */
  removeItem(key: string): void;
  
  /** Clears all cookies set by this instance */
  clear(): void;
}

interface ICookieStorageData {
  /** Cookie domain (default: current domain) */
  domain?: string;
  /** Cookie path (default: '/') */
  path?: string;
  /** Cookie expiration in days (default: 365) */
  expires?: number;
  /** Cookie secure flag (default: true) */
  secure?: boolean;
  /** Cookie sameSite attribute */
  sameSite?: 'strict' | 'lax' | 'none';
}

Usage Examples:

import { CognitoUserPool, CookieStorage } from "amazon-cognito-identity-js";

// Default cookie storage
const cookieStorage = new CookieStorage();

// Custom cookie storage configuration
const customCookieStorage = new CookieStorage({
  domain: '.example.com',
  path: '/app',
  expires: 30, // 30 days
  secure: true,
  sameSite: 'strict'
});

// Use with user pool
const userPool = new CognitoUserPool({
  UserPoolId: 'us-east-1_XXXXXXXXX',
  ClientId: 'your-client-id',
  Storage: customCookieStorage
});

// Custom storage implementation
class CustomStorage implements ICognitoStorage {
  private storage = new Map<string, string>();
  
  setItem(key: string, value: string): void {
    this.storage.set(key, value);
    // Could also persist to IndexedDB, etc.
  }
  
  getItem(key: string): string | null {
    return this.storage.get(key) || null;
  }
  
  removeItem(key: string): void {
    this.storage.delete(key);
  }
  
  clear(): void {
    this.storage.clear();
  }
}

Storage Keys

Common storage keys used by the SDK:

  • CognitoIdentityServiceProvider.[poolId].[username].accessToken
  • CognitoIdentityServiceProvider.[poolId].[username].idToken
  • CognitoIdentityServiceProvider.[poolId].[username].refreshToken
  • CognitoIdentityServiceProvider.[poolId].[username].clockDrift
  • CognitoIdentityServiceProvider.[poolId].LastAuthUser

Platform-Specific Storage

Web Browser:

  • Default: localStorage
  • Alternative: CookieStorage for cross-subdomain access

React Native:

  • Uses @react-native-async-storage/async-storage
  • Configured automatically when React Native is detected

Node.js:

  • No default storage (sessions don't persist)
  • Must provide custom storage implementation