Bermigrasi dari Smart Lock untuk Sandi ke Pengelola Kredensial

Smart Lock untuk Sandi, yang tidak digunakan lagi pada tahun 2022, kini dihapus dari Google Play Services Auth SDK (com.google.android.gms:play-services-auth) . Untuk meminimalkan perubahan yang dapat menyebabkan gangguan yang dapat memengaruhi integrasi yang ada, kemampuan Smart Lock untuk semua aplikasi yang ada di Play Store akan terus berfungsi dengan benar. Versi aplikasi baru yang dikompilasi dengan SDK terbaru (com.google.android.gms:play-services-auth:21.0.0) tidak lagi dapat mengakses API Smart Lock untuk Sandi dan tidak akan berhasil dibangun. Semua developer harus memigrasikan project Android mereka untuk menggunakan Pengelola Kredensial sesegera mungkin.

Manfaat migrasi ke Credential Manager API

Pengelola Kredensial menawarkan API sederhana dan terpadu yang memungkinkan dukungan untuk praktik dan fitur modern sekaligus meningkatkan pengalaman autentikasi bagi pengguna Anda:

  • Pengelola Kredensial sepenuhnya mendukung penyimpanan sandi dan autentikasi. Hal ini berarti tidak ada gangguan bagi pengguna saat aplikasi Anda bermigrasi dari Smart Lock ke Pengelola Kredensial.
  • Pengelola Kredensial mengintegrasikan dukungan untuk beberapa metode login, termasuk kunci sandi dan metode login gabungan seperti Login dengan Google, untuk meningkatkan keamanan dan mengaktifkan konversi jika Anda ingin mendukung salah satu metode pada masa mendatang.
  • Mulai Android 14, Pengelola Kredensial mendukung penyedia sandi dan kunci sandi pihak ketiga.
  • Pengelola Kredensial memberikan pengalaman pengguna yang konsisten dan terpadu di semua metode autentikasi.

Opsi migrasi

Kasus penggunaan Rekomendasi
Menyimpan sandi dan login dengan sandi tersimpan Gunakan opsi sandi dari panduan Memproses login pengguna dengan Pengelola Kredensial. Langkah-langkah mendetail untuk menyimpan sandi dan autentikasi akan dijelaskan nanti.
Login dengan Google Ikuti panduan Mengintegrasikan Pengelola Kredensial dengan Login dengan Google.
Menampilkan Petunjuk nomor telepon kepada pengguna Gunakan Phone Number Hint API dari Google Identity Services.

Login dengan sandi menggunakan Pengelola Kredensial

Untuk menggunakan Credential Manager API, selesaikan langkah-langkah yang diuraikan di bagian prasyarat dalam panduan Pengelola Kredensial, dan pastikan Anda melakukan hal berikut:

Login dengan sandi tersimpan

Untuk mengambil opsi sandi yang dikaitkan dengan akun pengguna, selesaikan langkah-langkah berikut:

1. Lakukan inisialisasi opsi autentikasi dan sandi

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

2. Gunakan opsi yang diambil dari langkah sebelumnya untuk membangun permintaan login

val getCredRequest = GetCredentialRequest(
    listOf(getPasswordOption)
)

3. Luncurkan alur login

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}")
        }
    }
}

Referensi lainnya