From 266915bef5ef4657bac78261c80d3d990daa88c6 Mon Sep 17 00:00:00 2001 From: Jake Whelan Date: Thu, 20 Jun 2024 14:02:06 +0100 Subject: [PATCH 1/2] add non-memory storage default for anon users --- .../react/src/context/slash-id-context.tsx | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/react/src/context/slash-id-context.tsx b/packages/react/src/context/slash-id-context.tsx index fb58a24e..b7f71616 100644 --- a/packages/react/src/context/slash-id-context.tsx +++ b/packages/react/src/context/slash-id-context.tsx @@ -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 }) => { 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(); } }; @@ -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 => { From 329561d98249be97956e2889747980b5b3a5c25f Mon Sep 17 00:00:00 2001 From: Jake Whelan Date: Thu, 20 Jun 2024 14:03:32 +0100 Subject: [PATCH 2/2] changeset --- .changeset/curly-shirts-sing.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/curly-shirts-sing.md diff --git a/.changeset/curly-shirts-sing.md b/.changeset/curly-shirts-sing.md new file mode 100644 index 00000000..1843a69e --- /dev/null +++ b/.changeset/curly-shirts-sing.md @@ -0,0 +1,5 @@ +--- +"@slashid/react": minor +--- + +Storage will default to localStorage when anonymous users are enabled