错误处理

Firebase Authentication SDK 提供了一种简单的方法来捕获使用身份验证方法时可能出现的各种错误。适用于 Flutter 的 SDK 通过 FirebaseAuthException 类公开这些错误。

系统至少会提供 codemessage,但在某些情况下,还会提供电子邮件地址和凭据等其他属性。例如,如果用户尝试使用电子邮件地址和密码登录,则可以明确捕获系统抛出的任何错误:

try {
  await FirebaseAuth.instance.signInWithEmailAndPassword(
    email: "barry.allen@example.com",
    password: "SuperSecretPassword!"
  );
} on FirebaseAuthException catch  (e) {
  print('Failed with error code: ${e.code}');
  print(e.message);
}

每种方法都会提供各种错误代码和消息,具体取决于身份验证调用类型。Reference API 提供了有关每个方法的错误的最新详细信息。

如果您达到 Firebase Authentication 配额或者尚未启用特定的身份验证提供方,则系统可能会抛出 too-many-requestsoperation-not-allowed 等其他错误。

处理 account-exists-with-different-credential 错误

如果您在 Firebase 控制台中启用了“每个电子邮件地址一个帐号”设置,当用户尝试使用一个 Firebase 用户的提供方服务(例如 Facebook)中已存在的电子邮件地址登录另一个提供方服务(例如 Google)时,系统会抛出 auth/account-exists-with-different-credential 错误及 AuthCredential 类(Google ID 令牌)。如需登录到所需的提供方服务,用户必须先登录到现有提供方服务(例如 Facebook),然后关联至之前的 AuthCredential(Google ID 令牌)。

FirebaseAuth auth = FirebaseAuth.instance;

// Create a credential from a Google Sign-in Request
var googleAuthCredential = GoogleAuthProvider.credential(accessToken: 'xxxx');

try {
  // Attempt to sign in the user in with Google
  await auth.signInWithCredential(googleAuthCredential);
} on FirebaseAuthException catch (e) {
  if (e.code == 'account-exists-with-different-credential') {
    // The account already exists with a different credential
    String email = e.email;
    AuthCredential pendingCredential = e.credential;

    // Fetch a list of what sign-in methods exist for the conflicting user
    List<String> userSignInMethods = await auth.fetchSignInMethodsForEmail(email);

    // If the user has several sign-in methods,
    // the first method in the list will be the "recommended" method to use.
    if (userSignInMethods.first == 'password') {
      // Prompt the user to enter their password
      String password = '...';

      // Sign the user in to their account with the password
      UserCredential userCredential = await auth.signInWithEmailAndPassword(
        email: email,
        password: password,
      );

      // Link the pending credential with the existing account
      await userCredential.user.linkWithCredential(pendingCredential);

      // Success! Go back to your application flow
      return goToApplication();
    }

    // Since other providers are now external, you must now sign the user in with another
    // auth provider, such as Facebook.
    if (userSignInMethods.first == 'facebook.com') {
      // Create a new Facebook credential
      String accessToken = await triggerFacebookAuthentication();
      var facebookAuthCredential = FacebookAuthProvider.credential(accessToken);

      // Sign the user in with the credential
      UserCredential userCredential = await auth.signInWithCredential(facebookAuthCredential);

      // Link the pending credential with the existing account
      await userCredential.user.linkWithCredential(pendingCredential);

      // Success! Go back to your application flow
      return goToApplication();
    }

    // Handle other OAuth providers...
  }
}