Skip to content

Commit

Permalink
fix(auth): SessionExpired Auth Hub event (#2609)
Browse files Browse the repository at this point in the history
* fix(auth): SessionExpired Auth Hub event

* chore: add todo comment
  • Loading branch information
Jordan-Nelson committed Jan 30, 2023
1 parent cf9520c commit f49134a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,16 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface<
if (state is SignInSuccess) {
hubEvent = AuthHubEvent.signedIn(state.user.authUser);
}
if (state is FetchAuthSessionFailure &&
(state.exception is UnauthorizedException ||
state.exception is AuthNotAuthorizedException)) {
hubEvent = AuthHubEvent.sessionExpired();
if (state is FetchAuthSessionSuccess) {
final exception = state.session.userPoolTokensResult.exception;
// TODO(Jordan-Nelson): Update list of exceptions once FetchAuthSession
/// is updated to only throw SessionExpiredException for expired
/// sessions.
if (exception is UnauthorizedException ||
exception is AuthNotAuthorizedException ||
exception is SessionExpiredException) {
hubEvent = AuthHubEvent.sessionExpired();
}
}

if (hubEvent != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart';
import 'package:amplify_core/amplify_core.dart';
import 'package:test/test.dart';

import '../common/jwt.dart';
import '../common/mock_clients.dart';
import '../common/mock_config.dart';
import '../common/mock_secure_storage.dart';

void main() {
AmplifyLogger().logLevel = LogLevel.verbose;

late CognitoAuthStateMachine stateMachine;
late AmplifyAuthCognitoDart plugin;

group('fetchAuthSession', () {
group('when session is expired', () {
setUp(() async {
final expiredIdToken = createJwt(
type: TokenType.id,
expiration: Duration.zero,
);
final secureStorage = MockSecureStorage();
seedStorage(
secureStorage,
identityPoolKeys: identityPoolKeys,
userPoolKeys: userPoolKeys,
);
secureStorage.write(
key: userPoolKeys[CognitoUserPoolKey.idToken],
value: expiredIdToken.raw,
);
stateMachine = CognitoAuthStateMachine();
plugin = AmplifyAuthCognitoDart(credentialStorage: secureStorage)
..stateMachine = stateMachine;
await plugin.configure(
config: mockConfig,
authProviderRepo: AmplifyAuthProviderRepository(),
);

stateMachine.addInstance<CognitoIdentityProviderClient>(
MockCognitoIdentityProviderClient(
initiateAuth: expectAsync0(
() async => throw const AuthNotAuthorizedException(
'Refresh Token has expired.',
),
),
),
);
});

test('should add a sessionExpired event to Auth Hub', () async {
final authStream = Amplify.Hub.availableStreams[HubChannel.Auth];
plugin.fetchAuthSession().ignore();
await expectLater(authStream, emits(AuthHubEvent.sessionExpired()));
});
});

tearDown(() async {
await plugin.close();
});
});
}

0 comments on commit f49134a

Please sign in to comment.