Smart Lock สำหรับรหัสผ่าน ซึ่งเลิกใช้งานในปี 2022 ถูกนำออกจาก Google Play Services Auth SDK
(com.google.android.gms:play-services-auth
) แล้วตอนนี้ ความสามารถของ Smart Lock สำหรับแอปทั้งหมดที่มีอยู่ใน Play Store จะทำงานอย่างถูกต้องต่อไป ทั้งนี้เพื่อลดการเปลี่ยนแปลงที่อาจส่งผลกับส่วนอื่นในการผสานรวมที่มีอยู่ให้น้อยที่สุด แอปเวอร์ชันใหม่ๆ ที่คอมไพล์ด้วย SDK ที่อัปเดตแล้ว (com.google.android.gms:play-services-auth:21.0.0
) จะไม่สามารถเข้าถึง Smart Lock สำหรับ Password API ได้อีกต่อไปและจะสร้างไม่สำเร็จ นักพัฒนาแอปทุกคนควรย้ายข้อมูลโปรเจ็กต์ Android ไปใช้ Credential Manager โดยเร็วที่สุด
ประโยชน์ของการย้ายข้อมูลไปยัง Credential Manager API
เครื่องมือจัดการข้อมูลเข้าสู่ระบบมี API แบบรวมที่ใช้งานง่ายซึ่งรองรับฟีเจอร์และแนวทางปฏิบัติสมัยใหม่ ในขณะเดียวกันก็ปรับปรุงประสบการณ์การตรวจสอบสิทธิ์ให้กับผู้ใช้ด้วย
- เครื่องมือจัดการข้อมูลเข้าสู่ระบบรองรับการบันทึกรหัสผ่านและการตรวจสอบสิทธิ์อย่างเต็มรูปแบบ ซึ่งหมายความว่าผู้ใช้จะไม่พบการหยุดชะงักขณะที่แอปย้ายข้อมูลจาก Smart Lock ไปยังเครื่องมือจัดการข้อมูลเข้าสู่ระบบ
- Credential Manager ผสานรวมการรองรับวิธีการลงชื่อเข้าใช้หลายวิธี ซึ่งรวมถึงพาสคีย์และวิธีการลงชื่อเข้าใช้แบบรวมศูนย์ เช่น ลงชื่อเข้าใช้ด้วย Google เพื่อเพิ่มความปลอดภัยและเปิดใช้ Conversion หากคุณวางแผนที่จะรองรับวิธีการดังกล่าวในอนาคต
- ตั้งแต่ Android 14 เป็นต้นไป เครื่องมือจัดการข้อมูลเข้าสู่ระบบจะรองรับผู้ให้บริการรหัสผ่านและพาสคีย์ของบุคคลที่สาม
- เครื่องมือจัดการข้อมูลเข้าสู่ระบบมอบประสบการณ์การใช้งานที่สอดคล้องกันและรวมเป็นหนึ่งเดียวสำหรับวิธีตรวจสอบสิทธิ์ทั้งหมด
ตัวเลือกการย้ายข้อมูล
การเล่าเรื่องผ่าน | คำแนะนำ |
---|---|
บันทึกรหัสผ่านและลงชื่อเข้าใช้ด้วยรหัสผ่านที่บันทึกไว้ | ใช้ตัวเลือกรหัสผ่านจากคู่มือลงชื่อเข้าใช้ผู้ใช้ด้วยเครื่องมือจัดการข้อมูลเข้าสู่ระบบ ขั้นตอนโดยละเอียดสำหรับการบันทึกรหัสผ่านและการตรวจสอบสิทธิ์จะอธิบายไว้ในส่วนถัดไป |
ลงชื่อเข้าใช้ด้วย Google | ทำตามคำแนะนำผสานรวม Credential Manager กับฟีเจอร์ลงชื่อเข้าใช้ด้วย Google |
แสดงคำแนะนำหมายเลขโทรศัพท์ให้ผู้ใช้เห็น | ใช้ Phone Number Hint API จากบริการระบุตัวตนของ Google |
ลงชื่อเข้าใช้ด้วยรหัสผ่านโดยใช้เครื่องมือจัดการข้อมูลเข้าสู่ระบบ
หากต้องการใช้ Credential Manager API ให้ทําตามขั้นตอนที่ระบุไว้ในส่วนข้อกําหนดเบื้องต้นของคู่มือ Credential Manager และตรวจสอบว่าคุณได้ทำสิ่งต่อไปนี้
- เพิ่มการพึ่งพาที่จำเป็น
- เก็บรักษาคลาสในไฟล์ ProGuard
- เพิ่มการรองรับลิงก์ชิ้นงานดิจิทัล (ขั้นตอนนี้ควรทำไปแล้วหากคุณใช้ Smart Lock สำหรับรหัสผ่าน)
- กำหนดค่าเครื่องมือจัดการข้อมูลเข้าสู่ระบบ
- ระบุช่องข้อมูลเข้าสู่ระบบ
- บันทึกรหัสผ่านของผู้ใช้
ลงชื่อเข้าใช้ด้วยรหัสผ่านที่บันทึกไว้
หากต้องการเรียกดูตัวเลือกรหัสผ่านที่เชื่อมโยงกับบัญชีของผู้ใช้ ให้ทำตามขั้นตอนต่อไปนี้
1. เริ่มต้นรหัสผ่านและตัวเลือกการตรวจสอบสิทธิ์
// Retrieves the user's saved password for your app from their
// password provider.
val getPasswordOption = GetPasswordOption()
2. ใช้ตัวเลือกที่ดึงมาจากขั้นตอนก่อนหน้าเพื่อสร้างคําขอลงชื่อเข้าใช้
val getCredRequest = GetCredentialRequest(
listOf(getPasswordOption)
)
3. เปิดขั้นตอนการลงชื่อเข้าใช้
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}")
}
}
}
แหล่งข้อมูลเพิ่มเติม
- ข้อมูลอ้างอิงตัวอย่างของ Credential Manager
- Credential Manager Codelab
- ลงชื่อเข้าใช้ผู้ใช้ด้วยเครื่องมือจัดการข้อมูลเข้าสู่ระบบ