Page updated Jan 16, 2024

Remember a device

The device tracking and remembering features are currently not available within the library when using the federated OAuth flow with Cognito User Pools or Hosted UI.

Remembering a device is useful in conjunction with Multi-Factor Authentication (MFA). If MFA is enabled for an Amazon Cognito user pool, end users have to type in a security code received via e-mail or SMS each time they want to sign in. This increases security but comes at the expense of the user's experience.

Remembering a device allows the second factor requirement to be automatically met when the user signs in on that device, thereby reducing friction in the user experience.

Configure Auth Category

To enable remembered device functionality, open the Cognito User Pool console. To do this, go to your project directory and issue the command:

amplify auth console
1amplify auth console

Select the following option to open the Cognito User Pool console:

? Which Console User Pool
1? Which Console
2 User Pool

When the console opens, scroll down to the Device Tracking section and select the Edit button. This will render the following page allowing you to configure your preference for remembering a user's device.

Edit device tracking options list is shown, with the don't remember highlighted.

Choose either Always remember or User Opt-in depending on whether you want to remember a user's device by default or give the user the ability to choose.

If MFA is enabled for the Cognito user pool, you will have the option to suppress the second factor during multi-factor authentication. Choose Yes if you want a remembered device to be used as a second factor mechanism or No otherwise.

Options for allow users to bypass MFA for trusted devices.

When you have made your selection(s), click "Save changes". You are now ready to start updating your code to manage your remembered devices.

APIs

Remember Device

You can mark your device as remembered:

Amplify.Auth.rememberDevice( () -> Log.i("AuthQuickStart", "Remember device succeeded"), error -> Log.e("AuthQuickStart", "Remember device failed with error " + error.toString()) );
1Amplify.Auth.rememberDevice(
2 () -> Log.i("AuthQuickStart", "Remember device succeeded"),
3 error -> Log.e("AuthQuickStart", "Remember device failed with error " + error.toString())
4);
Amplify.Auth.rememberDevice( { Log.i("AuthQuickStart", "Remember device succeeded") }, { Log.e("AuthQuickStart", "Remember device failed with error", it) } )
1Amplify.Auth.rememberDevice(
2 { Log.i("AuthQuickStart", "Remember device succeeded") },
3 { Log.e("AuthQuickStart", "Remember device failed with error", it) }
4)
try { Amplify.Auth.rememberDevice() Log.i("AuthQuickStart", "Remember device succeeded") } catch (error: AuthException) { Log.e("AuthQuickStart", "Remember device failed with error", error) }
1try {
2 Amplify.Auth.rememberDevice()
3 Log.i("AuthQuickStart", "Remember device succeeded")
4} catch (error: AuthException) {
5 Log.e("AuthQuickStart", "Remember device failed with error", error)
6}
RxAmplify.Auth.rememberDevice() .subscribe( () -> Log.i("AuthQuickStart", "Remember device succeeded"), error -> Log.e("AuthQuickStart", "Remember device failed with error " + error.toString()) );
1RxAmplify.Auth.rememberDevice()
2 .subscribe(
3 () -> Log.i("AuthQuickStart", "Remember device succeeded"),
4 error -> Log.e("AuthQuickStart", "Remember device failed with error " + error.toString())
5 );

Forget Device

You can forget your device by using the following API. Note that forgotten devices are still tracked. See below for the difference between remembered, forgotten and tracked.

Amplify.Auth.forgetDevice( () -> Log.i("AuthQuickStart", "Forget device succeeded"), error -> Log.e("AuthQuickStart", "Forget device failed with error " + error.toString()) );
1Amplify.Auth.forgetDevice(
2 () -> Log.i("AuthQuickStart", "Forget device succeeded"),
3 error -> Log.e("AuthQuickStart", "Forget device failed with error " + error.toString())
4);
Amplify.Auth.forgetDevice( { Log.i("AuthQuickStart", "Forget device succeeded") }, { Log.e("AuthQuickStart", "Forget device failed with error", it) } )
1Amplify.Auth.forgetDevice(
2 { Log.i("AuthQuickStart", "Forget device succeeded") },
3 { Log.e("AuthQuickStart", "Forget device failed with error", it) }
4)
try { Amplify.Auth.forgetDevice() Log.i("AuthQuickStart", "Forget device succeeded") } catch (error: AuthException) { Log.e("AuthQuickStart", "Forget device failed with error", error) }
1try {
2 Amplify.Auth.forgetDevice()
3 Log.i("AuthQuickStart", "Forget device succeeded")
4} catch (error: AuthException) {
5 Log.e("AuthQuickStart", "Forget device failed with error", error)
6}
RxAmplify.Auth.forgetDevice() .subscribe( () -> Log.i("AuthQuickStart", "Forget device succeeded"), error -> Log.e("AuthQuickStart", "Forget device failed with error " + error.toString()) );
1RxAmplify.Auth.forgetDevice()
2 .subscribe(
3 () -> Log.i("AuthQuickStart", "Forget device succeeded"),
4 error -> Log.e("AuthQuickStart", "Forget device failed with error " + error.toString())
5 );

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()));
1Amplify.Auth.fetchDevices(
2 devices -> {
3 for (AuthDevice device : devices) {
4 Log.i("AuthQuickStart", "Device: " + device);
5 }
6 },
7 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) } )
1Amplify.Auth.fetchDevices(
2 { devices ->
3 devices.forEach { Log.i("AuthQuickStart", "Device: " + it) }
4 },
5 { Log.e("AuthQuickStart", "Fetch devices failed with error", it) }
6)
try { Amplify.Auth.fetchDevices().forEach { device -> Log.i("AuthQuickStart", "Device: $device") } } catch (error: AuthException) { Log.e("AuthQuickStart", "Fetch devices failed with error", error) }
1try {
2 Amplify.Auth.fetchDevices().forEach { device ->
3 Log.i("AuthQuickStart", "Device: $device")
4 }
5} catch (error: AuthException) {
6 Log.e("AuthQuickStart", "Fetch devices failed with error", error)
7}
RxAmplify.Auth.fetchDevices() .subscribe( device -> Log.i("AuthQuickStart", "Device: " + device); error -> Log.e("AuthQuickStart", "Fetch devices failed with error: " + error.toString()) );
1RxAmplify.Auth.fetchDevices()
2 .subscribe(
3 device -> Log.i("AuthQuickStart", "Device: " + device);
4 error -> Log.e("AuthQuickStart", "Fetch devices failed with error: " + error.toString())
5 );

Terminology

  • 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
    • In the event that you no longer want to remember or track a device, you can use the Auth.forgetDevice() API to remove this device from being both remembered and tracked.