Skip to content

Commit aa42851

Browse files
authored
Merge pull request #5070 from aws-amplify/feat/fetchCurrentDevice-interfaces
Feat (Auth): Interface Implementation for fetchCurrentDevice
2 parents fb001b4 + 045b5b7 commit aa42851

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

packages/amplify_core/doc/lib/auth.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,17 @@ Future<void> forgetSpecificDevice(AuthDevice myDevice) async {
483483
}
484484
// #enddocregion forget-specific-device
485485

486+
// #docregion fetch-current-device
487+
Future<void> fetchCurrentUserDevice() async {
488+
try {
489+
final device = await Amplify.Auth.fetchCurrentDevice();
490+
safePrint('Device: $device');
491+
} on AuthException catch (e) {
492+
safePrint('Fetch current device failed with error: $e');
493+
}
494+
}
495+
// #enddocregion fetch-current-device
496+
486497
// #docregion fetch-devices
487498
Future<void> fetchAllDevices() async {
488499
try {

packages/amplify_core/lib/src/category/amplify_auth_category.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,37 @@ class AuthCategory extends AmplifyCategory<AuthPluginInterface> {
13551355
() => defaultPlugin.rememberDevice(),
13561356
);
13571357

1358+
/// {@template amplify_core.amplify_auth_category.fetch_current_device}
1359+
/// Retrieves the current device.
1360+
///
1361+
/// For more information about device tracking, see the
1362+
/// [Amplify docs](https://docs.amplify.aws/flutter/build-a-backend/auth/manage-users/manage-devices/#fetch-current-device).
1363+
///
1364+
/// ## Examples
1365+
///
1366+
/// <?code-excerpt "doc/lib/auth.dart" region="imports"?>
1367+
/// ```dart
1368+
/// import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
1369+
/// import 'package:amplify_flutter/amplify_flutter.dart';
1370+
/// ```
1371+
///
1372+
/// <?code-excerpt "doc/lib/auth.dart" region="fetch-current-device"?>
1373+
/// ```dart
1374+
/// Future<AuthDevice> getCurrentUserDevice() async {
1375+
/// try {
1376+
/// final device = await Amplify.Auth.fetchCurrentDevice();
1377+
/// safePrint('Device: $device');
1378+
/// } on AuthException catch (e) {
1379+
/// safePrint('Fetch current device failed with error: $e');
1380+
/// }
1381+
/// }
1382+
/// ```
1383+
/// {@endtemplate}
1384+
Future<AuthDevice> fetchCurrentDevice() => identifyCall(
1385+
AuthCategoryMethod.fetchCurrentDevice,
1386+
() => defaultPlugin.fetchCurrentDevice(),
1387+
);
1388+
13581389
/// {@template amplify_core.amplify_auth_category.forget_device}
13591390
/// Forgets the current device.
13601391
///

packages/amplify_core/lib/src/http/amplify_category_method.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ enum AuthCategoryMethod with AmplifyCategoryMethod {
5252
setMfaPreference('49'),
5353
getMfaPreference('50'),
5454
setUpTotp('51'),
55-
verifyTotpSetup('52');
55+
verifyTotpSetup('52'),
56+
fetchCurrentDevice('59');
5657

5758
const AuthCategoryMethod(this.method);
5859

packages/amplify_core/lib/src/plugin/amplify_auth_plugin_interface.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ abstract class AuthPluginInterface extends AmplifyPluginInterface {
189189
throw UnimplementedError('forgetDevice() has not been implemented.');
190190
}
191191

192+
/// {@macro amplify_core.amplify_auth_category.fetch_current_device}
193+
Future<AuthDevice> fetchCurrentDevice() {
194+
throw UnimplementedError('fetchCurrentDevice() has not been implemented.');
195+
}
196+
192197
/// {@macro amplify_core.amplify_auth_category.fetch_devices}
193198
Future<List<AuthDevice>> fetchDevices() {
194199
throw UnimplementedError('fetchDevices() has not been implemented.');

0 commit comments

Comments
 (0)