# AuthenticationUtils

Added in 1.4.0-beta01

*** ** * ** ***


```
public final class AuthenticationUtils
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` | `https://developer.android.com/reference/androidx/biometric/AuthenticationUtils#(androidx.activity.ComponentActivity).registerForAuthenticationResult(androidx.biometric.AuthenticationResultCallback)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/ComponentActivity receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback resultCallback )` Returns an `https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` that can be used to initiate authentication. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` | `https://developer.android.com/reference/androidx/biometric/AuthenticationUtils#(androidx.fragment.app.Fragment).registerForAuthenticationResult(androidx.biometric.AuthenticationResultCallback)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback resultCallback )` Returns an `https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` that can be used to initiate authentication. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` | `https://developer.android.com/reference/androidx/biometric/AuthenticationUtils#(androidx.activity.ComponentActivity).registerForAuthenticationResult(java.util.concurrent.Executor,androidx.biometric.AuthenticationResultCallback)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/activity/ComponentActivity receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback resultCallback )` Returns an `https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` that can be used to initiate authentication. |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` | `https://developer.android.com/reference/androidx/biometric/AuthenticationUtils#(androidx.fragment.app.Fragment).registerForAuthenticationResult(java.util.concurrent.Executor,androidx.biometric.AuthenticationResultCallback)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/fragment/app/Fragment receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback resultCallback )` Returns an `https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` that can be used to initiate authentication. |

## Public methods

### registerForAuthenticationResult

