From a286dbf68eb825a8f48d090168abc6bb20367662 Mon Sep 17 00:00:00 2001 From: Andrew Hahn Date: Tue, 25 Jun 2024 15:40:55 -0700 Subject: [PATCH 1/2] feat: implemented fetchCurrentDevice API --- .../lib/src/auth_plugin_impl.dart | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart b/packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart index 608fd09bf0..484d1a8b99 100644 --- a/packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart +++ b/packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart @@ -31,6 +31,7 @@ import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart ForgotPasswordRequest, GetUserAttributeVerificationCodeRequest, GetUserRequest, + GetDeviceRequest, ListDevicesRequest, ResendConfirmationCodeRequest, UserContextDataType, @@ -39,6 +40,7 @@ import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart VerifyUserAttributeRequest; import 'package:amplify_auth_cognito_dart/src/sdk/sdk_bridge.dart'; import 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/analytics_metadata_type.dart'; +import 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/get_device_response.dart'; import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart'; import 'package:amplify_auth_cognito_dart/src/state/state.dart'; import 'package:amplify_auth_cognito_dart/src/util/cognito_iam_auth_provider.dart'; @@ -97,6 +99,7 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface late CognitoAuthStateMachine _stateMachine = CognitoAuthStateMachine( dependencyManager: dependencies, ); + StreamSubscription? _stateMachineSubscription; /// The underlying state machine, for use in subclasses. @@ -993,6 +996,46 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface .result; } + @override + Future fetchCurrentDevice() async { + final tokens = await stateMachine.getUserPoolTokens(); + final deviceSecrets = await _deviceRepo.get(tokens.username); + final deviceKey = deviceSecrets?.deviceKey; + if (deviceSecrets == null || deviceKey == null) { + throw const DeviceNotTrackedException(); + } + + late GetDeviceResponse resp; + + try { + resp = await _cognitoIdp + .getDevice( + cognito.GetDeviceRequest( + deviceKey: deviceKey, + accessToken: tokens.accessToken.raw, + ), + ) + .result; + } on Exception catch (error) { + throw AuthException.fromException(error); + } + + final device = resp.device; + final attributes = + device.deviceAttributes ?? const []; + + return CognitoDevice( + id: deviceKey, + attributes: { + for (final attribute in attributes) + attribute.name: attribute.value ?? '', + }, + createdDate: device.deviceCreateDate, + lastAuthenticatedDate: device.deviceLastAuthenticatedDate, + lastModifiedDate: device.deviceLastModifiedDate, + ); + } + @override Future> fetchDevices() async { final allDevices = []; From 7bda0c3d02426b295bd2ea408ac912b57d0bc41f Mon Sep 17 00:00:00 2001 From: Andrew Hahn Date: Thu, 27 Jun 2024 16:21:11 -0700 Subject: [PATCH 2/2] fix: variable name change for clarity --- .../amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart b/packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart index 484d1a8b99..2c2c788f5a 100644 --- a/packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart +++ b/packages/auth/amplify_auth_cognito_dart/lib/src/auth_plugin_impl.dart @@ -1005,10 +1005,10 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface throw const DeviceNotTrackedException(); } - late GetDeviceResponse resp; + late GetDeviceResponse response; try { - resp = await _cognitoIdp + response = await _cognitoIdp .getDevice( cognito.GetDeviceRequest( deviceKey: deviceKey, @@ -1020,7 +1020,7 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface throw AuthException.fromException(error); } - final device = resp.device; + final device = response.device; final attributes = device.deviceAttributes ?? const [];