0Pricing
Firebase Auth & Realtime Database Apps · Lekcja

Łączenie kont i zarządzanie dostawcami

Pozwól jednemu użytkownikowi korzystać z jednego konta za pomocą wielu metod logowania, łącząc i odłączając dostawców uwierzytelniania, obsługując kolizje i bezpiecznie zarządzając powiązanymi danymi logowania.

Łączenie kont i zarządzanie dostawcami to bezpłatna lekcja Firebase Auth & Realtime Database Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Firebase Auth & Realtime Database Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Firebase Auth & Realtime Database Apps zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Łączenie kont i zarządzanie dostawcami” jest bezpłatna?

Tak — pełny tekst „Łączenie kont i zarządzanie dostawcami” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Firebase Auth & Realtime Database Apps, przejdź na CoddyKit PRO. Kurs Firebase Auth & Realtime Database Apps zawiera 4 lekcji w sumie.

Co nauczysz się w „Łączenie kont i zarządzanie dostawcami”?

Pozwól jednemu użytkownikowi korzystać z jednego konta za pomocą wielu metod logowania, łącząc i odłączając dostawców uwierzytelniania, obsługując kolizje i bezpiecznie zarządzając powiązanymi danymi… Ćwiczysz Firebase Auth & Realtime Database Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Firebase Auth & Realtime Database Apps?

Nie wymagamy żadnego doświadczenia. Firebase Auth & Realtime Database Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Łączenie kont i zarządzanie dostawcami”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Firebase Auth & Realtime Database Apps?

Tak. Każda lekcja Firebase Auth & Realtime Database Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Uwierzytelnianie numerem telefonu
  2. Uwierzytelnianie wieloskładnikowe (MFA)
  3. Niestandardowe claims i reguły bezpieczeństwa
  4. Łączenie kont i zarządzanie dostawcami
← Powrót do Firebase Auth & Realtime Database Apps