Credential


abstract class Credential

Known direct subclasses
CustomCredential

Base class for a custom credential with which the user consented to authenticate to the app.

PasswordCredential

Represents the user's password credential granted by the user for app sign-in.

PublicKeyCredential

Represents the user's passkey credential granted by the user for app sign-in.


Base class for a credential with which the user consented to authenticate to the app.

import androidx.credentials.Credential
import androidx.credentials.CustomCredential
import androidx.credentials.PasswordCredential
import androidx.credentials.PublicKeyCredential

when (credential) {
    is PublicKeyCredential -> {
        val responseJson = credential.authenticationResponseJson
        val userCredential = fidoAuthenticateWithServer(responseJson)
        loginWithPasskey(userCredential)
    }
    is PasswordCredential -> {
        val userName = credential.id
        val password = credential.password
        loginWithPassword(userName, password)
    }
    is CustomCredential -> {
        // If you are also using any external sign-in libraries, parse them here with the
        // utility functions provided they provided.
        if (credential.type == ExampleCustomCredential.TYPE) {
            try {
                val exampleCustomCredential =
                    ExampleCustomCredential.createFrom(credential.data)
                loginWithExampleCustomCredential(exampleCustomCredential)
            } catch (e: ExampleCustomCredential.ExampleCustomCredentialParsingException) {
                // Unlikely to happen. If it does, you likely need to update the dependency
                // version of your external sign-in library.
                Log.e(TAG, "Failed to parse an ExampleCustomCredential", e)
            }
        } else {
            Log.w(
                TAG,
                "Received unrecognized credential type ${credential.type}. " +
                    "This shouldn't happen")
        }
    }
    else -> {
        Log.w(
            TAG,
            "Received unrecognized credential type ${credential.type}. This shouldn't happen")
    }
}

Summary

Public properties

Bundle

the credential data in the Bundle format

String

the credential type determined by the credential-type-specific subclass (e.g. PasswordCredential.TYPE_PASSWORD_CREDENTIAL for PasswordCredential or PublicKeyCredential.TYPE_PUBLIC_KEY_CREDENTIAL for PublicKeyCredential)

Public properties

data

Added in 1.2.0
val dataBundle

the credential data in the Bundle format

type

Added in 1.2.0
val typeString

the credential type determined by the credential-type-specific subclass (e.g. PasswordCredential.TYPE_PASSWORD_CREDENTIAL for PasswordCredential or PublicKeyCredential.TYPE_PUBLIC_KEY_CREDENTIAL for PublicKeyCredential)