Skip to content

Commit 37aeb81

Browse files
authored
fix: reset anonymousId for users who have persisted incorrect value (#445)
1 parent 26c8263 commit 37aeb81

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

packages/core/src/storage/sovranStorage.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,31 @@ export class SovranStorage implements Storage {
6161
this.userInfoStore = createStore(
6262
{ userInfo: INITIAL_VALUES.userInfo },
6363
{
64-
persist: { storeId: `${this.storeId}-userInfo` },
64+
persist: {
65+
storeId: `${this.storeId}-userInfo`,
66+
},
6567
}
6668
);
69+
70+
this.fixAnonymousId();
6771
}
6872

73+
/**
74+
* This is a fix for users that have started the app with the anonymousId set to 'anonymousId' bug
75+
*/
76+
private fixAnonymousId = () => {
77+
const fixUnsubscribe = this.userInfoStore.subscribe((store) => {
78+
if (store.userInfo.anonymousId === 'anonymousId') {
79+
this.userInfoStore.dispatch((state) => {
80+
return {
81+
userInfo: { ...state.userInfo, anonymousId: getUUID() },
82+
};
83+
});
84+
}
85+
fixUnsubscribe();
86+
});
87+
};
88+
6989
readonly isReady = {
7090
get: () => true,
7191
onChange: (_callback: (value: boolean) => void) => {
@@ -119,6 +139,7 @@ export class SovranStorage implements Storage {
119139
});
120140
},
121141
};
142+
122143
readonly userInfo = {
123144
get: () => this.userInfoStore.getState().userInfo,
124145
onChange: (callback: (value: UserInfoState) => void) =>

0 commit comments

Comments
 (0)