Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/curly-shirts-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@slashid/react": minor
---

Storage will default to localStorage when anonymous users are enabled
22 changes: 11 additions & 11 deletions packages/react/src/context/slash-id-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,23 @@ SlashIDContext.displayName = "SlashIDContext";

const STORAGE_TOKEN_KEY = "@slashid/USER_TOKEN";

const createStorage = (storageType: StorageOption) => {
const createStorage = (storageType: StorageOption, options: { anonymousUsersEnabled: boolean }) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this must be handled one level above, as the SlashID context provider is the owner of this business logic.

switch (storageType) {
case "memory":
if (options.anonymousUsersEnabled) {
console.warn("Anonymous users created with 'memory' storage will not be persisted across sessions or page refreshes")
}
return new MemoryStorage();
case "localStorage":
return window.localStorage;
case "cookie":
return new CookieStorage();
default:
if (options.anonymousUsersEnabled) {
console.warn("Defaulting to 'localStorage' storage for better compatibility with anonymous users. It is recommended that you explicitly set the 'tokenStorage' prop when using anonymous users.")
return window.localStorage
}

return new MemoryStorage();
}
};
Expand Down Expand Up @@ -358,22 +366,14 @@ export const SlashIDProvider = ({
...(sdkUrl && { sdkURL: sdkUrl }),
...(typeof analyticsEnabled === "boolean" && { analyticsEnabled }),
});
const storage = createStorage(tokenStorage);
const storage = createStorage(tokenStorage, { anonymousUsersEnabled });

storageRef.current = storage;
sidRef.current = slashId;

setState("loaded");
}
}, [
oid,
baseApiUrl,
sdkUrl,
state,
tokenStorage,
analyticsEnabled,
environment,
]);
}, [oid, baseApiUrl, sdkUrl, state, tokenStorage, analyticsEnabled, environment, anonymousUsersEnabled]);

const createAndStoreUserFromToken = useCallback(
(token: string): User | AnonymousUser | null => {
Expand Down