Przejście z funkcji Smart Lock na hasła na Menedżera danych logowania

Usługa Smart Lock na hasła, która została wycofana w 2022 roku, została teraz usunięta z pakietu Google Play Services Auth SDK (com.google.android.gms:play-services-auth) . Aby zminimalizować wpływ zmian powodujących niezgodności w istniejących już integracjach, funkcje Smart Lock we wszystkich aplikacjach w Sklepie Play będą nadal działać. Nowe wersje aplikacji kompilowane z użyciem zaktualizowanego pakietu SDK (com.google.android.gms:play-services-auth:21.0.0) nie są już w stanie korzystać z interfejsu API Smart Lock na hasła i nie będą się pomyślnie kompilować. Wszyscy deweloperzy powinni jak najszybciej przeprowadzić migrację swoich projektów aplikacji na Androida, aby używać Credential Manager.

Korzyści z przejścia na interfejs Credential Manager API

Menedżer danych logowania udostępnia uproszczony, ujednolicony interfejs API, który umożliwia obsługę nowoczesnych funkcji i praktyk, a jednocześnie ułatwia użytkownikom uwierzytelnianie:

  • Credential Manager w pełni obsługuje zapisywanie haseł i uwierzytelnianie. Oznacza to, że użytkownicy nie odczują żadnych przerw w działaniu aplikacji podczas jej migracji ze Smart Lock do Credential Manager.
  • Credential Manager obsługuje wiele metod logowania, w tym klucze dostępu i metody logowania sfederowanego, takie jak Zaloguj się przez Google, aby zwiększyć bezpieczeństwo i umożliwić konwersję, jeśli planujesz obsługiwać którąś z tych metod w przyszłości.
  • Od Androida 14 Credential Manager obsługuje zewnętrznych dostawców haseł i kluczy dostępu.
  • Credential Manager zapewnia spójne, ujednolicone wrażenia użytkownika we wszystkich metodach uwierzytelniania.

Opcje migracji:

Przypadek użycia Rekomendacja
Zapisywanie hasła i logowanie się przy użyciu zapisanego hasła Użyj opcji hasła z przewodnika Logowanie użytkownika za pomocą Menedżera danych logowania. Szczegółowe instrukcje dotyczące zapisywania haseł i uwierzytelniania znajdziesz poniżej.
Zaloguj się przez Google Postępuj zgodnie z instrukcjami podanymi w przewodniku Integracja Credential Managera z funkcją Zaloguj się przez Google.
Wyświetlaj użytkownikom podpowiedź dotyczącą numeru telefonu Użyj interfejsu Phone Number Hint API z usług Google Identity Services.

Logowanie się za pomocą haseł przy użyciu Menedżera danych logowania

Aby używać interfejsu Credential Manager API, wykonaj czynności opisane w sekcji Wymagania wstępne w przewodniku po Menedżerze danych logowania i upewnij się, że:

Logowanie się przy użyciu zapisanych haseł

Aby pobrać opcje haseł powiązane z kontem użytkownika, wykonaj te czynności:

1. Inicjowanie opcji hasła i uwierzytelniania

// Retrieves the user's saved password for your app from their
// password provider.
val getPasswordOption = GetPasswordOption()

2. Użyj opcji pobranych w poprzednim kroku, aby utworzyć żądanie logowania.

val getCredRequest = GetCredentialRequest(
    listOf(getPasswordOption)
)

3. Uruchamianie procesu logowania

fun launchSignInFlow() {
    coroutineScope.launch {
        try {
            // Attempt to retrieve the credential from the Credential Manager.
            val result = credentialManager.getCredential(
                // Use an activity-based context to avoid undefined system UI
                // launching behavior.
                context = activityContext,
                request = getCredRequest
            )

            // Process the successfully retrieved credential.
            handleSignIn(result)
        } catch (e: GetCredentialException) {
            // Handle any errors that occur during the credential retrieval
            // process.
            handleFailure(e)
        }
    }
}

private fun handleSignIn(result: GetCredentialResponse) {
    // Extract the credential from the response.
    val credential = result.credential

    // Determine the type of credential and handle it accordingly.
    when (credential) {
        is PasswordCredential -> {
            val username = credential.id
            val password = credential.password

            // Use the extracted username and password to perform
            // authentication.
        }

        else -> {
            // Handle unrecognized credential types.
            Log.e(TAG, "Unexpected type of credential")
        }
    }
}

private fun handleFailure(e: GetCredentialException) {
    // Handle specific credential retrieval errors.
    when (e) {
        is GetCredentialCancellationException -> {
            /* This exception is thrown when the user intentionally cancels
                the credential retrieval operation. Update the application's state
                accordingly. */
        }

        is GetCredentialCustomException -> {
            /* This exception is thrown when a custom error occurs during the
                credential retrieval flow. Refer to the documentation of the
                third-party SDK used to create the GetCredentialRequest for
                handling this exception. */
        }

        is GetCredentialInterruptedException -> {
            /* This exception is thrown when an interruption occurs during the
                credential retrieval flow. Determine whether to retry the
                operation or proceed with an alternative authentication method. */
        }

        is GetCredentialProviderConfigurationException -> {
            /* This exception is thrown when there is a mismatch in
                configurations for the credential provider. Verify that the
                provider dependency is included in the manifest and that the
                required system services are enabled. */
        }

        is GetCredentialUnknownException -> {
            /* This exception is thrown when the credential retrieval
                operation fails without providing any additional details. Handle
                the error appropriately based on the application's context. */
        }

        is GetCredentialUnsupportedException -> {
            /* This exception is thrown when the device does not support the
                Credential Manager feature. Inform the user that credential-based
                authentication is unavailable and guide them to an alternative
                authentication method. */
        }

        is NoCredentialException -> {
            /* This exception is thrown when there are no viable credentials
                available for the user. Prompt the user to sign up for an account
                or provide an alternative authentication method. Upon successful
                authentication, store the login information using
                androidx.credentials.CredentialManager.createCredential to
                facilitate easier sign-in the next time. */
        }

        else -> {
            // Handle unexpected exceptions.
            Log.w(TAG, "Unexpected exception type: ${e::class.java.name}")
        }
    }
}

Dodatkowe materiały