يُرجى العلم بأنّ ميزة Smart Lock لكلمات المرور، التي توقفت نهائيًا في عام 2022، قد تمت
إزالتها من حزمة تطوير البرامج (SDK) للمصادقة في "خدمات Google Play"
(com.google.android.gms:play-services-auth
) . لتقليل التغييرات التي قد تؤدي إلى أعطال في عمليات التكامل الحالية، ستواصل إمكانات Smart Lock عملها بشكل سليم في جميع التطبيقات
الحالية في "متجر Play". لم يعُد بإمكان إصدارات التطبيقات الجديدة
التي تم تجميعها باستخدام حزمة SDK المعدَّلة
(com.google.android.gms:play-services-auth:21.0.0
) الوصول إلى واجهة برمجة التطبيقات لميزة ”Smart Lock لكلمات المرور“، وبذلك لن يتم إصدار هذه التطبيقات بنجاح. على جميع
المطوّرين نقل مشاريع Android لاستخدام Credential Manager
في أقرب وقت ممكن.
مزايا نقل البيانات إلى واجهة برمجة التطبيقات Credential Manager API
يوفّر تطبيق "إدارة بيانات الاعتماد" واجهة برمجة تطبيقات بسيطة ومُوحّدة تتيح استخدام المميزات والممارسات الحديثة مع تحسين تجربة المصادقة للمستخدمين:
- يتوافق "مدير بيانات الاعتماد" بالكامل مع حفظ كلمات المرور والمصادقة. وهذا يعني عدم انقطاع الخدمة للمستخدمين أثناء نقل بيانات تطبيقك من ميزة "قفل ذكي" إلى "مدير بيانات الاعتماد".
- توفّر واجهة Credential Manager إمكانية استخدام طرق متعددة لتسجيل الدخول، بما في ذلك مفاتيح المرور وطرق تسجيل الدخول المُدار مثل تسجيل الدخول باستخدام حساب Google، وذلك لتعزيز الأمان وتفعيل عملية التحويل إذا كنت تخطط لاستخدام أي منهما في المستقبل.
- بدءًا من Android 14، يتيح "مدير بيانات الاعتماد" استخدام مقدّمي خدمات كلمات المرور ومفاتيح المرور التابعين لجهات خارجية.
- يقدّم "مدير بيانات الاعتماد" تجربة مستخدم متسقة ومُوحّدة على جميع طرق المصادقة.
خيارات نقل البيانات:
سرد قصة | الاقتراح |
---|---|
حفظ كلمة المرور وتسجيل الدخول باستخدام كلمة المرور المحفوظة | استخدِم خيار كلمة المرور من دليل تسجيل دخول المستخدم باستخدام "مدير بيانات الاعتماد". سيتم وصف الخطوات التفصيلية لحفظ كلمة المرور والمصادقة عليها في وقت لاحق. |
تسجيل الدخول باستخدام حساب Google | اتّبِع الخطوات الواردة في دليل دمج Credential Manager مع ميزة "تسجيل الدخول باستخدام حساب Google". |
إظهار تلميح رقم الهاتف للمستخدمين | استخدِم Phone Number Hint API من Google Identity Services. |
تسجيل الدخول باستخدام كلمات المرور باستخدام "مدير بيانات الاعتماد"
لاستخدام واجهة برمجة التطبيقات Credential Manager API، أكمِل الخطوات الموضّحة في قسم المتطلبات الأساسية من دليل Credential Manager، وتأكَّد من تنفيذ الخطوات التالية:
- إضافة التبعيات المطلوبة
- الحفاظ على الفئات في ملف ProGuard
- إتاحة روابط مواد العرض الرقمية (من المفترض أن تكون هذه الخطوة قد تم تنفيذها في حال كنت تستخدم ميزة "قفل ذكي" لكلمات المرور)
- ضبط "مدير بيانات الاعتماد"
- الإشارة إلى حقول بيانات الاعتماد
- حفظ كلمة مرور المستخدم
تسجيل الدخول باستخدام كلمات المرور المحفوظة
لاسترداد خيارات كلمة المرور المرتبطة بحساب المستخدم، عليك إكمال الخطوات التالية:
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
- الدرس التطبيقي حول أداة "إدارة بيانات الاعتماد"
- تسجيل دخول المستخدم باستخدام "مدير بيانات الاعتماد"