0Pricing
Firebase Auth & Realtime Database Apps · Lezione

Collegamento degli account e gestione dei provider

Permetta a un utente di avere un unico account con più metodi di accesso collegando e scollegando i provider di autenticazione, gestendo i conflitti e amministrando in sicurezza le credenziali collegate.

Collegamento degli account e gestione dei provider è una lezione Firebase Auth & Realtime Database Apps gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Firebase Auth & Realtime Database Apps, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Firebase Auth & Realtime Database Apps include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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

Domande Frequenti

La lezione «Collegamento degli account e gestione dei provider» è gratuita?

Sì — il testo completo di «Collegamento degli account e gestione dei provider» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Firebase Auth & Realtime Database Apps, passa a CoddyKit PRO. Il corso Firebase Auth & Realtime Database Apps include 4 lezioni in totale.

Cosa imparerò in «Collegamento degli account e gestione dei provider»?

Permetta a un utente di avere un unico account con più metodi di accesso collegando e scollegando i provider di autenticazione, gestendo i conflitti e amministrando in sicurezza le credenziali colleg… Eserciti Firebase Auth & Realtime Database Apps con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Firebase Auth & Realtime Database Apps?

Non è richiesta alcuna esperienza precedente. Firebase Auth & Realtime Database Apps su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Collegamento degli account e gestione dei provider»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Firebase Auth & Realtime Database Apps?

Sì. Ogni lezione Firebase Auth & Realtime Database Apps include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Autenticazione tramite numero di telefono
  2. Autenticazione a più fattori (MFA)
  3. Custom claims e regole di sicurezza
  4. Collegamento degli account e gestione dei provider
← Torna a Firebase Auth & Realtime Database Apps