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

Setting Store Tokens should only clear tokens that no longer have a value #13584

Open
wants to merge 1 commit into
base: main
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
44 changes: 31 additions & 13 deletions packages/auth/src/providers/cognito/tokenProvider/TokenStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,43 @@ export class DefaultTokenStore implements AuthTokenStore {

async storeTokens(tokens: CognitoAuthTokens): Promise<void> {
assert(tokens !== undefined, TokenProviderErrorCode.InvalidAuthTokens);
await this.clearTokens();

const lastAuthUser = tokens.username;
await this.getKeyValueStorage().setItem(
this.getLastAuthUserKey(),
lastAuthUser,
);
if (lastAuthUser) {
await this.getKeyValueStorage().setItem(
this.getLastAuthUserKey(),
lastAuthUser,
);
} else {
await this.getKeyValueStorage().removeItem(this.getLastAuthUserKey());
}

const authKeys = await this.getAuthKeys();
await this.getKeyValueStorage().setItem(
authKeys.accessToken,
tokens.accessToken.toString(),
);
if (authKeys.accessToken) {
await this.getKeyValueStorage().setItem(
authKeys.accessToken,
tokens.accessToken.toString(),
);
} else {
await this.getKeyValueStorage().removeItem(authKeys.accessToken);
}

if (tokens.idToken) {
await this.getKeyValueStorage().setItem(
authKeys.idToken,
tokens.idToken.toString(),
);
} else {
await this.getKeyValueStorage().removeItem(authKeys.idToken);
}

if (tokens.refreshToken) {
await this.getKeyValueStorage().setItem(
authKeys.refreshToken,
tokens.refreshToken,
);
} else {
await this.getKeyValueStorage().removeItem(authKeys.refreshToken);
}

if (tokens.deviceMetadata) {
Expand All @@ -145,12 +157,18 @@ export class DefaultTokenStore implements AuthTokenStore {
authKeys.signInDetails,
JSON.stringify(tokens.signInDetails),
);
} else {
await this.getKeyValueStorage().removeItem(authKeys.signInDetails);
}

await this.getKeyValueStorage().setItem(
authKeys.clockDrift,
`${tokens.clockDrift}`,
);
if (authKeys.clockDrift) {
await this.getKeyValueStorage().setItem(
authKeys.clockDrift,
`${tokens.clockDrift}`,
);
} else {
await this.getKeyValueStorage().removeItem(authKeys.clockDrift);
}
}

async clearTokens(): Promise<void> {
Expand Down