Skip to content

Commit

Permalink
fix(auth): clear out identityId as well when clearing credentials for…
Browse files Browse the repository at this point in the history
… sign out (aws-amplify#11908)

* fix: signOut test faiures
  • Loading branch information
Samaritan1011001 authored Aug 29, 2023
1 parent b06ce0d commit 6fd4a4f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/auth/__tests__/providers/cognito/signOut.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('signOut tests no oauth request fail', () => {
Auth: {
tokenProvider: TokenProvider.CognitoUserPoolsTokenProvider,
credentialsProvider: {
clearCredentials() {
clearCredentialsAndIdentityId() {
clearCredentialsSpy();
},
getCredentialsAndIdentityId(getCredentialsOptions) {
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('signOut tests with oauth', () => {
Auth: {
tokenProvider: TokenProvider.CognitoUserPoolsTokenProvider,
credentialsProvider: {
clearCredentials() {
clearCredentialsAndIdentityId() {
clearCredentialsSpy();
},
getCredentialsAndIdentityId(getCredentialsOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ export class CognitoAWSCredentialsAndIdentityIdProvider
}

// TODO(V6): export clear crecentials to singleton
async clearCredentialsAndIdentityId(): Promise<void> {
logger.debug('Clearing out credentials and identityId');
this._credentialsAndIdentityId = undefined;
await this._identityIdStore.clearIdentityId();
}

async clearCredentials(): Promise<void> {
logger.debug('Clearing out credentials');
logger.debug('Clearing out in-memory credentials');
this._credentialsAndIdentityId = undefined;
}

Expand Down Expand Up @@ -71,20 +77,7 @@ export class CognitoAWSCredentialsAndIdentityIdProvider
this.clearCredentials();
}

// check eligibility for guest credentials
// - if there is error fetching tokens
// - if user is not signed in
if (!isAuthenticated) {
// Check if mandatory sign-in is enabled
if (authConfig.Cognito.allowGuestAccess) {
// TODO(V6): confirm if this needs to throw or log
throw new AuthError({
name: 'AuthConfigException',
message:
'Cannot get guest credentials when mandatory signin is enabled',
recoverySuggestion: 'Make sure mandatory signin is disabled.',
});
}
return await this.getGuestCredentials(identityId, authConfig);
} else {
// Tokens will always be present if getCredentialsOptions.authenticated is true as dictated by the type
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/singleton/Auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class AuthClass {

async clearCredentials(): Promise<void> {
if (this.authOptions?.credentialsProvider) {
return await this.authOptions.credentialsProvider.clearCredentials();
return await this.authOptions.credentialsProvider.clearCredentialsAndIdentityId();
}
}
}
2 changes: 1 addition & 1 deletion packages/core/src/singleton/Auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface AWSCredentialsAndIdentityIdProvider {
getCredentialsAndIdentityId: (
getCredentialsOptions: GetCredentialsOptions
) => Promise<AWSCredentialsAndIdentityId>;
clearCredentials: () => void;
clearCredentialsAndIdentityId: () => void;
}

export type TokenProvider = {
Expand Down

0 comments on commit 6fd4a4f

Please sign in to comment.