User IDs can be used for reporting purposes, storing the read status of Clips and Stories, followed categories, user preferences and other features. By default, the Storyteller SDK creates an externalId for users when the SDK is first initialized. externalId is stored until the user uninstalls the app or until the SDK is initialized with a different externalId.
However, if you have a user account system then you may wish to set your own user IDs within the Storyteller SDK.
The externalId should be an identifier that is unique per user and does not change. Therefore, using something like the user's email address is not a good choice for an externalId, as the user may change it at some point in the future. However, using a unique UUID/GUID would be a good choice as it is guaranteed not to change over time.
In order to supply the externalId to the Storyteller SDK, call the initialize method. This method is Promise-based and will throw an error if initialization fails.
externalId (optional): Your unique user identifier. If null or omitted, the SDK will use a default autogenerated externalId
Important Notes:
When calling StorytellerSdk.initialize with a different externalId than the previous one, all local data related to the previous user will be deleted
Call StorytellerSdk.initialize as soon as the externalId is known in your app
Never make simultaneous StorytellerSdk.initialize calls - this will result in unexpected behavior. Always await the completion of one initialization before calling it again
StorytellerSdk.initialize is asynchronous, so StorytellerSdk.isInitialized() will return true only after initialization completes
You can set the user's locale to localize content within the SDK:
StorytellerSdk.setLocale(locale?:string|null)
Ensure the locale parameter uses an ISO 639-1 two-letter code if available, otherwise use an ISO 639-2 three-letter language code for precise language specification:
// ISO 639-1 (two-letter code)StorytellerSdk.setLocale('fr');// ISO 639-2 (three-letter code)StorytellerSdk.setLocale('ace');// Clear locale preferenceStorytellerSdk.setLocale(null);
You can access information about the current API configuration:
// Get the current API keyconstapiKey:string=StorytellerSdk.currentApiKey();console.log('Current API key:',apiKey);// Check if SDK is initializedconstisInitialized:boolean=StorytellerSdk.isInitialized();console.log('SDK initialized:',isInitialized);
If your app supports login/logout functionality and you need to switch between users, call initialize again with a new externalId when the user changes:
```typescript
// User logs out - reinitialize with null or a new user's externalId
try {
await StorytellerSdk.initialize('api-key', null); // Anonymous user
// or
await StorytellerSdk.initialize('api-key', 'new-user-guid'); // New logged-in user
console.log('Switched to new user');
} catch (error) {
console.error('Failed to switch user:', error);
}
```
Important: Calling initialize with a different externalId will delete all local data associated with the previous user, including:
Read status of Clips and Stories
Followed categories
User preferences
Viewed pages history
{"slug": "users", "page_title": "Working with Users", "page_url": "Users/", "canonical_url": "/react-native/Users/", "markdown": "# Working with Users\n\nUser IDs can be used for reporting purposes, storing the read status of Clips and Stories, followed categories, user preferences and other features. By default, the Storyteller SDK creates an `externalId` for users when the SDK is first initialized. `externalId` is stored until the user uninstalls the app or until the SDK is initialized with a different `externalId`.\n\nHowever, if you have a user account system then you may wish to set your own user IDs within the Storyteller SDK.\n\nThe `externalId` should be an identifier that is unique per user and does not change. Therefore, using something like the user's email address is not a good choice for an `externalId`, as the user may change it at some point in the future. However, using a unique UUID/GUID would be a good choice as it is guaranteed not to change over time.\n\n## Initialization\n\nIn order to supply the `externalId` to the Storyteller SDK, call the `initialize` method. This method is Promise-based and will throw an error if initialization fails.\n\n=== \"v11.0.0+\"\n\n ```typescript\n try {\n await StorytellerSdk.initialize('api-key', 'user-guid');\n console.log('SDK initialized successfully');\n } catch (error) {\n console.error('Initialization failed:', error);\n }\n ```\n\n=== \"v10.x\"\n\n ```typescript\n StorytellerSdk.initialize(\n {\n apiKey: 'api-key',\n externalId: 'user-guid',\n },\n (callback: { result: Boolean; message: string }) => {\n console.log(`result: ${callback.result} message: ${callback.message}`);\n }\n );\n ```\n\n**Parameters:**\n\n- `apiKey` (required): Your Storyteller API key\n- `externalId` (optional): Your unique user identifier. If `null` or omitted, the SDK will use a default autogenerated `externalId`\n\n**Important Notes:**\n\n- When calling `StorytellerSdk.initialize` with a different `externalId` than the previous one, all local data related to the previous user will be deleted\n- Call `StorytellerSdk.initialize` as soon as the `externalId` is known in your app\n- **Never make simultaneous `StorytellerSdk.initialize` calls** - this will result in unexpected behavior. Always await the completion of one initialization before calling it again\n- `StorytellerSdk.initialize` is asynchronous, so `StorytellerSdk.isInitialized()` will return `true` only after initialization completes\n\n## Setting the User's Locale\n\nYou can set the user's locale to localize content within the SDK:\n\n```typescript\nStorytellerSdk.setLocale(locale?: string | null)\n```\n\nEnsure the `locale` parameter uses an ISO 639-1 two-letter code if available, otherwise use an ISO 639-2 three-letter language code for precise language specification:\n\n```typescript\n// ISO 639-1 (two-letter code)\nStorytellerSdk.setLocale('fr');\n\n// ISO 639-2 (three-letter code)\nStorytellerSdk.setLocale('ace');\n\n// Clear locale preference\nStorytellerSdk.setLocale(null);\n```\n\n## Current User Information\n\nYou can access information about the current API configuration:\n\n```typescript\n// Get the current API key\nconst apiKey: string = StorytellerSdk.currentApiKey();\nconsole.log('Current API key:', apiKey);\n\n// Check if SDK is initialized\nconst isInitialized: boolean = StorytellerSdk.isInitialized();\nconsole.log('SDK initialized:', isInitialized);\n```\n\n## Changing Users\n\nIf your app supports login/logout functionality and you need to switch between users, call `initialize` again with a new `externalId` when the user changes:\n\n ```typescript\n // User logs out - reinitialize with null or a new user's externalId\n try {\n await StorytellerSdk.initialize('api-key', null); // Anonymous user\n // or\n await StorytellerSdk.initialize('api-key', 'new-user-guid'); // New logged-in user\n\n console.log('Switched to new user');\n } catch (error) {\n console.error('Failed to switch user:', error);\n }\n ```\n\n**Important:** Calling `initialize` with a different `externalId` will delete all local data associated with the previous user, including:\n\n- Read status of Clips and Stories\n- Followed categories\n- User preferences\n- Viewed pages history\n", "copy_markdown_include_header": false, "base_path": "", "ai_dir": "ai", "missing_payload_behavior": "empty"}