Firebase身份验证Unity持久性

时间:2018-08-20 16:53:06

标签: c# firebase unity3d firebase-authentication

我一直在尝试按照本教程并使用其文档中提供的示例代码在我的Unity项目中实现Firebase Auth。

1)我可以注册并登录,但是在关闭->打开应用后,我无法保留已登录的用户。

2)这可能与第一个问题有关,但是当我重新安装该应用程序时,我可以第一次登录。之后,我将无法登录该版本。编辑器是完全可以的。通过代码运行,我只能推测问题出在onAuthStateChanged中,因为我的解决方法是在首次实例化该对象时注销用户。

FirebaseAuth m_Auth;
FirebaseUser m_User;

private void Awake()
{
    if (instance == null)
    {
        instance = this;
    } else if (instance != this)
    {
        Destroy(gameObject);
    }
    DontDestroyOnLoad(gameObject);
    InitializeFirebase();
}


void InitializeFirebase()
{
    m_Auth = FirebaseAuth.DefaultInstance;
    m_Auth.StateChanged += AuthStateChanged;
    AuthStateChanged(this, null);
}

void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
    if (m_Auth.CurrentUser != m_User)
    {
        bool signedIn = m_User != m_Auth.CurrentUser && m_Auth.CurrentUser != null;
        if (!signedIn && m_User != null)
        {
            Debug.Log("Signed out " + m_User.UserId);
        }
        m_User = m_Auth.CurrentUser;
        if (signedIn)
        {
            /*
            displayName = user.DisplayName ?? "";
            emailAddress = user.Email ?? "";
            photoUrl = user.PhotoUrl ?? "";
            */
            Debug.Log("Sign in");
            AuthDelegate();
        }
    }
}

public void EmailAuthentication(Dictionary<string, string> profile)
{
    string email = profile["email"];
    string password = profile["password"];
    m_Auth.SignInWithEmailAndPasswordAsync(email, password).ContinueWith(task => {
        if (AuthHasError(task))
        {
            return;
        }

        m_User = task.Result;
        profileUpdateDelegate();
    });
}

bool AuthHasError(Task task)
{
    bool isError = task.IsCanceled || task.IsFaulted;
    if (isError)
    {
        if (task.Exception.InnerExceptions.Count > 0)
        {

            errorDelegate(task.Exception.InnerExceptions[0].Message);
        }
        else
        {
            errorDelegate("An Error has occured");
        }
    }
    return isError;
}

据我所知,这些是与我的问题相关的更相关的代码。任何帮助或建议都会很棒。

0 个答案:

没有答案