Firebase Auth & Realtime Database Apps · درس

ربط الحسابات وإدارة مزوّدي المصادقة

مكّن المستخدم من امتلاك حساب واحد عبر عدة طرق لتسجيل الدخول بربط مزوّدي المصادقة وإلغاء ربطهم، ومعالجة التعارضات، وإدارة بيانات الاعتماد المرتبطة بأمان.

الدرس 4 من 413 خطوة

ربط الحسابات وإدارة مزوّدي المصادقة درس مجاني في Firebase Auth & Realtime Database Apps على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Firebase Auth & Realtime Database Apps، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Firebase Auth & Realtime Database Apps 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

The Multi-Provider Problem

The same person may sign in with email/password today and Google tomorrow. Without linking, Firebase could treat these as two separate accounts.

Account linking unifies multiple sign-in methods under one Firebase user (one uid).

How Linking Works

Linking attaches an additional credential to the currently signed-in user. The user keeps the same uid and data, but gains a new way to log in.

  • Email/password can be linked to a social account
  • Multiple social providers can coexist

Linking a Provider

Use linkWithPopup on the current user to add another provider interactively.

import { getAuth, GoogleAuthProvider, linkWithPopup } from 'firebase/auth';

const user = getAuth().currentUser;
await linkWithPopup(user, new GoogleAuthProvider());
console.log('Google linked to existing account');

Linking a Credential Directly

When you already hold a credential (for example email/password the user just typed), use linkWithCredential.

import { EmailAuthProvider, linkWithCredential } from 'firebase/auth';

const cred = EmailAuthProvider.credential(email, password);
await linkWithCredential(getAuth().currentUser, cred);

The Collision Error

If the new provider's email already belongs to a different account, Firebase throws auth/account-exists-with-different-credential. This is the key case you must handle.

Resolving a Collision

To resolve it: read the conflicting email, ask the user to sign in with the existing provider, then link the new credential onto that account.

import { fetchSignInMethodsForEmail } from 'firebase/auth';

const methods = await fetchSignInMethodsForEmail(auth, email);
// prompt user to sign in with methods[0], then link pendingCred

Inspecting Linked Providers

The user object exposes providerData, an array describing every linked provider. Use it to render a 'connected accounts' settings screen.

const user = getAuth().currentUser;
user.providerData.forEach(p => console.log(p.providerId));

Unlinking a Provider

Let users disconnect a method with unlink, passing the provider ID. Always keep at least one sign-in method so the account stays accessible.

import { unlink } from 'firebase/auth';

await unlink(getAuth().currentUser, 'google.com');

Guarding the Last Provider

Before unlinking, check that more than one provider remains. Removing the only method would orphan the user.

const user = getAuth().currentUser;
if (user.providerData.length <= 1) {
  showError('You must keep at least one sign-in method.');
  return;
}

Reauthentication for Sensitive Changes

Linking or unlinking is a sensitive operation. If the user's session is old, Firebase may require recent login and throw auth/requires-recent-login. Reauthenticate before retrying.

import { reauthenticateWithPopup } from 'firebase/auth';

await reauthenticateWithPopup(user, new GoogleAuthProvider());

Design Considerations

Plan your identity model up front:

  • Treat email as the unifying key when possible
  • Surface linked accounts in user settings
  • Always handle the collision error gracefully

Quick Check

Test your understanding of account linking.

Recap

You can now unify identities across providers.

  • Link with linkWithPopup or linkWithCredential
  • Handle account-exists-with-different-credential
  • Inspect providerData and unlink safely
  • Always keep at least one provider
  • Reauthenticate for sensitive changes
البدء مجانًا

تعلم Firebase Auth & Realtime Database Apps مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
11
الدروس
44

الأسئلة الشائعة

هل درس «ربط الحسابات وإدارة مزوّدي المصادقة» مجاني؟

نعم — نص درس «ربط الحسابات وإدارة مزوّدي المصادقة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Firebase Auth & Realtime Database Apps، انتقل إلى CoddyKit PRO. تتضمن دورة Firebase Auth & Realtime Database Apps 4 دروس في المجموع.

ماذا ستتعلم في «ربط الحسابات وإدارة مزوّدي المصادقة»؟

مكّن المستخدم من امتلاك حساب واحد عبر عدة طرق لتسجيل الدخول بربط مزوّدي المصادقة وإلغاء ربطهم، ومعالجة التعارضات، وإدارة بيانات الاعتماد المرتبطة بأمان. تتمرن على Firebase Auth & Realtime Database Apps مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Firebase Auth & Realtime Database Apps؟

لا تُشترط خبرة سابقة. Firebase Auth & Realtime Database Apps على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «ربط الحسابات وإدارة مزوّدي المصادقة»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Firebase Auth & Realtime Database Apps هذا؟

نعم. كل درس في Firebase Auth & Realtime Database Apps يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. المصادقة برقم الهاتف
  2. المصادقة متعددة العوامل (MFA)
  3. المطالبات المخصصة وقواعد الأمان
  4. ربط الحسابات وإدارة مزوّدي المصادقة
← العودة إلى Firebase Auth & Realtime Database Apps