Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Firebase auth errors on Unity

I created a simple password login in my Unity app following the doc and it works fine, however I would like to handle the different kind of errors and since task.Exception doesn't return an error code I don't know how to proceed.

I found this but it doesn't seems to be the same Firebase version since the login method is different...

like image 759
J'hack le lezard Avatar asked Aug 09 '17 10:08

J'hack le lezard


People also ask

What does Firebase auth () CurrentUser return?

You can also get the currently signed-in user by calling CurrentUser . If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing.

How long does Firebase auth session last?

By default, a session ends (times out) after 30 minutes of user inactivity. There is no limit to how long a session can last.

Does Firebase work with Unity?

Registering Your Unity App with Firebase. In the console, select the Unity icon to get started with adding Firebase to your project. You can register both iOS and Android apps, but to do so, you'll have to provide an iOS bundle ID/Android package name. To find that information, open up your existing Unity project.


2 Answers

Firebase has a AuthError enum that tells you the meaning of each ErrorCode in the FirebaseException, the documentation is here: https://firebase.google.com/docs/reference/unity/namespace/firebase/auth

To get the error message in Spanish I am doing this:

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;
}
like image 154
MauricioJuanes Avatar answered Sep 25 '22 22:09

MauricioJuanes


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

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

However it doesn't solve theoriginal problem, since it seems this is something missing in Firebase for Unity (at least for now).

like image 30
J'hack le lezard Avatar answered Sep 25 '22 22:09

J'hack le lezard