Artifact: [androidx.biometric:biometric](https://developer.android.com/jetpack/androidx/releases/biometric) [View Source](https://cs.android.com/search?q=file:androidx/biometric/AuthenticationExtensions.kt+function:registerForAuthenticationResult) Added in [1.4.0-beta01](https://developer.android.com/jetpack/androidx/releases/biometric#1.4.0-beta01)

```
public static final @NonNull AuthenticationResultLauncher registerForAuthenticationResult(
    @NonNull ComponentActivity receiver,
    @NonNull AuthenticationResultCallback resultCallback
)
```

Returns an `https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` that can be used to initiate authentication.

A success or error result will be delivered to `https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback#onAuthResult(androidx.biometric.AuthenticationResult)` and (one or more) failures will be delivered to `https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback#onAuthAttemptFailed()`, which is set by `https://developer.android.com/reference/androidx/biometric/package-summary#(androidx.activity.ComponentActivity).registerForAuthenticationResult(androidx.biometric.AuthenticationResultCallback)`. The callback will be executed on the main thread.

This *must* be called unconditionally, as part of initialization path, typically as a field initializer of an Activity.

Note that if multiple calls to this method are made within a single Fragment or Activity, only the callback registered by the last invocation will be saved and receive authentication results. This can result in unexpected behavior if you intend to manage multiple independent authentication flows. It is strongly recommended to avoid multiple calls to this method in such scenarios.

```kotlin
import androidx.activity.ComponentActivity
import androidx.biometric.AuthenticationRequest
import androidx.biometric.AuthenticationRequest.Biometric
import androidx.biometric.AuthenticationRequest.Companion.biometricRequest
import androidx.biometric.AuthenticationResult
import androidx.biometric.AuthenticationResultCallback
import androidx.biometric.PromptContentItemBulletedText
import androidx.biometric.registerForAuthenticationResult

class MyActivityForBiometricAuth : ComponentActivity() {
    val requestAuthentication =
        registerForAuthenticationResult(
            object : AuthenticationResultCallback {
                override fun onAuthResult(result: AuthenticationResult) {
                    when (result) {
                        // Handle successful authentication
                        is AuthenticationResult.Success -> {
                            Log.i(TAG, "onAuthenticationSucceeded with type ${result.authType}")
                        }
                        // Handle authentication error, e.g. user cancellation, lockout errors,
                        // etc
                        is AuthenticationResult.Error -> {
                            Log.i(
                                TAG,
                                "onAuthenticationError " +
                                    "with error code: ${result.errorCode} " +
                                    "and error string: ${result.errString}",
                            )
                        }
                        // Handle fallback option clicks
                        is AuthenticationResult.CustomFallbackSelected -> {
                            Log.i(TAG, "fallback is selected, text: ${result.fallback.text}")
                        }
                    }
                }

                // Handle intermediate authentication failure, this is optional and
                // not needed in most cases
                override fun onAuthAttemptFailed() {
                    Log.i(TAG, "onAuthenticationFailed, try again")
                }
            }
        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val authRequest =
            biometricRequest(
                title = "Title",
                Biometric.Fallback.DeviceCredential,
                Biometric.Fallback.CustomOption(
                    text = "Use account password",
                    iconType = Biometric.Fallback.ICON_TYPE_PASSWORD,
                ),
            ) {
                setSubtitle("Subtitle")
                setContent(
                    AuthenticationRequest.BodyContent.VerticalList(
                        "Vertical list description",
                        listOf(
                            PromptContentItemBulletedText("test item1"),
                            PromptContentItemBulletedText("test item2"),
                        ),
                    )
                )
                setMinStrength(Biometric.Strength.Class2)
                setIsConfirmationRequired(true)
            }

        Button(this).setOnClickListener { requestAuthentication.launch(authRequest) }
    }
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/biometric/package-summary#(androidx.activity.ComponentActivity).registerForAuthenticationResult(java.util.concurrent.Executor,androidx.biometric.AuthenticationResultCallback)` | (Executor, AuthenticationResultCallback) |

### registerForAuthenticationResult

Artifact: [androidx.biometric:biometric](https://developer.android.com/jetpack/androidx/releases/biometric) [View Source](https://cs.android.com/search?q=file:androidx/biometric/AuthenticationExtensions.kt+function:registerForAuthenticationResult) Added in [1.4.0-beta01](https://developer.android.com/jetpack/androidx/releases/biometric#1.4.0-beta01)

```
public static final @NonNull AuthenticationResultLauncher registerForAuthenticationResult(
    @NonNull Fragment receiver,
    @NonNull AuthenticationResultCallback resultCallback
)
```

Returns an `https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` that can be used to initiate authentication.

A success or error result will be delivered to `https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback#onAuthResult(androidx.biometric.AuthenticationResult)` and (one or more) failures will be delivered to `https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback#onAuthAttemptFailed()`, which is set by `https://developer.android.com/reference/androidx/biometric/package-summary#(androidx.fragment.app.Fragment).registerForAuthenticationResult(androidx.biometric.AuthenticationResultCallback)`. The callback will be executed on the main thread.

This *must* be called unconditionally, as part of initialization path, typically as a field initializer of an Fragment.

Note that if multiple calls to this method are made within a single Fragment or Activity, only the callback registered by the last invocation will be saved and receive authentication results. This can result in unexpected behavior if you intend to manage multiple independent authentication flows. It is strongly recommended to avoid multiple calls to this method in such scenarios.

```kotlin
import androidx.biometric.AuthenticationRequest
import androidx.biometric.AuthenticationRequest.Biometric
import androidx.biometric.AuthenticationRequest.Companion.biometricRequest
import androidx.biometric.AuthenticationResult
import androidx.biometric.PromptContentItemBulletedText
import androidx.biometric.registerForAuthenticationResult
import androidx.fragment.app.Fragment

class MyFragmentForBiometricAuth : Fragment() {
    val requestAuthentication =
        registerForAuthenticationResult { result: AuthenticationResult ->
            when (result) {
                // Handle successful authentication
                is AuthenticationResult.Success -> {
                    Log.i(TAG, "onAuthenticationSucceeded with type ${result.authType}")
                }
                // Handle authentication error, e.g. user cancellation, lockout errors, etc
                is AuthenticationResult.Error -> {
                    Log.i(
                        TAG,
                        "onAuthenticationError " +
                            "with error code: ${result.errorCode} " +
                            "and error string: ${result.errString}",
                    )
                }
                // Handle fallback option clicks
                is AuthenticationResult.CustomFallbackSelected -> {
                    Log.i(TAG, "fallback is selected, text: ${result.fallback.text}")
                }
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val authRequest =
            biometricRequest(
                title = "Title",
                Biometric.Fallback.DeviceCredential,
                Biometric.Fallback.CustomOption(
                    text = "Use account password",
                    iconType = Biometric.Fallback.ICON_TYPE_PASSWORD,
                ),
            ) {
                setSubtitle("Subtitle")
                setContent(
                    AuthenticationRequest.BodyContent.VerticalList(
                        "Vertical list description",
                        listOf(
                            PromptContentItemBulletedText("test item1"),
                            PromptContentItemBulletedText("test item2"),
                        ),
                    )
                )
                setMinStrength(Biometric.Strength.Class2)
                setIsConfirmationRequired(true)
            }

        Button(context).setOnClickListener { requestAuthentication.launch(authRequest) }
    }
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/biometric/package-summary#(androidx.fragment.app.Fragment).registerForAuthenticationResult(java.util.concurrent.Executor,androidx.biometric.AuthenticationResultCallback)` | (Executor, AuthenticationResultCallback) |

### registerForAuthenticationResult

Artifact: [androidx.biometric:biometric](https://developer.android.com/jetpack/androidx/releases/biometric) [View Source](https://cs.android.com/search?q=file:androidx/biometric/AuthenticationExtensions.kt+function:registerForAuthenticationResult) Added in [1.4.0-beta01](https://developer.android.com/jetpack/androidx/releases/biometric#1.4.0-beta01)

```
public static final @NonNull AuthenticationResultLauncher registerForAuthenticationResult(
    @NonNull ComponentActivity receiver,
    @NonNull Executor callbackExecutor,
    @NonNull AuthenticationResultCallback resultCallback
)
```

Returns an `https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` that can be used to initiate authentication.

A success or error result will be delivered to `https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback#onAuthResult(androidx.biometric.AuthenticationResult)` and (one or more) failures will be delivered to `https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback#onAuthAttemptFailed()`, which is set by `https://developer.android.com/reference/androidx/biometric/package-summary#(androidx.activity.ComponentActivity).registerForAuthenticationResult(java.util.concurrent.Executor,androidx.biometric.AuthenticationResultCallback)`. The callback will be executed on the thread provided by the `https://developer.android.com/reference/androidx/biometric/package-summary#(androidx.activity.ComponentActivity).registerForAuthenticationResult(java.util.concurrent.Executor,androidx.biometric.AuthenticationResultCallback)`.

This *must* be called unconditionally, as part of initialization path, typically as a field initializer of an Activity.

Note that if multiple calls to this method are made within a single Fragment or Activity, only the callback registered by the last invocation will be saved and receive authentication results. This can result in unexpected behavior if you intend to manage multiple independent authentication flows. It is strongly recommended to avoid multiple calls to this method in such scenarios.

```kotlin
import androidx.activity.ComponentActivity
import androidx.biometric.AuthenticationRequest
import androidx.biometric.AuthenticationRequest.Biometric
import androidx.biometric.AuthenticationRequest.Companion.biometricRequest
import androidx.biometric.AuthenticationResult
import androidx.biometric.AuthenticationResultCallback
import androidx.biometric.PromptContentItemBulletedText
import androidx.biometric.registerForAuthenticationResult

class MyActivityForBiometricAuth : ComponentActivity() {
    val requestAuthentication =
        registerForAuthenticationResult(
            object : AuthenticationResultCallback {
                override fun onAuthResult(result: AuthenticationResult) {
                    when (result) {
                        // Handle successful authentication
                        is AuthenticationResult.Success -> {
                            Log.i(TAG, "onAuthenticationSucceeded with type ${result.authType}")
                        }
                        // Handle authentication error, e.g. user cancellation, lockout errors,
                        // etc
                        is AuthenticationResult.Error -> {
                            Log.i(
                                TAG,
                                "onAuthenticationError " +
                                    "with error code: ${result.errorCode} " +
                                    "and error string: ${result.errString}",
                            )
                        }
                        // Handle fallback option clicks
                        is AuthenticationResult.CustomFallbackSelected -> {
                            Log.i(TAG, "fallback is selected, text: ${result.fallback.text}")
                        }
                    }
                }

                // Handle intermediate authentication failure, this is optional and
                // not needed in most cases
                override fun onAuthAttemptFailed() {
                    Log.i(TAG, "onAuthenticationFailed, try again")
                }
            }
        )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val authRequest =
            biometricRequest(
                title = "Title",
                Biometric.Fallback.DeviceCredential,
                Biometric.Fallback.CustomOption(
                    text = "Use account password",
                    iconType = Biometric.Fallback.ICON_TYPE_PASSWORD,
                ),
            ) {
                setSubtitle("Subtitle")
                setContent(
                    AuthenticationRequest.BodyContent.VerticalList(
                        "Vertical list description",
                        listOf(
                            PromptContentItemBulletedText("test item1"),
                            PromptContentItemBulletedText("test item2"),
                        ),
                    )
                )
                setMinStrength(Biometric.Strength.Class2)
                setIsConfirmationRequired(true)
            }

        Button(this).setOnClickListener { requestAuthentication.launch(authRequest) }
    }
}
```

### registerForAuthenticationResult

Artifact: [androidx.biometric:biometric](https://developer.android.com/jetpack/androidx/releases/biometric) [View Source](https://cs.android.com/search?q=file:androidx/biometric/AuthenticationExtensions.kt+function:registerForAuthenticationResult) Added in [1.4.0-beta01](https://developer.android.com/jetpack/androidx/releases/biometric#1.4.0-beta01)

```
public static final @NonNull AuthenticationResultLauncher registerForAuthenticationResult(
    @NonNull Fragment receiver,
    @NonNull Executor callbackExecutor,
    @NonNull AuthenticationResultCallback resultCallback
)
```

Returns an `https://developer.android.com/reference/androidx/biometric/AuthenticationResultLauncher` that can be used to initiate authentication.

A success or error result will be delivered to `https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback#onAuthResult(androidx.biometric.AuthenticationResult)` and (one or more) failures will be delivered to `https://developer.android.com/reference/androidx/biometric/AuthenticationResultCallback#onAuthAttemptFailed()`, which is set by `https://developer.android.com/reference/androidx/biometric/package-summary#(androidx.fragment.app.Fragment).registerForAuthenticationResult(java.util.concurrent.Executor,androidx.biometric.AuthenticationResultCallback)`. The callback will be executed on the thread provided by the `https://developer.android.com/reference/androidx/biometric/package-summary#(androidx.fragment.app.Fragment).registerForAuthenticationResult(java.util.concurrent.Executor,androidx.biometric.AuthenticationResultCallback)`.

This *must* be called unconditionally, as part of initialization path, typically as a field initializer of an Fragment.

Note that if multiple calls to this method are made within a single Fragment or Activity, only the callback registered by the last invocation will be saved and receive authentication results. This can result in unexpected behavior if you intend to manage multiple independent authentication flows. It is strongly recommended to avoid multiple calls to this method in such scenarios.

```kotlin
import androidx.biometric.AuthenticationRequest
import androidx.biometric.AuthenticationRequest.Biometric
import androidx.biometric.AuthenticationRequest.Companion.biometricRequest
import androidx.biometric.AuthenticationResult
import androidx.biometric.PromptContentItemBulletedText
import androidx.biometric.registerForAuthenticationResult
import androidx.fragment.app.Fragment

class MyFragmentForBiometricAuth : Fragment() {
    val requestAuthentication =
        registerForAuthenticationResult { result: AuthenticationResult ->
            when (result) {
                // Handle successful authentication
                is AuthenticationResult.Success -> {
                    Log.i(TAG, "onAuthenticationSucceeded with type ${result.authType}")
                }
                // Handle authentication error, e.g. user cancellation, lockout errors, etc
                is AuthenticationResult.Error -> {
                    Log.i(
                        TAG,
                        "onAuthenticationError " +
                            "with error code: ${result.errorCode} " +
                            "and error string: ${result.errString}",
                    )
                }
                // Handle fallback option clicks
                is AuthenticationResult.CustomFallbackSelected -> {
                    Log.i(TAG, "fallback is selected, text: ${result.fallback.text}")
                }
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val authRequest =
            biometricRequest(
                title = "Title",
                Biometric.Fallback.DeviceCredential,
                Biometric.Fallback.CustomOption(
                    text = "Use account password",
                    iconType = Biometric.Fallback.ICON_TYPE_PASSWORD,
                ),
            ) {
                setSubtitle("Subtitle")
                setContent(
                    AuthenticationRequest.BodyContent.VerticalList(
                        "Vertical list description",
                        listOf(
                            PromptContentItemBulletedText("test item1"),
                            PromptContentItemBulletedText("test item2"),
                        ),
                    )
                )
                setMinStrength(Biometric.Strength.Class2)
                setIsConfirmationRequired(true)
            }

        Button(context).setOnClickListener { requestAuthentication.launch(authRequest) }
    }
}
```