Skip to content

Commit 45202d0

Browse files
authored
Merge pull request #5075 from aws-amplify/feat/fetchCurrentDevice-impl
Feat: fetchCurrentDevice API Implementation
2 parents 0f43d6c + 7bda0c3 commit 45202d0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart
3131
ForgotPasswordRequest,
3232
GetUserAttributeVerificationCodeRequest,
3333
GetUserRequest,
34+
GetDeviceRequest,
3435
ListDevicesRequest,
3536
ResendConfirmationCodeRequest,
3637
UserContextDataType,
@@ -39,6 +40,7 @@ import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart
3940
VerifyUserAttributeRequest;
4041
import 'package:amplify_auth_cognito_dart/src/sdk/sdk_bridge.dart';
4142
import 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/analytics_metadata_type.dart';
43+
import 'package:amplify_auth_cognito_dart/src/sdk/src/cognito_identity_provider/model/get_device_response.dart';
4244
import 'package:amplify_auth_cognito_dart/src/state/cognito_state_machine.dart';
4345
import 'package:amplify_auth_cognito_dart/src/state/state.dart';
4446
import 'package:amplify_auth_cognito_dart/src/util/cognito_iam_auth_provider.dart';
@@ -97,6 +99,7 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
9799
late CognitoAuthStateMachine _stateMachine = CognitoAuthStateMachine(
98100
dependencyManager: dependencies,
99101
);
102+
100103
StreamSubscription<AuthState>? _stateMachineSubscription;
101104

102105
/// The underlying state machine, for use in subclasses.
@@ -993,6 +996,46 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
993996
.result;
994997
}
995998

999+
@override
1000+
Future<CognitoDevice> fetchCurrentDevice() async {
1001+
final tokens = await stateMachine.getUserPoolTokens();
1002+
final deviceSecrets = await _deviceRepo.get(tokens.username);
1003+
final deviceKey = deviceSecrets?.deviceKey;
1004+
if (deviceSecrets == null || deviceKey == null) {
1005+
throw const DeviceNotTrackedException();
1006+
}
1007+
1008+
late GetDeviceResponse response;
1009+
1010+
try {
1011+
response = await _cognitoIdp
1012+
.getDevice(
1013+
cognito.GetDeviceRequest(
1014+
deviceKey: deviceKey,
1015+
accessToken: tokens.accessToken.raw,
1016+
),
1017+
)
1018+
.result;
1019+
} on Exception catch (error) {
1020+
throw AuthException.fromException(error);
1021+
}
1022+
1023+
final device = response.device;
1024+
final attributes =
1025+
device.deviceAttributes ?? const <cognito.AttributeType>[];
1026+
1027+
return CognitoDevice(
1028+
id: deviceKey,
1029+
attributes: {
1030+
for (final attribute in attributes)
1031+
attribute.name: attribute.value ?? '',
1032+
},
1033+
createdDate: device.deviceCreateDate,
1034+
lastAuthenticatedDate: device.deviceLastAuthenticatedDate,
1035+
lastModifiedDate: device.deviceLastModifiedDate,
1036+
);
1037+
}
1038+
9961039
@override
9971040
Future<List<CognitoDevice>> fetchDevices() async {
9981041
final allDevices = <CognitoDevice>[];

0 commit comments

Comments
 (0)