المصادقة من خلال Firebase على أنظمة Apple الأساسية باستخدام نظام مصادقة مخصّص
تنظيم صفحاتك في مجموعات
يمك��ك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يمكنك دمج Firebase Authentication مع نظام مصادقة مخصّص من خلال تعديل خادم المصادقة لإنشاء رموز مميّزة موقَّعة مخصّصة عند تسجيل دخول أحد المستخدمين بنجاح. يتلقّى تطبيقك هذا الرمز المميّز ويستخدمه للمصادقة
مع Firebase.
انقر على إنشاء مفتاح خاص جديد في أسفل القسم
حزمة تطوير البرامج (SDK) للمشرف في Firebase ضمن صفحة حسابات الخدمة.
يتم تلقائيًا
حفظ زوج المفتاح العام/الخاص لحساب الخدمة الجديد على جهاز الكمبيوتر. انسخ هذا الملف إلى خادم المصادقة.
المصادقة باستخدام Firebase
استورِد وحدة FirebaseCore في
UIApplicationDelegate، بالإضافة إلى أي
وحدات Firebase أخرى يستخدمها مفوّض تطبيقك.
على سبيل المثال، لاستخدام Cloud Firestore وAuthentication:
يمكنك ضبط مثيل مشترَك لملف FirebaseApp
في طريقة
application(_:didFinishLaunchingWithOptions:) لمفوّض التطبيق باتّباع ال��طوات التالية:
SwiftUI
// Use Firebase library to configure APIsFirebaseApp.configure()
Swift
// Use Firebase library to configure APIsFirebaseApp.configure()
Objective-C
// Use Firebase library to configure APIs[FIRAppconfigure];
إذا كنت تستخدم SwiftUI، عليك إنشاء عنصر نائب للتطبيق وإرفاقه
ببنية App من خلال UIApplicationDelegateAdaptor أو
NSApplicationDelegateAdaptor. عليك أيضًا إيقاف عملية تبديل مفوّض التطبيق. لمزيد من المعلومات، يمكنك الاطّلاع على تعليمات SwiftUI.
SwiftUI
@mainstructYourApp:App{// register app delegate for Firebase setup@UIApplicationDelegateAdaptor(AppDelegate.self)vardelegatevarbody:someScene{WindowGroup{NavigationView{ContentView()}}}}
عندما يسجّل المستخدمون الدخول إلى تطبيقك، أرسِل بيانات اعتماد تسجيل الدخول (مثل
اسم المستخدم وكلمة المرور) إلى خادم المصادقة. يفحص
الخادم بيانات الاعتماد ويعرض
رمزًا مميّزًا مخصّصًا
إذا كانت صالحة.
بعد استلام الرمز المميّز المخصّص من خادم المصادقة، مرِّره
إلى signInWithCustomToken لتسجيل دخول المستخدم:
بعد أن يسجِّل المستخدم الدخول لأول مرة، يتم إنشاء حساب مستخدم جديد وربط�� ببيانات الاعتماد التي استخدمها المستخدم لتسجيل الدخول، أي اسم المستخدم وكلمة المرور أو رقم الهاتف أو معلومات مقدّم خدمة المصادقة. يتم تخزين
هذا الحساب الجديد كجزء من مشروعك على Firebase، ويمكن استخدامه لتحديد هوية
مستخدم في كل تطبيق في مشروعك، بغض النظر عن كيفية تسجيل دخول المستخدم.
في تطبيقاتك، يمكنك الحصول على معلومات الملف الشخصي الأساسية للمستخدم من العنصر
User. راجِع إدارة المستخدمين.
في Firebase Realtime Database وCloud Storageقواعد الأمان، يمكنك
الحصول على معرّف المستخدم الفريد للمستخدم الذي سجّل الدخول من متغيّر auth،
واستخدامه للتحكّم في البيانات التي يمكن للمستخدم الوصول إليها.
تاريخ التعديل الأخير: 2025-08-16 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-08-16 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["You can integrate Firebase Authentication with a custom authentication system by\nmodifying your authentication server to produce custom signed tokens when a user\nsuccessfully signs in. Your app receives this token and uses it to authenticate\nwith Firebase.\n\nBefore you begin\n\n1. [Create a Firebase project and register your app](/docs/ios/setup) if you haven't already.\n2. Use Swift Package Manager to install and manage Firebase dependencies.\n\n | Visit [our installation guide](/docs/ios/installation-methods) to learn about the different ways you can add Firebase SDKs to your Apple project, including importing frameworks directly and using CocoaPods.\n 1. In Xcode, with your app project open, navigate to **File \\\u003e Add Packages**.\n 2. When prompted, add the Firebase Apple platforms SDK repository: \n\n ```text\n https://github.com/firebase/firebase-ios-sdk.git\n ```\n | **Note:** New projects should use the default (latest) SDK version, but you can choose an older version if needed.\n 3. Choose the Firebase Authentication library.\n 4. Add the `-ObjC` flag to the *Other Linker Flags* section of your target's build settings.\n 5. When finished, Xcode will automatically begin resolving and downloading your dependencies in the background.\n3. Get your project's server keys:\n 1. Go to the [Service Accounts](https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk) page in your project's settings.\n 2. Click *Generate New Private Key* at the bottom of the *Firebase Admin SDK* section of the *Service Accounts* page.\n 3. The new service account's public/private key pair is automatically saved on your computer. Copy this file to your authentication server.\n\nAuthenticate with Firebase\n\n1. Import the `FirebaseCore` module in your `UIApplicationDelegate`, as well as any other [Firebase modules](/docs/ios/setup#available-pods) your app delegate uses. For example, to use Cloud Firestore and Authentication: \n\n SwiftUI \n\n ```swift\n import SwiftUI\n import FirebaseCore\n import FirebaseFirestore\n import FirebaseAuth\n // ...\n \n ```\n\n Swift \n\n ```swift\n import FirebaseCore\n import FirebaseFirestore\n import FirebaseAuth\n // ...\n \n ```\n\n Objective-C \n\n ```objective-c\n @import FirebaseCore;\n @import FirebaseFirestore;\n @import FirebaseAuth;\n // ...\n \n ```\n2. Configure a [`FirebaseApp`](/docs/reference/swift/firebasecore/api/reference/Classes/FirebaseApp) shared instance in your app delegate's `application(_:didFinishLaunchingWithOptions:)` method: \n\n SwiftUI \n\n ```swift\n // Use Firebase library to configure APIs\n FirebaseApp.configure()\n ```\n\n Swift \n\n ```swift\n // Use Firebase library to configure APIs\n FirebaseApp.configure()\n ```\n\n Objective-C \n\n ```objective-c\n // Use Firebase library to configure APIs\n [FIRApp configure];\n ```\n3. If you're using SwiftUI, you must create an application delegate and attach it to your `App` struct via `UIApplicationDelegateAdaptor` or `NSApplicationDelegateAdaptor`. You must also disable app delegate swizzling. For more information, see the [SwiftUI instructions](/docs/ios/learn-more#swiftui). \n\n SwiftUI \n\n ```swift\n @main\n struct YourApp: App {\n // register app delegate for Firebase setup\n @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate\n\n var body: some Scene {\n WindowGroup {\n NavigationView {\n ContentView()\n }\n }\n }\n }\n \n ```\n4. When users sign in to your app, send their sign-in credentials (for example, their username and password) to your authentication server. Your server checks the credentials and returns a [custom token](/docs/auth/admin/create-custom-tokens) if they are valid.\n5. After you receive the custom token from your authentication server, pass it to `signInWithCustomToken` to sign in the user: \n\n Swift \n\n ```swift\n Auth.auth().signIn(withCustomToken: customToken ?? \"\") { user, error in\n // ...\n }\n ```\n\n Objective-C \n\n ```objective-c\n [[FIRAuth auth] signInWithCustomToken:customToken\n completion:^(FIRAuthDataResult * _Nullable authResult,\n NSError * _Nullable error) {\n // ...\n }];\n ```\n\nNext steps\n\nAfter a user signs in for the first time, a new user account is created and\nlinked to the credentials---that is, the user name and password, phone\nnumber, or auth provider information---the user signed in with. This new\naccount is stored as part of your Firebase project, and can be used to identify\na user across every app in your project, regardless of how the user signs in.\n\n- In your apps, you can get the user's basic profile information from the\n [`User`](/docs/reference/ios/firebaseauth/interface_f_i_r_user) object. See [Manage Users](/docs/auth/ios/manage-users).\n\n- In your Firebase Realtime Database and Cloud Storage\n [Security Rules](/docs/database/security/user-security), you can\n get the signed-in user's unique user ID from the `auth` variable,\n and use it to control what data a user can access.\n\nYou can allow users to sign in to your app using multiple authentication\nproviders by [linking auth provider credentials to an\nexisting user account.](/docs/auth/ios/account-linking)\n\nTo sign out a user, call [`signOut:`](/docs/reference/ios/firebaseauth/interface_f_i_r_auth#ab0d5111f05c3f1906243852cc8ef41b1). \n\nSwift \n\n```swift\nlet firebaseAuth = Auth.auth()\ndo {\n try firebaseAuth.signOut()\n} catch let signOutError as NSError {\n print(\"Error signing out: %@\", signOutError)\n}\n```\n\nObjective-C \n\n```objective-c\nNSError *signOutError;\nBOOL status = [[FIRAuth auth] signOut:&signOutError];\nif (!status) {\n NSLog(@\"Error signing out: %@\", signOutError);\n return;\n}\n```\n\nYou may also want to add error handling code for the full range of authentication\nerrors. See [Handle Errors](/docs/auth/ios/errors)."]]