From c77760094dd63dffc05249e4d7b187aa986178e3 Mon Sep 17 00:00:00 2001 From: Oscar Bazaldua <511911+oscb@users.noreply.github.com> Date: Wed, 16 Feb 2022 19:17:19 -0800 Subject: [PATCH] fix: reset anonymousId for users who have persisted incorrect value --- packages/core/src/storage/sovranStorage.ts | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/core/src/storage/sovranStorage.ts b/packages/core/src/storage/sovranStorage.ts index 674aa0c7f..02cafed99 100644 --- a/packages/core/src/storage/sovranStorage.ts +++ b/packages/core/src/storage/sovranStorage.ts @@ -61,11 +61,31 @@ export class SovranStorage implements Storage { this.userInfoStore = createStore( { userInfo: INITIAL_VALUES.userInfo }, { - persist: { storeId: `${this.storeId}-userInfo` }, + persist: { + storeId: `${this.storeId}-userInfo`, + }, } ); + + this.fixAnonymousId(); } + /** + * This is a fix for users that have started the app with the anonymousId set to 'anonymousId' bug + */ + private fixAnonymousId = () => { + const fixUnsubscribe = this.userInfoStore.subscribe((store) => { + if (store.userInfo.anonymousId === 'anonymousId') { + this.userInfoStore.dispatch((state) => { + return { + userInfo: { ...state.userInfo, anonymousId: getUUID() }, + }; + }); + } + fixUnsubscribe(); + }); + }; + readonly isReady = { get: () => true, onChange: (_callback: (value: boolean) => void) => { @@ -119,6 +139,7 @@ export class SovranStorage implements Storage { }); }, }; + readonly userInfo = { get: () => this.userInfoStore.getState().userInfo, onChange: (callback: (value: UserInfoState) => void) =>