Flexible storage abstractions supporting various persistence mechanisms for session and configuration data.
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;
}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();
}
}Common storage keys used by the SDK:
CognitoIdentityServiceProvider.[poolId].[username].accessTokenCognitoIdentityServiceProvider.[poolId].[username].idTokenCognitoIdentityServiceProvider.[poolId].[username].refreshTokenCognitoIdentityServiceProvider.[poolId].[username].clockDriftCognitoIdentityServiceProvider.[poolId].LastAuthUserWeb Browser:
localStorageCookieStorage for cross-subdomain accessReact Native:
@react-native-async-storage/async-storageNode.js: