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:
Amplify.Auth.rememberDevice( () -> Log.i("AuthQuickStart", "Remember device succeeded"), error -> Log.e("AuthQuickStart", "Remember device failed with error " + error.toString()));
Amplify.Auth.rememberDevice( { Log.i("AuthQuickStart", "Remember device succeeded") }, { Log.e("AuthQuickStart", "Remember device failed with error", it) })
try { Amplify.Auth.rememberDevice() Log.i("AuthQuickStart", "Remember device succeeded")} catch (error: AuthException) { Log.e("AuthQuickStart", "Remember device failed with error", error)}
RxAmplify.Auth.rememberDevice() .subscribe( () -> Log.i("AuthQuickStart", "Remember device succeeded"), error -> Log.e("AuthQuickStart", "Remember device failed with error " + error.toString()) );
Forget devices
You can also forget devices but note that forgotten devices are neither remembered nor tracked.
Amplify.Auth.forgetDevice( () -> Log.i("AuthQuickStart", "Forget device succeeded"), error -> Log.e("AuthQuickStart", "Forget device failed with error " + error.toString()));
Amplify.Auth.forgetDevice( { Log.i("AuthQuickStart", "Forget device succeeded") }, { Log.e("AuthQuickStart", "Forget device failed with error", it) })
try { Amplify.Auth.forgetDevice() Log.i("AuthQuickStart", "Forget device succeeded")} catch (error: AuthException) { Log.e("AuthQuickStart", "Forget device failed with error", error)}
RxAmplify.Auth.forgetDevice() .subscribe( () -> Log.i("AuthQuickStart", "Forget device succeeded"), error -> Log.e("AuthQuickStart", "Forget device failed with error " + error.toString()) );
Fetch devices
You can fetch a list of remembered devices by using the following:
Amplify.Auth.fetchDevices( devices -> { for (AuthDevice device : devices) { Log.i("AuthQuickStart", "Device: " + device); } }, error -> Log.e("AuthQuickStart", "Fetch devices failed with error: " + error.toString()));
Amplify.Auth.fetchDevices( { devices -> devices.forEach { Log.i("AuthQuickStart", "Device: " + it) } }, { Log.e("AuthQuickStart", "Fetch devices failed with error", it) })
try { Amplify.Auth.fetchDevices().forEach { device -> Log.i("AuthQuickStart", "Device: $device") }} catch (error: AuthException) { Log.e("AuthQuickStart", "Fetch devices failed with error", error)}
RxAmplify.Auth.fetchDevices() .subscribe( device -> Log.i("AuthQuickStart", "Device: " + device); error -> Log.e("AuthQuickStart", "Fetch devices failed with error: " + error.toString()) );
You can now set up devices to be remembered, forgotten, and fetched.