يمكنك السماح للمستخدمين بالمصادقة من خلال Firebase باستخدام موفّري OAuth مثل Yahoo من خلال دمج تسجيل الدخول العام عبر OAuth في تطبيقك باستخدام حزمة SDK لـ Firebase من أجل من البداية إلى النهاية
قبل البدء
لتسجيل دخول المستخدمين باستخدام حسابات Yahoo، يجب أولاً تمكين Yahoo لتسجيل الدخول مقدم الخدمة لمشروعك على Firebase:
- إضافة Firebase إلى مشروع Apple
- في وحدة تحكُّم Firebase، افتح قسم المصادقة.
- في علامة التبويب طريقة تسجيل الدخول، فعِّل موفّر الخدمة Yahoo.
- أضِف Client-ID (معرّف العميل) وClient Secret (سر العميل) من وحدة تحكّم المطوّرين لدى هذا الموفّر إلى
إعداد موفِّر الخدمة:
-
لتسجيل عميل Yahoo OAuth، اتبع الخطوات الخاصة بمطوّر برامج Yahoo مستندات عن تسجيل تطبيق ويب في Yahoo.
تأكّد من اختيار إذنَي OpenID Connect API: "
profile
" وemail
" - عند تسجيل التطبيقات لدى مقدّمي الخدمات هؤلاء، يُرجى التأكّد من تسجيل
نطاق
*.firebaseapp.com
لمشروعك كنطاق إعادة التوجيه التطبيق.
-
- انقر على حفظ.
معالجة عملية تسجيل الدخول باستخدام حزمة تطوير البرامج (SDK) لمنصّة Firebase
لمعالجة عملية تسجيل الدخول باستخدام حزمة تطوير البرامج (SDK) لمنصات Firebase Apple، اتّبِع الخطوات التالية:
إضافة مخططات عناوين URL مخصصة إلى مشروع Xcode:
- افتح إعدادات المشروع: انقر نقرًا مزدوجًا على اسم المشروع في العرض الشجري الأيسر. اختَر تطبيقك من قسم TARGETS، ثم واختَر علامة التبويب المعلومات، ووسِّع القسم أنواع عناوين URL.
- انقر على الزر + وأضِف رقم تعريف التطبيق المشفر كعنوان URL.
. يمكنك العثور على رقم تعريف التطبيق المشفر على
عام
"الإعدادات" في وحدة تحكُّم Firebase، في قسم نظام التشغيل iOS
التطبيق. واترك الحقول الأخرى فارغة.
عند الانتهاء، من المفترض أن تبدو التهيئة بشكلٍ مشابه التالية (ولكن مع القيم الخاصة بالتطبيقات):
إنشاء نسخة افتراضية من OAuthProvider باستخدام رقم تعريف الموفِّر yahoo.com.
var provider = OAuthProvider(providerID: "yahoo.com")
FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:@"yahoo.com"];
اختياري: تحديد مَعلمات OAuth المخصَّصة الإضافية التي تريد الإرسال باستخدام طلب OAuth.
provider.customParameters = [ "prompt": "login", "language": "fr" ]
[provider setCustomParameters:@{@"prompt": @"login", @"language": @"fr"}];
بالنسبة إلى المعلمات التي تدعمها Yahoo، راجع مستندات Yahoo OAuth. تجدر الإشارة إلى أنّه لا يمكنك تمرير المَعلمات المطلوبة في Firebase مع
setCustomParameters
هذه المَعلمات هي client_id redirect_uri وresponse_type وscope وstate.اختياري: يمكنك تحديد نطاقات OAuth 2.0 الإضافية التي تتجاوز
profile
email
الذي تريد طلبه من مزوِّد المصادقة. إذا كان الوصول إلى بيانات المستخدم الخاصة من واجهات برمجة تطبيقات Yahoo، فيجب طلب أذونات لواجهات برمجة تطبيقات Yahoo API ضمن أذونات واجهة برمجة التطبيقات في وحدة تحكّم المطوّرين في Yahoo. يجب أن تتطابق نطاقات OAuth المطلوبة تمامًا مع الإعدادات مسبقًا في أذونات واجهة برمجة التطبيقات الخاصة بالتطبيق. على سبيل المثال، إذا كانت قراءة/كتابة يتم طلب الوصول إلى جهات اتصال المستخدم ويتم ضبطه مسبقًا في واجهة برمجة التطبيقات الخاصة بالتطبيق يجب تمريرsdct-w
بدلاً من نطاق OAuth للقراءة فقطsdct-r
وإلا، سيفشل التدفق وسيظهر خطأ في المستخدم النهائي.// Request access to Yahoo Mail API. // Request read/write access to user contacts. // This must be preconfigured in the app's API permissions. provider.scopes = ["mail-r", "sdct-w"]
// Request access to Yahoo Mail API. // Request read/write access to user contacts. // This must be preconfigured in the app's API permissions. [provider setScopes:@[@"mail-r", @"sdct-w"]];
لمزيد من المعلومات، يُرجى الرجوع إلى مستندات نطاقات Yahoo.
اختياري: إذا كنت تريد تخصيص طريقة عرض تطبيقك
SFSafariViewController
أوUIWebView
عندما تعرض اختبار reCAPTCHA للمستخدم، أنشئ فئة مخصصة تتوافق إلى بروتوكولAuthUIDelegate
، ومرره إلىcredentialWithUIDelegate
عليك المصادقة مع Firebase باستخدام كائن موفّر بروتوكول OAuth.
provider.getCredentialWith(nil) { credential, error in if error != nil { // Handle error. } if credential != nil { Auth().signIn(with: credential) { authResult, error in if error != nil { // Handle error. } // User is signed in. // IdP data available in authResult.additionalUserInfo.profile. // Yahoo OAuth access token can also be retrieved by: // (authResult.credential as? OAuthCredential)?.accessToken // Yahoo OAuth ID token can be retrieved by calling: // (authResult.credential as? OAuthCredential)?.idToken } } }
[provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential, NSError *_Nullable error) { if (error) { // Handle error. } if (credential) { [[FIRAuth auth] signInWithCredential:credential completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { if (error) { // Handle error. } // User is signed in. // IdP data available in authResult.additionalUserInfo.profile. // Yahoo OAuth access token can also be retrieved by: // ((FIROAuthCredential *)authResult.credential).accessToken // Yahoo OAuth ID token can be retrieved by calling: // ((FIROAuthCredential *)authResult.credential).idToken }]; } }];
باستخدام رمز الدخول عبر OAuth، يمكنك طلب Yahoo API.
على سبيل المثال، للحصول على معلومات الملف الشخصي الأساسية، يمكنك طلب واجهة برمجة تطبيقات REST، تمرير رمز الدخول في العنوان
Authorization
:https://social.yahooapis.com/v1/user/YAHOO_USER_UID/profile?format=json
يشير
YAHOO_USER_UID
إلى رقم تعريف مستخدم Yahoo الذي يمكن استرداده من الحقلAuth.auth.currentUser.providerData[0].uid
أو منauthResult.additionalUserInfo.profile
بينما تركز الأمثلة أعلاه على تدفقات تسجيل الدخول، لديك أيضًا القدرة على ربط موفر Yahoo بمستخدم حالي باستخدام
linkWithPopup
على سبيل المثال، يمكنك ربط عدة مواقع مقدمي الخدمة للمستخدم نفسه مما يسمح لهم بتسجيل الدخول باستخدام أي منهما.Auth().currentUser.link(withCredential: credential) { authResult, error in if error != nil { // Handle error. } // Yahoo credential is linked to the current user. // IdP data available in authResult.additionalUserInfo.profile. // Yahoo OAuth access token can also be retrieved by: // (authResult.credential as? OAuthCredential)?.accessToken // Yahoo OAuth ID token can be retrieved by calling: // (authResult.credential as? OAuthCredential)?.idToken }
[[FIRAuth auth].currentUser linkWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { // Handle error. } // Yahoo credential is linked to the current user. // IdP data available in authResult.additionalUserInfo.profile. // Yahoo OAuth access token is can also be retrieved by: // ((FIROAuthCredential *)authResult.credential).accessToken // Yahoo OAuth ID token can be retrieved by calling: // ((FIROAuthCredential *)authResult.credential).idToken }];
يمكن استخدام نفس النمط مع
reauthenticateWithPopup
/reauthenticateWithRedirect
التي يمكن استخدامها استرداد بيانات اعتماد جديدة للعمليات الحساسة التي تتطلب مؤخرًا تسجيل الدخول.Auth().currentUser.reauthenticateWithCredential(withCredential: credential) { authResult, error in if error != nil { // Handle error. } // User is re-authenticated with fresh tokens minted and // should be able to perform sensitive operations like account // deletion and email or password update. // IdP data available in result.additionalUserInfo.profile. // Additional OAuth access token is can also be retrieved by: // (authResult.credential as? OAuthCredential)?.accessToken // Yahoo OAuth ID token can be retrieved by calling: // (authResult.credential as? OAuthCredential)?.idToken }
[[FIRAuth auth].currentUser reauthenticateWithCredential:credential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) { if (error) { // Handle error. } // User is re-authenticated with fresh tokens minted and // should be able to perform sensitive operations like account // deletion and email or password update. // IdP data available in result.additionalUserInfo.profile. // Additional OAuth access token is can also be retrieved by: // ((FIROAuthCredential *)authResult.credential).accessToken // Yahoo OAuth ID token can be retrieved by calling: // ((FIROAuthCredential *)authResult.credential).idToken }];
التعامل مع أخطاء بيانات اعتماد الحساب المتوفّرة ومختلفة
في حال تفعيل الإعداد حساب واحد لكل عنوان بريد إلكتروني في وحدة تحكّم Firebase،
عندما يحاول المستخدم تسجيل الدخول إلى موفر خدمة (مثل Yahoo) باستخدام بريد إلكتروني
لمقدم خدمة مستخدم Firebase آخر (مثل Google)، فإن الخطأ
تم طرح FIRAuthErrorCodeAccountExistsWithDifferentCredential
مع رمز مؤقت
عنصر FIRAuthCredential
(بيانات اعتماد Yahoo). لإكمال عملية تسجيل الدخول إلى
مقدم الخدمة المقصود، يتعين على المستخدم التوقيع أولاً إلى مقدم الخدمة الحالي (Google)، ثم الربط
FIRAuthCredential
السابق (بيانات اعتماد Yahoo). وسيبدو هذا كما هو موضح أدناه:
// Sign-in with an OAuth credential. provider.getCredentialWith(nil) { credential, error in // An account with the same email already exists. if (error as NSError?)?.code == AuthErrorCode.accountExistsWithDifferentCredential.rawValue { // Get pending credential and email of existing account. let existingAcctEmail = (error! as NSError).userInfo[AuthErrorUserInfoEmailKey] as! String let pendingCred = (error! as NSError).userInfo[AuthErrorUserInfoUpdatedCredentialKey] as! AuthCredential // Lookup existing account identifier by the email. Auth.auth().fetchProviders(forEmail:existingAcctEmail) { providers, error in // Existing email/password account. if (providers?.contains(EmailAuthProviderID))! { // Existing password account for email. Ask user to provide the password of the // existing account. // Sign in with existing account. Auth.auth().signIn(withEmail:existingAcctEmail, password:password) { user, error in // Successfully signed in. if user != nil { // Link pending credential to account. Auth.auth().currentUser?.linkAndRetrieveData(with: pendingCred) { result, error in // ... } } } } } return } // Other errors. if error != nil { // handle the error. return } // Sign in with the credential. if credential != nil { Auth.auth().signInAndRetrieveData(with: credential!) { result, error in if error != nil { // handle the error. return } } } }
// Sign-in with an OAuth credential. [provider getCredentialWithUIDelegate:nil completion:^(FIRAuthCredential *_Nullable credential, NSError *_Nullable error) { // An account with the same email already exists. if (error.code == FIRAuthErrorCodeAccountExistsWithDifferentCredential) { // Get pending credential and email of existing account. NSString *existingAcctEmail = error.userInfo[FIRAuthErrorUserInfoEmailKey]; FIRAuthCredential *pendingCred = error.userInfo[FIRAuthErrorUserInfoUpdatedCredentialKey]; // Lookup existing account identifier by the email. [[FIRAuth auth] fetchProvidersForEmail:existingAcctEmail completion:^(NSArray<NSString *> *_Nullable providers, NSError *_Nullable error) { // Existing email/password account. if ( [providers containsObject:FIREmailAuthProviderID] ) { // Existing password account for email. Ask user to provide the password of the // existing account. // Sign in with existing account. [[FIRAuth auth] signInWithEmail:existingAcctEmail password:password completion:^(FIRUser *user, NSError *error) { // Successfully signed in. if (user) { // Link pending credential to account. [[FIRAuth auth].currentUser linkWithCredential:pendingCred completion:^(FIRUser *_Nullable user, NSError *_Nullable error) { // ... }]; } }]; } }]; return; } // Other errors. if (error) { // handle the error. return; } // Sign in with the credential. if (credential) { [[FIRAuth auth] signInAndRetrieveDataWithCredential:credential completion:^(FIRAuthDataResult *_Nullable authResult, NSError *_Nullable error) { if (error) { // handle the error. return; } }]; } }];
إعدادات متقدّمة: معالجة عملية تسجيل الدخول يدويًا
وعلى عكس موفِّري OAuth الآخرين الذين يتوافقون مع Firebase مثل Google وFacebook وTwitter، حيث يمكن تسجيل الدخول مباشرةً باستخدام رمز الدخول OAuth مستندة إلى بيانات الاعتماد، فإن مصادقة Firebase لا تدعم نفس الإمكانية مثل Yahoo نظرًا لعدم قدرة Firebase خادم المصادقة للتحقُّق من جمهور رموز الدخول عبر بروتوكول OAuth في Yahoo. ويعد ذلك من المتطلبات الأمنية المهمة وقد يكشف عن التطبيقات إعادة تشغيل الهجمات الإلكترونية من خلال رمز دخول Yahoo OAuth الذي تم الحصول عليه من يمكن استخدام مشروع واحد (المرفق) لتسجيل الدخول إلى مشروع آخر (الضحية). بدلاً من ذلك، توفر مصادقة Firebase إمكانية التعامل مع تدفق OAuth بالكامل تبادل رمز التفويض باستخدام معرّف وسر عميل OAuth في "وحدة تحكُّم Firebase" بما أنّه لا يمكن استخدام رمز التفويض إلّا إلى جانب معرِّف أو سر عميل أو رمز تفويض التي تم الحصول عليها لمشروع ما مع مشروع آخر.
فإذا كان يلزم استخدام هؤلاء المزودين في بيئات غير متوافقة، ومكتبة OAuth التابعة لجهة خارجية مصادقة Firebase المخصَّصة استخدامها. يجب استخدام بروتوكول المصادقة الأول للمصادقة مع مقدِّم الخدمة. وأخيرًا استبدال بيانات اعتماد موفر الخدمة برمز مخصص.
الخطوات التالية
بعد تسجيل المستخدم الدخول لأول مرة، يتم إنشاء حساب مستخدم جديد المرتبطة ببيانات الاعتماد - أي اسم المستخدم وكلمة المرور، أو الرقم أو معلومات مقدم المصادقة - المستخدم الذي سجّل الدخول باستخدامه. هذا الجديد كجزء من مشروع Firebase، ويمكن استخدامه لتحديد مستخدم في كل تطبيق في مشروعك، بغض النظر عن كيفية تسجيل دخوله.
-
في تطبيقاتك، يمكنك الحصول على معلومات الملف الشخصي الأساسية للمستخدم من
User
. راجع إدارة المستخدمين. في Firebase Realtime Database وCloud Storage قواعد الأمان، تتيح لك الحصول على معرّف المستخدم الفريد للمستخدم الذي سجّل الدخول من المتغير
auth
، واستخدامها للتحكم في البيانات التي يمكن للمستخدم الوصول إليها
يمكنك السماح للمستخدمين بتسجيل الدخول إلى تطبيقك باستخدام طرق مصادقة متعددة. موفِّري خدمة المصادقة من خلال ربط بيانات اعتماد موفر المصادقة حساب مستخدم حالي
لتسجيل خروج مستخدم، اتصل بالرقم
signOut:
let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() } catch let signOutError as NSError { print("Error signing out: %@", signOutError) }
NSError *signOutError; BOOL status = [[FIRAuth auth] signOut:&signOutError]; if (!status) { NSLog(@"Error signing out: %@", signOutError); return; }
قد تحتاج أيضًا إلى إضافة رمز معالجة الأخطاء للنطاق الكامل للمصادقة الأخطاء. يُرجى الاطّلاع على التعامل مع الأخطاء.