Collegamento di più provider a un account

Questo documento mostra come collegare più provider a un singolo account Identity Platform.

Identity Platform utilizza un ID univoco per identificare gli utenti. In questo modo, gli utenti possono accedere allo stesso account con provider diversi. Ad esempio, un utente che inizialmente si è registrato con un numero di telefono potrebbe in un secondo momento collegare il proprio Account Google e quindi utilizzare uno dei due metodi per accedere.

Prima di iniziare

Aggiungi il supporto per due o più provider di identità alla tua app.

Attivazione o disattivazione del collegamento degli account

L'impostazione di collegamento degli account determina il modo in cui Identity Platform gestisce gli utenti che tentano di accedere con lo stesso indirizzo email utilizzando provider diversi.

  • Collega gli account che utilizzano lo stesso indirizzo email: Identity Platform genererà un errore se un utente tenta di accedere con un indirizzo email già in uso. La tua app può rilevare questo errore e collegare il nuovo provider all'account esistente.

  • Crea più account per ogni provider di identità: verrà creato un nuovo account utente Identity Platform ogni volta che un utente accede con un provider diverso.

Per scegliere un'impostazione:

  1. Vai alla pagina Impostazioni di Identity Platform nella consoleGoogle Cloud .

    Vai alla pagina Impostazioni

  2. Seleziona un'impostazione in Collegamento degli account utente.

  3. Fai clic su Salva.

Collegamento delle credenziali del provider federato

Per collegare le credenziali di un provider federato:

  1. Consenti all'utente di accedere con qualsiasi provider o metodo di autenticazione.

  2. Recupera l'oggetto provider corrispondente al provider che vuoi collegare all'account dell'utente. Ad esempio:

    Web version 9

    import { GoogleAuthProvider, FacebookAuthProvider, TwitterAuthProvider, GithubAuthProvider } from "firebase/auth";
    
    const googleProvider = new GoogleAuthProvider();
    const facebookProvider = new FacebookAuthProvider();
    const twitterProvider = new TwitterAuthProvider();
    const githubProvider = new GithubAuthProvider();

    Web version 8

    var googleProvider = new firebase.auth.GoogleAuthProvider();
    var facebookProvider = new firebase.auth.FacebookAuthProvider();
    var twitterProvider = new firebase.auth.TwitterAuthProvider();
    var githubProvider = new firebase.auth.GithubAuthProvider();
  3. Chiedi all'utente di accedere con il provider che vuoi collegare. Puoi aprire una finestra popup o reindirizzare la pagina corrente. Il reindirizzamento è più semplice per gli utenti di dispositivi mobili.

    Per mostrare un popup, chiama linkWithPopup():

    Web version 9

    import { getAuth, linkWithPopup, GoogleAuthProvider } from "firebase/auth";
    const provider = new GoogleAuthProvider();
    
    const auth = getAuth();
    linkWithPopup(auth.currentUser, provider).then((result) => {
      // Accounts successfully linked.
      const credential = GoogleAuthProvider.credentialFromResult(result);
      const user = result.user;
      // ...
    }).catch((error) => {
      // Handle Errors here.
      // ...
    });

    Web version 8

    auth.currentUser.linkWithPopup(provider).then((result) => {
      // Accounts successfully linked.
      var credential = result.credential;
      var user = result.user;
      // ...
    }).catch((error) => {
      // Handle Errors here.
      // ...
    });

    Per reindirizzare la pagina, chiama prima linkWithRedirect():

    Segui le best practice quando utilizzi signInWithRedirect, linkWithRedirect o reauthenticateWithRedirect.

    Web version 9

    import { getAuth, linkWithRedirect, GoogleAuthProvider } from "firebase/auth";
    const provider = new GoogleAuthProvider();
    
    const auth = getAuth();
    linkWithRedirect(auth.currentUser, provider)
      .then(/* ... */)
      .catch(/* ... */);

    Web version 8

    auth.currentUser.linkWithRedirect(provider)
      .then(/* ... */)
      .catch(/* ... */);

    Dopo l'accesso, l'utente verrà reindirizzato alla tua app. A questo punto, puoi recuperare il risultato dell'accesso chiamando getRedirectResult():

    Web version 9

    import { getRedirectResult } from "firebase/auth";
    getRedirectResult(auth).then((result) => {
      const credential = GoogleAuthProvider.credentialFromResult(result);
      if (credential) {
        // Accounts successfully linked.
        const user = result.user;
        // ...
      }
    }).catch((error) => {
      // Handle Errors here.
      // ...
    });

    Web version 8

    auth.getRedirectResult().then((result) => {
      if (result.credential) {
        // Accounts successfully linked.
        var credential = result.credential;
        var user = result.user;
        // ...
      }
    }).catch((error) => {
      // Handle Errors here.
      // ...
    });

