-
Notifications
You must be signed in to change notification settings - Fork 260
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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<AuthState>? _stateMachineSubscription; | ||||||||||||
|
||||||||||||
/// The underlying state machine, for use in subclasses. | ||||||||||||
|
@@ -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 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 <cognito.AttributeType>[]; | ||||||||||||
|
||||||||||||
return CognitoDevice( | ||||||||||||
id: deviceKey, | ||||||||||||
attributes: { | ||||||||||||
for (final attribute in attributes) | ||||||||||||
attribute.name: attribute.value ?? '', | ||||||||||||
}, | ||||||||||||
Comment on lines
+1029
to
+1032
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: Does this need a for loop? Does
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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 Note that
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>[]; | ||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.