Skip to content

Commit f49134a

Browse files
fix(auth): SessionExpired Auth Hub event (#2609)
* fix(auth): SessionExpired Auth Hub event * chore: add todo comment
1 parent cf9520c commit f49134a

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,16 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface<
221221
if (state is SignInSuccess) {
222222
hubEvent = AuthHubEvent.signedIn(state.user.authUser);
223223
}
224-
if (state is FetchAuthSessionFailure &&
225-
(state.exception is UnauthorizedException ||
226-
state.exception is AuthNotAuthorizedException)) {
227-
hubEvent = AuthHubEvent.sessionExpired();
224+
if (state is FetchAuthSessionSuccess) {
225+
final exception = state.session.userPoolTokensResult.exception;
226+
// TODO(Jordan-Nelson): Update list of exceptions once FetchAuthSession
227+
/// is updated to only throw SessionExpiredException for expired
228+
/// sessions.
229+
if (exception is UnauthorizedException ||
230+
exception is AuthNotAuthorizedException ||
231+
exception is SessionExpiredException) {
232+
hubEvent = AuthHubEvent.sessionExpired();
233+
}
228234
}
229235

230236
if (hubEvent != null) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import 'package:amplify_auth_cognito_dart/amplify_auth_cognito_dart.dart';
5+
import 'package:amplify_auth_cognito_dart/src/credentials/cognito_keys.dart';
6+
import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart';
7+
import 'package:amplify_core/amplify_core.dart';
8+
import 'package:test/test.dart';
9+
10+
import '../common/jwt.dart';
11+
import '../common/mock_clients.dart';
12+
import '../common/mock_config.dart';
13+
import '../common/mock_secure_storage.dart';
14+
15+
void main() {
16+
AmplifyLogger().logLevel = LogLevel.verbose;
17+
18+
late CognitoAuthStateMachine stateMachine;
19+
late AmplifyAuthCognitoDart plugin;
20+
21+
group('fetchAuthSession', () {
22+
group('when session is expired', () {
23+
setUp(() async {
24+
final expiredIdToken = createJwt(
25+
type: TokenType.id,
26+
expiration: Duration.zero,
27+
);
28+
final secureStorage = MockSecureStorage();
29+
seedStorage(
30+
secureStorage,
31+
identityPoolKeys: identityPoolKeys,
32+
userPoolKeys: userPoolKeys,
33+
);
34+
secureStorage.write(
35+
key: userPoolKeys[CognitoUserPoolKey.idToken],
36+
value: expiredIdToken.raw,
37+
);
38+
stateMachine = CognitoAuthStateMachine();
39+
plugin = AmplifyAuthCognitoDart(credentialStorage: secureStorage)
40+
..stateMachine = stateMachine;
41+
await plugin.configure(
42+
config: mockConfig,
43+
authProviderRepo: AmplifyAuthProviderRepository(),
44+
);
45+
46+
stateMachine.addInstance<CognitoIdentityProviderClient>(
47+
MockCognitoIdentityProviderClient(
48+
initiateAuth: expectAsync0(
49+
() async => throw const AuthNotAuthorizedException(
50+
'Refresh Token has expired.',
51+
),
52+
),
53+
),
54+
);
55+
});
56+
57+
test('should add a sessionExpired event to Auth Hub', () async {
58+
final authStream = Amplify.Hub.availableStreams[HubChannel.Auth];
59+
plugin.fetchAuthSession().ignore();
60+
await expectLater(authStream, emits(AuthHubEvent.sessionExpired()));
61+
});
62+
});
63+
64+
tearDown(() async {
65+
await plugin.close();
66+
});
67+
});
68+
}

0 commit comments

Comments
 (0)