forgetDevice method
Forgets the current device.
For more information about device tracking, see the Amplify docs.
Examples
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
Future<void> forgetCurrentDevice() async {
try {
await Amplify.Auth.forgetDevice();
safePrint('Forget device succeeded');
} on AuthException catch (e) {
safePrint('Forget device failed with error: $e');
}
}
Optionally, to forget a specific device, i.e. one retrieved from fetchDevices, pass the AuthDevice when making the API call.
Future<void> forgetSpecificDevice(AuthDevice myDevice) async {
try {
await Amplify.Auth.forgetDevice(myDevice);
safePrint('Forget device succeeded');
} on AuthException catch (e) {
safePrint('Forget device failed with error: $e');
}
}
Implementation
@override
Future<void> forgetDevice([AuthDevice? device]) async {
final tokens = await stateMachine.getUserPoolTokens();
final username = tokens.username;
final deviceSecrets = await _deviceRepo.get(username);
final deviceKey = device?.id ?? deviceSecrets?.deviceKey;
if (deviceKey == null) {
throw const DeviceNotTrackedException();
}
if (device == null || device.id == deviceSecrets?.deviceKey) {
await _deviceRepo.remove(username);
}
await _cognitoIdp
.forgetDevice(
cognito.ForgetDeviceRequest(
accessToken: tokens.accessToken.raw,
deviceKey: deviceKey,
),
)
.result;
}