Di chuyển từ Smart Lock cho Mật khẩu sang Trình quản lý thông tin xác thực

Tính năng Smart Lock cho Mật khẩu (đã ngừng hoạt động vào năm 2022) hiện bị xoá khỏi SDK xác thực của Dịch vụ Google Play (com.google.android.gms:play-services-auth) . Để giảm thiểu những thay đổi có thể gây lỗi làm ảnh hưởng đến các công cụ tích hợp hiện có, các chức năng của Smart Lock cho mọi ứng dụng hiện có trong Cửa hàng Play sẽ tiếp tục hoạt động đúng cách. Các phiên bản ứng dụng mới được biên dịch bằng SDK đã cập nhật (com.google.android.gms:play-services-auth:21.0.0) không thể truy cập vào API Smart Lock cho Mật khẩu được nữa và sẽ không tạo thành công. Tất cả các nhà phát triển nên di chuyển dự án Android của mình để sử dụng Trình quản lý thông tin xác thực sớm nhất có thể.

Lợi ích của việc di chuyển sang Credential Manager API

Trình quản lý thông tin xác thực cung cấp một API hợp nhất và đơn giản, tạo điều kiện hỗ trợ các tính năng và phương pháp hiện đại, đồng thời cải thiện trải nghiệm xác thực cho người dùng:

  • Trình quản lý thông tin xác thực hỗ trợ đầy đủ việc lưu và xác thực mật khẩu. Nhờ vậy, người dùng sẽ không bị gián đoạn khi ứng dụng của bạn di chuyển từ Smart Lock sang Trình quản lý thông tin xác thực.
  • Trình quản lý thông tin xác thực tích hợp tính năng hỗ trợ nhiều phương thức đăng nhập (trong đó có khoá truy cập và các phương thức đăng nhập liên kết như Đăng nhập bằng Google) để tăng tính bảo mật và cho phép chuyển đổi nếu bạn dự định hỗ trợ trong tương lai.
  • Kể từ Android 14, Trình quản lý thông tin xác thực hỗ trợ các trình cung cấp mật khẩu và khoá truy cập bên thứ ba.
  • Trình quản lý thông tin xác thực mang đến trải nghiệm người dùng nhất quán và hợp nhất trên mọi phương thức xác thực.

Các lựa chọn di chuyển:

Trường hợp sử dụng Đề xuất
Lưu mật khẩu và đăng nhập bằng mật khẩu đã lưu Sử dụng lựa chọn mật khẩu trong hướng dẫn Đăng nhập cho người dùng bằng Trình quản lý thông tin xác thực. Các bước chi tiết để lưu và xác thực mật khẩu sẽ được mô tả ở phần sau.
Đăng nhập bằng Google Làm theo hướng dẫn Tích hợp Trình quản lý thông tin xác thực với tính năng Đăng nhập bằng Google.
Hiện gợi ý số điện thoại cho người dùng Sử dụng API Gợi ý số điện thoại trong Dịch vụ nhận dạng của Google.

Đăng nhập bằng các mật khẩu thông qua Trình quản lý thông tin xác thực

Để sử dụng API Trình quản lý thông tin xác thực, hãy hoàn thành các bước nêu trong phần điều kiện tiên quyết của hướng dẫn về Trình quản lý thông tin xác thực và đảm bảo bạn làm như sau:

Đăng nhập bằng các mật khẩu đã lưu

Để truy xuất các lựa chọn mật khẩu được liên kết với tài khoản của người dùng, hãy hoàn thành các bước sau:

1. Khởi động lựa chọn mật khẩu và xác thực

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

2. Sử dụng các lựa chọn được truy xuất từ bước trước để tạo yêu cầu đăng nhập

val getCredRequest = GetCredentialRequest(
    listOf(getPasswordOption)
)

3. Khởi động quy trình đăng nhập

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

Tài nguyên khác