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: handle case where account entities is empty in after cache access #7329

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
47 changes: 28 additions & 19 deletions lib/msal-node/src/cache/distributed/DistributedCachePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,34 @@ export class DistributedCachePlugin implements ICachePlugin {
public async afterCacheAccess(
cacheContext: TokenCacheContext
): Promise<void> {
if (cacheContext.cacheHasChanged) {
const kvStore = (
cacheContext.tokenCache as TokenCache
).getKVStore();
const accountEntities = Object.values(kvStore).filter((value) =>
AccountEntity.isAccountEntity(value as object)
);

if (accountEntities.length > 0) {
const accountEntity = accountEntities[0] as AccountEntity;
const partitionKey = await this.partitionManager.extractKey(
accountEntity
);

await this.client.set(
partitionKey,
cacheContext.tokenCache.serialize()
);
}
const { cacheHasChanged, tokenCache } = cacheContext;

if (!cacheHasChanged) return;

const kvStore = (tokenCache as TokenCache).getKVStore();

const accountEntities = Object.values(kvStore).filter((value) =>
AccountEntity.isAccountEntity(value as object)
);

const partitionKey = await this.getPartitionKey(
accountEntities as AccountEntity[]
);

if (partitionKey) {
const serializedTokenCache = tokenCache.serialize();

await this.client.set(partitionKey, serializedTokenCache);
}
}

private async getPartitionKey(
accountEntities: AccountEntity[]
): Promise<string> {
if (accountEntities.length) {
return this.partitionManager.extractKey(accountEntities[0]);
}

return this.partitionManager.getKey();
}
}
Loading