W 2022 roku wycofaliśmy usługę Smart Lock na hasła i usunęliśmy ją 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 dotychczasowych aplikacjach w Sklepie Play będą nadal działać. Nowe wersje aplikacji kompilowane z użyciem zaktualizowanego pakietu (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 zacząć używać w swoich projektach na Androida usługi Credential Manager.
Zalety przejścia na interfejs Credential Manager API
Menedżer danych logowania oferuje prosty, ujednolicony interfejs API, który umożliwia obsługę nowoczesnych funkcji i praktyk, a także ułatwia uwierzytelnianie użytkowników:
- Menedżer danych logowania w pełni obsługuje zapisywanie haseł i uwierzytelnianie. Oznacza to, że użytkownicy nie odczują żadnej przerwy w działaniu aplikacji podczas migracji z Smart Lock na Menedżera danych logowania.
- Menedżer danych logowania obsługuje wiele metod logowania, w tym klucze dostępu i federowane metody logowania, takie jak Zaloguj się przez Google. Dzięki temu możesz zwiększyć bezpieczeństwo i umożliwić konwersję, jeśli planujesz wdrożenie tych metod w przyszłości.
- Począwszy od Androida 14 Menedżer danych logowania obsługuje zewnętrznych dostawców haseł i kluczy dostępu.
- Menedżer danych logowania 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 poradnika Logowanie użytkownika za pomocą menedżera danych logowania. Szczegółowe instrukcje dotyczące zapisywania haseł i uwierzytelniania znajdziesz w dalszej części artykułu. |
Zaloguj się przez Google | Postępuj zgodnie z instrukcjami w tym przewodniku, aby zintegrować Credential Manager z funkcją Zaloguj się przez Google. |
Wyświetlanie użytkownikom podpowiedzi dotyczącej numeru telefonu | Użyj interfejsu Phone Number Hint API z usług Google Identity. |
Logowanie się za pomocą haseł przy użyciu Menedżera danych logowania
Aby korzystać z interfejsu Credential Manager API, wykonaj czynności opisane w sekcji wymagania wstępne w przewodniku po Credential Manager. Pamiętaj o tych kwestiach:
- Dodawanie wymaganych zależności
- Zachowaj klasy w pliku ProGuard
- Dodaj obsługę linków do zasobów cyfrowych (jeśli używasz funkcji Smart Lock do haseł, ten krok powinien być już wykonany)
- Konfigurowanie Menedżera danych uwierzytelniających
- Wskazanie pól danych logowania
- Zapisywanie hasła użytkownika
Logowanie się przy użyciu zapisanych haseł
Aby pobrać opcje hasła powiązane z kontem użytkownika:
1. Inicjowanie hasła i opcji uwierzytelniania
// Retrieves the user's saved password for your app from their
// password provider.
val getPasswordOption = GetPasswordOption()
2. Użyj opcji pobranych z poprzedniego etapu, aby utworzyć prośbę o logowanie
val getCredRequest = GetCredentialRequest(
listOf(getPasswordOption)
)
3. Uruchamianie procedury 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
- Przykładowe odwołanie do Credential Managera
- Ćwiczenie Codelab dotyczące Credential Managera
- Logowanie użytkownika za pomocą Menedżera danych logowania