Skip to content

Feat: fetchCurrentDevice API Implementation #5075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'package:amplify_auth_cognito_dart/src/sdk/cognito_identity_provider.dart
ForgotPasswordRequest,
GetUserAttributeVerificationCodeRequest,
GetUserRequest,
GetDeviceRequest,
ListDevicesRequest,
ResendConfirmationCodeRequest,
UserContextDataType,
Expand All @@ -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';
Expand Down Expand Up @@ -97,6 +99,7 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
late CognitoAuthStateMachine _stateMachine = CognitoAuthStateMachine(
dependencyManager: dependencies,
);

StreamSubscription<AuthState>? _stateMachineSubscription;

/// The underlying state machine, for use in subclasses.
Expand Down Expand Up @@ -993,6 +996,46 @@ class AmplifyAuthCognitoDart extends AuthPluginInterface
.result;
}

@override
Future<CognitoDevice> 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 response;

try {
response = await _cognitoIdp
.getDevice(
cognito.GetDeviceRequest(
deviceKey: deviceKey,
accessToken: tokens.accessToken.raw,
),
)
.result;
} on Exception catch (error) {
throw AuthException.fromException(error);
}

final device = response.device;
final attributes =
device.deviceAttributes ?? const <cognito.AttributeType>[];

return CognitoDevice(
id: deviceKey,
attributes: {
for (final attribute in attributes)
attribute.name: attribute.value ?? '',
},
Comment on lines +1029 to +1032
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Does this need a for loop? Does attributes have other properties?

Suggested change
attributes: {
for (final attribute in attributes)
attribute.name: attribute.value ?? '',
},
attributes: attributes,

Copy link
Contributor Author

@hahnandrew hahnandrew Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Does this need a for loop?

Yes, it's for type conversion. At first glance it looks redundant, but the loop achieves two important things - converting null values to an empty string, and converting a BuiltList to Map

Note that attributes: attributes is type invalid. CognitoDevice types its attributes as map<string, string>, while the attributes from the getDeviceResponse.device (smithy-generatively) types its attributes as BuiltList<AttributeType>?.

Does attributes have other properties?

No

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks for adding that context.

createdDate: device.deviceCreateDate,
lastAuthenticatedDate: device.deviceLastAuthenticatedDate,
lastModifiedDate: device.deviceLastModifiedDate,
);
}

@override
Future<List<CognitoDevice>> fetchDevices() async {
final allDevices = <CognitoDevice>[];
Expand Down
Loading