Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reset anonymousId for users who have persisted incorrect value #445

Merged
merged 1 commit into from
Feb 17, 2022
Merged
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
23 changes: 22 additions & 1 deletion packages/core/src/storage/sovranStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -119,6 +139,7 @@ export class SovranStorage implements Storage {
});
},
};

readonly userInfo = {
get: () => this.userInfoStore.getState().userInfo,
onChange: (callback: (value: UserInfoState) => void) =>
Expand Down