Smart Lock สำหรับรหัสผ่าน ซึ่งเลิกใช้งานในปี 2022 ถูกนำออกจาก SDK การตรวจสอบสิทธิ์ของบริการ Google Play (com.google.android.gms:play-services-auth) แล้ว ความสามารถของ Smart Lock สำหรับแอปที่มีอยู่ทั้งหมดใน Play Store จะทำงานอย่างถูกต้องต่อไป ทั้งนี้เพื่อลดการเปลี่ยนแปลงที่ส่งผลกับส่วนอื่นในระบบที่อาจส่งผลกับส่วนอื่นในการผสานรวมที่มีอยู่ให้น้อยที่สุด แอปเวอร์ชันใหม่ๆ ที่คอมไพล์ด้วย SDK ที่อัปเดต (com.google.android.gms:play-services-auth:21.0.0) จะไม่สามารถเข้าถึง Smart Lock สำหรับรหัสผ่าน API ได้อีกต่อไปและจะสร้างไม่สำเร็จ นักพัฒนาแอปทุกคนควรย้ายข้อมูลโปรเจ็กต์ Android ไปใช้ Credential Manager โดยเร็วที่สุด
ประโยชน์ของการย้ายข้อมูลไปยัง Credential Manager API
Credential Manager มี API แบบรวมที่ใช้งานง่าย ซึ่งรองรับฟีเจอร์และแนวทางปฏิบัติที่ทันสมัย พร้อมทั้งปรับปรุงประสบการณ์การตรวจสอบสิทธิ์สำหรับผู้ใช้ ดังนี้
- Credential Manager รองรับการบันทึกและการตรวจสอบสิทธิ์รหัสผ่านอย่างเต็มรูปแบบ ซึ่งหมายความว่าผู้ใช้จะไม่ถูกรบกวนเมื่อแอปย้ายข้อมูลจาก Smart Lock ไปยัง Credential Manager
- Credential Manager ผสานรวมการรองรับวิธีการลงชื่อเข้าใช้หลายวิธี ซึ่งรวมถึงพาสคีย์และวิธีการลงชื่อเข้าใช้แบบรวมศูนย์ เช่น ลงชื่อเข้าใช้ด้วย Google เพื่อเพิ่มความปลอดภัยและเปิดใช้ Conversion หากคุณวางแผนที่จะรองรับวิธีใดวิธีหนึ่งในอนาคต
- ตั้งแต่ Android 14 เป็นต้นไป Credential Manager จะรองรับผู้ให้บริการรหัสผ่านและพาสคีย์บุคคลที่สาม
- Credential Manager มอบประสบการณ์ของผู้ใช้ที่สอดคล้องและเป็นหนึ่งเดียวในวิธีการตรวจสอบสิทธิ์ทั้งหมด
ตัวเลือกการย้ายข้อมูล
| กรณีการใช้งาน | คำแนะนำ |
|---|---|
| บันทึกรหัสผ่านและลงชื่อเข้าใช้ด้วยรหัสผ่านที่บันทึกไว้ | ใช้ตัวเลือกรหัสผ่านจากคู่มือลงชื่อเข้าใช้ผู้ใช้ด้วย Credential Manager รายละเอียดขั้นตอนการบันทึกรหัสผ่านและการตรวจสอบสิทธิ์จะอธิบายไว้ในภายหลัง |
| ลงชื่อเข้าใช้ด้วย Google | ทำตามคำแนะนำในการผสานรวม Credential Manager กับฟีเจอร์ลงชื่อเข้าใช้ด้วย Google |
| แสดงคำแนะนำหมายเลขโทรศัพท์แก่ผู้ใช้ | ใช้ Phone Number Hint API จาก Google Identity Services |
ลงชื่อเข้าใช้ด้วยรหัสผ่านโดยใช้ Credential Manager
หากต้องการใช้ Credential Manager API ให้ทำตามขั้นตอนที่ระบุไว้ในส่วนข้อกำหนดเบื้องต้นของคู่มือ Credential Manager และตรวจสอบว่าคุณได้ทำสิ่งต่อไปนี้
- เพิ่มทรัพยากร Dependency ที่จำเป็น
- เก็บรักษาคลาสในไฟล์ ProGuard
- เพิ่มการรองรับ Digital Asset Links (ขั้นตอนนี้ควรดำเนินการแล้วหากคุณใช้ Smart Lock สำหรับรหัสผ่าน)
- กำหนดค่า Credential Manager
- ระบุช่องข้อมูลเข้าสู่ระบบ
- บันทึกรหัสผ่านของผู้ใช้
ลงชื่อเข้าใช้ด้วยรหัสผ่านที่บันทึกไว้
หากต้องการเรียกดูตัวเลือกรหัสผ่านที่เชื่อมโยงกับบัญชีของผู้ใช้ ให้ทำตามขั้นตอนต่อไปนี้
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
- Codelab ของ Credential Manager
- ลงชื่อเข้าใช้ผู้ใช้ด้วย Credential Manager