|
| 1 | +# ForgeRock SDK Effects - Storage |
| 2 | + |
| 3 | +This package provides a storage effect for managing token storage within the ForgeRock JavaScript SDK ecosystem. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @forgerock/sdk-effects-storage |
| 9 | +# or |
| 10 | +yarn add @forgerock/sdk-effects-storage |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +The `storage` effect facilitates getting, setting, and removing items from browser storage (`localStorage` or `sessionStorage`) or a custom token store implementation. |
| 16 | + |
| 17 | +```typescript |
| 18 | +import { storage } from '@forgerock/sdk-effects-storage'; |
| 19 | +import { TokenStoreObject } from '@forgerock/sdk-types'; |
| 20 | + |
| 21 | +// Example using localStorage |
| 22 | +const storageEffect = storage({ |
| 23 | + tokenStore: 'localStorage', |
| 24 | + prefix: 'fr-auth', |
| 25 | + clientId: 'my-client-id', |
| 26 | +}); |
| 27 | + |
| 28 | +const storageApi = storageEffect(); |
| 29 | + |
| 30 | +async function manageTokens() { |
| 31 | + // Set a token |
| 32 | + await storageApi.set('someTokenValue'); |
| 33 | + |
| 34 | + // Get a token |
| 35 | + const token = await storageApi.get(); |
| 36 | + console.log(token); // Output: 'someTokenValue' |
| 37 | + |
| 38 | + // Remove a token |
| 39 | + await storageApi.remove(); |
| 40 | + |
| 41 | + // Verify removal |
| 42 | + const removedToken = await storageApi.get(); |
| 43 | + console.log(removedToken); // Output: null |
| 44 | +} |
| 45 | + |
| 46 | +// Example using a custom token store |
| 47 | +const myCustomStore: TokenStoreObject = { |
| 48 | + get: async (key) => { |
| 49 | + /* ... custom logic ... */ return null; |
| 50 | + }, |
| 51 | + set: async (key, value) => { |
| 52 | + /* ... custom logic ... */ |
| 53 | + }, |
| 54 | + remove: async (key) => { |
| 55 | + /* ... custom logic ... */ |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +const customStorageEffect = storage( |
| 60 | + { |
| 61 | + tokenStore: 'localStorage', // This is ignored when customTokenStore is provided |
| 62 | + prefix: 'fr-auth', |
| 63 | + clientId: 'my-client-id', |
| 64 | + }, |
| 65 | + myCustomStore, |
| 66 | +); |
| 67 | + |
| 68 | +const customStorageApi = customStorageEffect(); |
| 69 | +// Use customStorageApi.get(), .set(), .remove() as above |
| 70 | +``` |
| 71 | + |
| 72 | +## Configuration |
| 73 | + |
| 74 | +The `storage` function accepts a configuration object with the following properties: |
| 75 | + |
| 76 | +- `tokenStore`: Specifies the storage mechanism. Can be `'localStorage'`, `'sessionStorage'`, or a custom object conforming to the `TokenStoreObject` interface. |
| 77 | +- `prefix`: A string prefix used in generating the storage key. |
| 78 | +- `clientId`: The client ID, also used in generating the storage key. |
| 79 | + |
| 80 | +An optional second argument allows providing a `customTokenStore` object directly, which overrides the `tokenStore` configuration property if provided. |
| 81 | + |
| 82 | +The storage key is generated as \`\` `${prefix}-${clientId}` |
0 commit comments