是否可以自定义 Firebase 错误消息模板或警报以显示自定义警报?

Can a Firebase error message template or Alert be customised to show custom Alerts?

我正在开发 React 应用程序。该应用程序具有基本的身份验证功能。这是下面的代码。

export async function registration(email, password, firstName, phone) {
  try {
    await firebase.auth().createUserWithEmailAndPassword(email, password);
    const currentUser = firebase.auth().currentUser;

const db = firebase.firestore();
db.collection("users").doc(currentUser.uid).set({
  email: currentUser.email,
  firstName: firstName,
  phone: phone,
  password: password,
});


} catch (err) {
    Alert.alert("Please Use Another Email ID", err.message);
  }
}

这里的问题是我正在尝试为“电子邮件 ID 已存在”的特定错误创建自定义警报。但是 Firebase 有自己的预定义错误模板。

当我用下面提到的自定义代码替换时,

 Alert.alert("Please Use Another Email ID", "This email ID already exists. If issue persists, contact support", [
  {
    text: "Ok",
    onPress: () => null,
    style: "cancel",
  },
  {
    text: "Contact Support",
    onPress: () => Linking.openURL('mailto: example@email.com'),
  },
]);
return true;

}

它有效,但对出现的每个错误显示相同的警报。

索蒙。帮助。我对 React 和 JS 比较陌生。

编写一个接受身份验证错误并相应地显示警报的自定义函数:

export function processAuthError(authError: string) {
    if(authError.includes('user-not-found')) {
        Alert.alert('User not found', 'You probably have to sign up first.')
    } else if(authError.includes('wrong-password')) {
        Alert.alert('Wrong password', 'Try again.')
    } else if(authError.includes('email-already-in-use')) {
        Alert.alert("Please Use Another Email ID", "This email ID already exists. If issue persists, contact support", [
            {
              text: "Ok",
              onPress: () => null,
              style: "cancel",
            },
            {
              text: "Contact Support",
              onPress: () => Linking.openURL('mailto: example@email.com'),
            },
          ]);
    } else if(authError.includes('network-request-failed')) {
        Alert.alert('Network error', 'Try again later or check your internet connection.')
    } else {
        Alert.alert('Unknown Error', 'Try again later.')
    }
}

这实际上应该开箱即用,因为我在我自己的代码中使用它与 Firebase 只是略有不同。我只是 return 一个自定义字符串,然后用它显示警报,但由于您不想自定义警报,这是更好的方法。