L'account dell'utente presso il provider federato è ora collegato al suo account Identity Platform e può utilizzare il provider per accedere.

Collegamento delle credenziali di email e password

Per aggiungere un indirizzo email e una password a un account utente esistente:

  1. Consenti all'utente di accedere con qualsiasi provider di identità o metodo.

  2. Chiedi all'utente di inserire un indirizzo email e una password.

  3. Crea un oggetto AuthCredential con l'indirizzo email e la password:

    Web version 9

    import { EmailAuthProvider } from "firebase/auth";
    
    const credential = EmailAuthProvider.credential(email, password);

    Web version 8

    var credential = firebase.auth.EmailAuthProvider.credential(email, password);
  4. Passa l'oggetto AuthCredential al metodo linkWithCredential() sull'utente che ha eseguito l'accesso:

    Web version 9

    import { getAuth, linkWithCredential } from "firebase/auth";
    
    const auth = getAuth();
    linkWithCredential(auth.currentUser, credential)
      .then((usercred) => {
        const user = usercred.user;
        console.log("Account linking success", user);
      }).catch((error) => {
        console.log("Account linking error", error);
      });

    Web version 8

    auth.currentUser.linkWithCredential(credential)
      .then((usercred) => {
        var user = usercred.user;
        console.log("Account linking success", user);
      }).catch((error) => {
        console.log("Account linking error", error);
      });

Le credenziali email e password sono ora collegate all'account Identity Platform dell'utente, che può utilizzarle per accedere.

Tieni presente che una credenziale di un provider federato può essere collegata a un account email/password con un'email diversa. In questo caso, l'email corrispondente al provider federato può essere utilizzata per creare un account email/password separato.

Gestione dell'errore account-exists-with-different-credential

Se hai attivato l'impostazione Collega gli account che utilizzano lo stesso indirizzo email nella consoleGoogle Cloud , quando un utente tenta di accedere a un provider con un indirizzo email già esistente per un altro provider (ad esempio Google), l'operazione non va a buon fine e viene generato l'errore auth/account-exists-with-different-credential e un oggetto AuthCredential.

Per gestire questo errore, chiedi all'utente di accedere con il provider esistente. Poi, chiama il numero linkWithCredential(), linkWithPopup() o linkWithRedirect() per associare il nuovo fornitore al suo account utilizzando il AuthCredential.

L'esempio seguente mostra come gestire questo errore quando un utente tenta di accedere utilizzando Facebook:

Web version 9

  import { signInWithPopup, signInWithEmailAndPassword, linkWithCredential, FacebookAuthProvider } from "firebase/auth";

  // User tries to sign in with Facebook.
  signInWithPopup(auth, facebookProvider).catch((error) => {
  // User's email already exists.
  if (error.code === 'auth/account-exists-with-different-credential') {
    // The pending Facebook credential.
    const pendingCred = FacebookAuthProvider.credentialFromError(error);
    // The provider account's email address.
    const email = error.customData.email;

    // Present the user with a list of providers they might have
    // used to create the original account.
    // Then, ask the user to sign in with the existing provider.
    const method = promptUserForSignInMethod();

    if (method === 'password') {
      // TODO: Ask the user for their password.
      // In real scenario, you should handle this asynchronously.
      const password = promptUserForPassword();
      signInWithEmailAndPassword(auth, email, password).then((result) => {
        return linkWithCredential(result.user, pendingCred);
      }).then(() => {
        // Facebook account successfully linked to the existing user.
        goToApp();
      });
      return;
    }

    // All other cases are external providers.
    // Construct provider object for that provider.
    // TODO: Implement getProviderForProviderId.
    const provider = getProviderForProviderId(method);
    // At this point, you should let the user know that they already have an
    // account with a different provider, and validate they want to sign in
    // with the new provider.
    // Note: Browsers usually block popups triggered asynchronously, so in
    // real app, you should ask the user to click on a "Continue" button
    // that will trigger signInWithPopup().
    signInWithPopup(auth, provider).then((result) => {
      // Note: Identity Platform doesn't control the provider's sign-in
      // flow, so it's possible for the user to sign in with an account
      // with a different email from the first one.

      // Link the Facebook credential. We have access to the pending
      // credential, so we can directly call the link method.
      linkWithCredential(result.user, pendingCred).then((userCred) => {
        // Success.
        goToApp();
      });
    });
  }
});

