Manage devices
Amplify Auth enables you to track devices your users use for auditing, MFA, and more. Before you begin it is important to understand the terminology for device statuses:
- Tracked: Every time the user signs in with a new device, the client is given the device key at the end of a successful authentication event. We use this device key to generate a salt and password verifier which is used to call the ConfirmDevice API. At this point, the device is considered to be tracked. Once the device is in a tracked state, you can use the Amazon Cognito console to see the time it started to be tracked, last authentication time, and other information about that device.
- Remembered: Remembered devices are also tracked. During user authentication, the device key and secret pair assigned to a remembered device is used to authenticate the device to verify that it is the same device that the user previously used to sign in.
- Not Remembered: A not-remembered device is a tracked device where Cognito has been configured to require users to "Opt-in" to remember a device, but the user has not opt-ed in to having the device remembered. This use case is used for users signing into their application from a device that they don't own.
- Forgotten: a forgotten device is one removed from being remembered
Remember devices
You can remember devices using the following:
Future<void> rememberCurrentDevice() async { try { await Amplify.Auth.rememberDevice(); safePrint('Remember device succeeded'); } on AuthException catch (e) { safePrint('Remember device failed with error: $e'); }}
Forget devices
You can also forget devices but note that forgotten devices are neither remembered nor tracked.
Future<void> forgetCurrentDevice() async { try { await Amplify.Auth.forgetDevice(); safePrint('Forget device succeeded'); } on AuthException catch (e) { safePrint('Forget device failed with error: $e'); }}
// A device that was fetched via Amplify.Auth.fetchDevices()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'); }}
Fetch devices
You can fetch a list of remembered devices by using the following:
Future<void> fetchAllDevices() async { try { final devices = await Amplify.Auth.fetchDevices(); for (final device in devices) { safePrint('Device: $device'); } } on AuthException catch (e) { safePrint('Fetch devices failed with error: $e'); }}
Fetch the current device
You can fetch the current device by using the following:
Future<void> fetchCurrentUserDevice() async { try { final device = await Amplify.Auth.fetchCurrentDevice(); safePrint('Device: $device'); } on AuthException catch (e) { safePrint('Get current device failed with error: $e'); }}
You can now set up devices to be remembered, forgotten, and fetched.