From 23496550cc7ce5e599398ef3821e92fbc6692553 Mon Sep 17 00:00:00 2001 From: Andrew Hahn Date: Tue, 25 Jun 2024 12:32:48 -0700 Subject: [PATCH] feat: added class interfaces for fetchCurrentDevices --- .../src/category/amplify_auth_category.dart | 31 +++++++++++++++++++ .../lib/src/http/amplify_category_method.dart | 3 +- .../plugin/amplify_auth_plugin_interface.dart | 5 +++ .../example/lib/common.dart | 4 +++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/amplify_core/lib/src/category/amplify_auth_category.dart b/packages/amplify_core/lib/src/category/amplify_auth_category.dart index 4a23528d70..30b44797aa 100644 --- a/packages/amplify_core/lib/src/category/amplify_auth_category.dart +++ b/packages/amplify_core/lib/src/category/amplify_auth_category.dart @@ -1355,6 +1355,37 @@ class AuthCategory extends AmplifyCategory { () => defaultPlugin.rememberDevice(), ); + /// {@template amplify_core.amplify_auth_category.fetch_current_device} + /// Retrieves the current device. + /// + /// For more information about device tracking, see the + /// [Amplify docs](https://docs.amplify.aws/lib/auth/device_features/q/platform/flutter/). + /// + /// ## Examples + /// + /// + /// ```dart + /// import 'package:amplify_auth_cognito/amplify_auth_cognito.dart'; + /// import 'package:amplify_flutter/amplify_flutter.dart'; + /// ``` + /// + /// + /// ```dart + /// Future getCurrentUserDevice() async { + /// try { + /// final device = await Amplify.Auth.fetchCurrentDevice(); + /// safePrint('Device: $device'); + /// } on AuthException catch (e) { + /// safePrint('Get current device failed with error: $e'); + /// } + /// } + /// ``` + /// {@endtemplate} + Future fetchCurrentDevice() => identifyCall( + AuthCategoryMethod.fetchCurrentDevice, + () => defaultPlugin.fetchCurrentDevice(), + ); + /// {@template amplify_core.amplify_auth_category.forget_device} /// Forgets the current device. /// diff --git a/packages/amplify_core/lib/src/http/amplify_category_method.dart b/packages/amplify_core/lib/src/http/amplify_category_method.dart index a0c978594b..97ee62251e 100644 --- a/packages/amplify_core/lib/src/http/amplify_category_method.dart +++ b/packages/amplify_core/lib/src/http/amplify_category_method.dart @@ -52,7 +52,8 @@ enum AuthCategoryMethod with AmplifyCategoryMethod { setMfaPreference('49'), getMfaPreference('50'), setUpTotp('51'), - verifyTotpSetup('52'); + verifyTotpSetup('52'), + fetchCurrentDevice('59'); const AuthCategoryMethod(this.method); diff --git a/packages/amplify_core/lib/src/plugin/amplify_auth_plugin_interface.dart b/packages/amplify_core/lib/src/plugin/amplify_auth_plugin_interface.dart index 43e74853f3..80521de18d 100644 --- a/packages/amplify_core/lib/src/plugin/amplify_auth_plugin_interface.dart +++ b/packages/amplify_core/lib/src/plugin/amplify_auth_plugin_interface.dart @@ -189,6 +189,11 @@ abstract class AuthPluginInterface extends AmplifyPluginInterface { throw UnimplementedError('forgetDevice() has not been implemented.'); } + /// {@macro amplify_core.amplify_auth_category.fetch_current_device} + Future fetchCurrentDevice() { + throw UnimplementedError('fetchCurrentDevice() has not been implemented.'); + } + /// {@macro amplify_core.amplify_auth_category.fetch_devices} Future> fetchDevices() { throw UnimplementedError('fetchDevices() has not been implemented.'); diff --git a/packages/auth/amplify_auth_cognito_dart/example/lib/common.dart b/packages/auth/amplify_auth_cognito_dart/example/lib/common.dart index 42347c4c90..b266f9ee7d 100644 --- a/packages/auth/amplify_auth_cognito_dart/example/lib/common.dart +++ b/packages/auth/amplify_auth_cognito_dart/example/lib/common.dart @@ -104,6 +104,10 @@ Future> fetchUserAttributes() async { return Amplify.Auth.fetchUserAttributes(); } +Future fetchCurrentDevice() async { + return Amplify.Auth.fetchCurrentDevice(); +} + Future> fetchDevices() async { return Amplify.Auth.fetchDevices(); }