How to handle Firebase auth errors in Unity

I created a simple password in my Unity application after the doc , and it works fine, however I would like to handle various errors, and since it task.Exceptiondoes not return an error code, I do not know how to do it.

I found this one , but it does not look like the same version of Firebase as the login method is different ...

+4
source share
3 answers

Firebase has an AuthError rename that tells you the value of each ErrorCode in FirebaseException, the documentation is here: https://firebase.google.com/docs/reference/unity/namespace/firebase/auth p>

To get the error message in Spanish, I do the following:

public static string GetErrorMessage(Exception exception)
{
    Debug.Log(exception.ToString());
    Firebase.FirebaseException firebaseEx = exception as Firebase.FirebaseException;
    if (firebaseEx != null)
    {
        var errorCode = (AuthError)firebaseEx.ErrorCode;
        return GetErrorMessage(errorCode);
    }

    return exception.ToString();
}

private static string GetErrorMessage(AuthError errorCode)
{
    var message = "";
    switch (errorCode)
    {
        case AuthError.AccountExistsWithDifferentCredentials:
            message = "Ya existe la cuenta con credenciales diferentes";
            break;
        case AuthError.MissingPassword:
            message = "Hace falta el Password";
            break;
        case AuthError.WeakPassword:
            message = "El password es debil";
            break;
        case AuthError.WrongPassword:
            message = "El password es Incorrecto";
            break;
        case AuthError.EmailAlreadyInUse:
            message = "Ya existe la cuenta con ese correo electrónico";
            break;
        case AuthError.InvalidEmail:
            message = "Correo electronico invalido";
            break;
        case AuthError.MissingEmail:
            message = "Hace falta el correo electrónico";
            break;
        default:
            message = "Ocurrió un error";
            break;
    }
    return message;
}
+1
source

I finally found a way to display only error messages using this:

FirebaseException error = task.Exception.InnerExceptions[0] as FirebaseException;
string errorMsg = error.ToString();

However, this does not solve the theoretical problem, since it seems that something is missing in Firebase for Unity (at least for now).

+2
source
FirebaseAuth.DefaultInstance.CreateUserWithEmailAndPasswordAsync (Email.text, Password.text).
ContinueWith (task => {
        if (task.IsCanceled) 
        {
            Debug.LogError ("CreateUserWithEmailAndPasswordAsync was canceled");
            return;
        }
        if (task.IsFaulted) 
        {
            Debug.LogError ("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
            //      
            ErrorText.text = "Error: " + task.Exception.InnerExceptions[0].Message;
            //
            return;
        }
});

, , . ":", .

0

Source: https://habr.com/ru/post/1683329/


All Articles