Skip to content

Commit

Permalink
fix: missing await for removing user data at logout
Browse files Browse the repository at this point in the history
When logging out, we need to wait for the async removal
of user data before emitting the new state to the consumer.

This also makes it such that alternateUsers is always `null`
instead of an empty object if there are no alternate users.
  • Loading branch information
jackson-dean committed May 23, 2023
1 parent 60ef702 commit 1984c35
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions src/identity/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,54 +158,50 @@ export class Identity<T extends StorageProvider> {
*/
#getState(): T extends Storage ? IdentityState : Promise<IdentityState> {
const users = this.#getUsers();
const constructState = (
users: Record<string, StoredUser> | null,
activePublicKey: string | null
) => {
const currentUser =
activePublicKey && users && (users[activePublicKey] ?? null);
const alternateUsers =
users &&
Object.keys(users).reduce<Record<string, StoredUser>>(
(res, publicKey) => {
if (publicKey !== activePublicKey) {
res[publicKey] = (users as Record<string, StoredUser>)?.[
publicKey
];
}
return res;
},
{}
);

if (typeof users?.then === 'function') {
// we're in async mode
return Promise.all([
users,
this.#getActivePublicKey(),
this.#getCurrentUser(),
]).then(([allStoredUsers, activePublicKey, currentUser]) => ({
return {
currentUser: currentUser && {
...currentUser,
publicKey: currentUser.primaryDerivedKey.publicKeyBase58Check,
},
alternateUsers:
allStoredUsers &&
Object.keys(allStoredUsers).reduce<Record<string, StoredUser>>(
(res, publicKey) => {
if (publicKey !== activePublicKey) {
res[publicKey] = allStoredUsers[publicKey];
}
return res;
},
{}
),
})) as any;
alternateUsers: Object.keys(alternateUsers ?? {})?.length
? alternateUsers
: null,
};
};

if (typeof users?.then === 'function') {
// we're in async mode
return Promise.all([users, this.#getActivePublicKey()]).then((args) =>
constructState(...args)
) as T extends Storage ? IdentityState : Promise<IdentityState>;
} else {
// we're in sync mode
const activePublicKey = this.#getActivePublicKey() as string | null;
const currentUser = this.#getCurrentUser() as StoredUser | null;

return {
currentUser: currentUser && {
...currentUser,
publicKey: currentUser.primaryDerivedKey.publicKeyBase58Check,
},
alternateUsers:
users &&
Object.keys(users).reduce<Record<string, StoredUser>>(
(res, publicKey) => {
if (publicKey !== activePublicKey) {
res[publicKey] = (users as Record<string, StoredUser>)?.[
publicKey
];
}
return res;
},
{}
),
} as any;
return constructState(
users as Record<string, StoredUser> | null,
activePublicKey
) as T extends Storage ? IdentityState : Promise<IdentityState>;
}
}

Expand Down Expand Up @@ -1600,7 +1596,8 @@ export class Identity<T extends StorageProvider> {
await this.#storageProvider.removeItem(
LOCAL_STORAGE_KEYS.activePublicKey
);
this.#purgeUserDataForPublicKey(activePublicKey);

await this.#purgeUserDataForPublicKey(activePublicKey);

const state = await this.#getState();
this.#subscriber?.({
Expand Down

0 comments on commit 1984c35

Please sign in to comment.