Web version 8

  // User tries to sign in with Facebook.
      auth.signInWithPopup(facebookProvider).catch((error) => {
  // User's email already exists.
  if (error.code === 'auth/account-exists-with-different-credential') {
    // The pending Facebook credential.
    const pendingCred = error.credential;
    // The provider account's email address.
    const email = error.email;

    // Present the user with a list of providers they might have
    // used to create the original account.
    // Then, ask the user to sign in with the existing provider.
    const method = promptUserForSignInMethod();

    if (method === 'password') {
      // TODO: Ask the user for their password.
      // In real scenario, you should handle this asynchronously.
      const password = promptUserForPassword();
      auth.signInWithEmailAndPassword(email, password).then((result) => {
        return result.user.linkWithCredential(pendingCred);
      }).then(() => {
        // Facebook account successfully linked to the existing user.
        goToApp();
      });
      return;
    }

    // All other cases are external providers.
    // Construct provider object for that provider.
    // TODO: Implement getProviderForProviderId.
    const provider = getProviderForProviderId(method);
    // At this point, you should let the user know that they already have an
    // account with a different provider, and validate they want to sign in
    // with the new provider.
    // Note: Browsers usually block popups triggered asynchronously, so in
    // real app, you should ask the user to click on a "Continue" button
    // that will trigger signInWithPopup().
    auth.signInWithPopup(provider).then((result) => {
      // Note: Identity Platform doesn't control the provider's sign-in
      // flow, so it's possible for the user to sign in with an account
      // with a different email from the first one.

      // Link the Facebook credential. We have access to the pending
      // credential, so we can directly call the link method.
      result.user.linkWithCredential(pendingCred).then((userCred) => {
        // Success.
        goToApp();
      });
    });
  }
});

L'utilizzo di un reindirizzamento è simile a un popup, con la differenza che dovrai memorizzare nella cache la credenziale in attesa tra i reindirizzamenti di pagina (ad esempio, utilizzando lo spazio di archiviazione della sessione).

Tieni presente che alcuni provider, come Google e Microsoft, fungono sia da provider di identità email che social. I provider email sono considerati autorevoli per tutti gli indirizzi correlati al dominio email ospitato. Ciò significa che un utente che accede con un indirizzo email ospitato dallo stesso provider non genererà mai questo errore (ad esempio, l'accesso con Google utilizzando un'email @gmail.com o con Microsoft utilizzando un'email @live.com o @outlook.com).

L'accesso con un provider attendibile (come SAML) non genera un errore auth/account-exists-with-different-credential. Se l'account esistente ha un'email verificata, Identity Platform collega automaticamente il nuovo provider all'account esistente e conserva le credenziali esistenti. Se l'account esistente ha un indirizzo email non verificato (ad esempio un account con indirizzo email o password non verificati), il provider attendibile sovrascrive l'account non verificato e rimuove la password esistente e le credenziali del provider non verificate per impedire il takeover dell'account.

Unione manuale degli account

Se un utente tenta di accedere con credenziali già collegate a un altro account utente utilizzando lo stesso provider, i metodi integrati dell'SDK client per il collegamento degli account non andranno a buon fine. In questo caso, dovrai unire gli account manualmente e poi eliminare il secondo account. Ad esempio:

Web version 9

// Sign in first account.
const result1 = await signInWithCredential(auth, cred1);
const user1 = result1.user;
// Try to link a credential that belongs to an existing account
try {
  await linkWithCredential(user1, cred2);
} catch (error) {
  // cred2 already exists so an error is thrown.
  const result2 = await signInWithCredential(auth, error.credential);
  const user2 = result2.user;
  // Merge the data.
  mergeData(user1, user2);
  // Delete one of the accounts, and try again.
  await user2.delete();
  // Linking now will work.
  await linkWithCredential(user1, result2.credential);
}

Web version 8

// Sign in first account.
const result1 = await auth.signInWithCredential(cred1);
const user1 = result1.user;
// Try to link a credential that belongs to an existing account
try {
  await user1.linkWithCredential(cred2);
} catch (error) {
  // cred2 already exists so an error is thrown.
  const result2 = await auth.signInWithCredential(error.credential);
  const user2 = result2.user;
  // Merge the data.
  mergeData(user1, user2);
  // Delete one of the accounts, and try again.
  await user2.delete();
  // Linking now will work.
  await user1.linkWithCredential(result2.credential);
}

Puoi scollegare un provider dall'account di un utente. L'utente non potrà più eseguire l'autenticazione con quel provider.

Per scollegare un provider, passa l'ID provider al metodo unlink(). Puoi ottenere gli ID provider dei provider di autenticazione collegati a un utente dalla proprietà providerData.

Web version 9

import { getAuth, unlink } from "firebase/auth";

const auth = getAuth();
unlink(auth.currentUser, providerId).then(() => {
  // Auth provider unlinked from account
  // ...
}).catch((error) => {
  // An error happened
  // ...
});

Web version 8

user.unlink(providerId).then(() => {
  // Auth provider unlinked from account
  // ...
}).catch((error) => {
  // An error happened
  // ...
});

Passaggi successivi