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 8 commits into
base: main
Choose a base branch
from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typewise, tokens.username, tokens.accessToken and tokens.clockDrift are non optional types so technically no need for the if...else on line 100, 110, and 165 since also in the before changes they were just being set.

Maybe we can remove the if...else from those ones?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the extra if/else statements.

Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ export class DefaultTokenStore implements AuthTokenStore {

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should assert that in the absence of this all keys in https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/tokenProvider/types.ts#L27 are cleared if unspecified.


const lastAuthUser = tokens.username;
await this.getKeyValueStorage().setItem(
this.getLastAuthUserKey(),
lastAuthUser,
);

const authKeys = await this.getAuthKeys();
await this.getKeyValueStorage().setItem(
authKeys.accessToken,
Expand All @@ -113,13 +113,17 @@ export class DefaultTokenStore implements AuthTokenStore {
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 @@ -146,6 +150,8 @@ export class DefaultTokenStore implements AuthTokenStore {
authKeys.signInDetails,
JSON.stringify(tokens.signInDetails),
);
} else {
await this.getKeyValueStorage().removeItem(authKeys.signInDetails);
}

await this.getKeyValueStorage().setItem(
Expand Down