linkWithCredential method

Future<UserCredential> linkWithCredential(
  1. AuthCredential credential
)

Links the user account with the given credentials.

A FirebaseAuthException maybe thrown with the following error code:

  • provider-already-linked:
  • Thrown if the provider has already been linked to the user. This error is thrown even if this is not the same provider's account that is currently linked to the user.
  • invalid-credential:
  • Thrown if the provider's credential is not valid. This can happen if it has already expired when calling link, or if it used invalid token(s). See the Firebase documentation for your provider, and make sure you pass in the correct parameters to the credential method.
  • credential-already-in-use:
  • Thrown if the account corresponding to the credential already exists among your users, or is already linked to a Firebase User. For example, this error could be thrown if you are upgrading an anonymous user to a Google user by linking a Google credential to it and the Google credential used is already associated with an existing Firebase Google user. The fields email, phoneNumber, and credential (AuthCredential) may be provided, depending on the type of credential. You can recover from this error by signing in with credential directly via signInWithCredential. Please note, you will not recover from this error if you're using a PhoneAuthCredential to link a provider to an account. Once an attempt to link an account has been made, a new sms code is required to sign in the user.
  • email-already-in-use:
  • Thrown if the email corresponding to the credential already exists among your users. When thrown while linking a credential to an existing user, an email and credential (AuthCredential) fields are also provided. You have to link the credential to the existing user with that email if you wish to continue signing in with that credential. To do so, call fetchSignInMethodsForEmail, sign in to email via one of the providers returned and then User.linkWithCredential the original credential to that newly signed in user.
  • operation-not-allowed:
  • Thrown if you have not enabled the provider in the Firebase Console. Go to the Firebase Console for your project, in the Auth section and the Sign in Method tab and configure the provider.
  • invalid-email:
  • Thrown if the email used in a EmailAuthProvider.credential is invalid.
  • invalid-email:
  • Thrown if the password used in a EmailAuthProvider.credential is not correct or when the user associated with the email does not have a password.
  • invalid-verification-code:
  • Thrown if the credential is a PhoneAuthProvider.credential and the verification code of the credential is not valid.
  • invalid-verification-id:
  • Thrown if the credential is a PhoneAuthProvider.credential and the verification ID of the credential is not valid.

Implementation

Future<UserCredential> linkWithCredential(AuthCredential credential) async {
  try {
    return UserCredential._(
      _auth,
      await _delegate.linkWithCredential(credential),
    );
  } on FirebaseAuthMultiFactorExceptionPlatform catch (e) {
    throw FirebaseAuthMultiFactorException._(_auth, e);
  } catch (e) {
    rethrow;
  }
}