【问题标题】:unity Firebase auth stateunity Firebase 身份验证状态
【发布时间】:2019-12-25 16:21:39
【问题描述】:

我正在尝试使用 firebase 统一实现身份验证状态更改。我不知道我是否做得对,因为医生对我来说有点不清楚。来自你们的一个很好的代码示例会受到我的极大尊重。这是文档的链接。 https://firebase.google.com/docs/auth/unity/manage-users?authuser=0
文档中的标题是(获取当前登录的用户)。我真的需要帮助伙计们。我不明白如何正确使用它。我试图从 firebase 开发人员那里获得帮助,但没有得到回应。我你们想要一个详细的细节,我们可以在不和谐的地方聊天。让我知道。 :)

DatabaseReference reference;

public static Login instance;

FirebaseAuth auth;
FirebaseUser Player;

bool isServiceAvailable;
bool isAuthenticated;
public Text TEXTName;
public Text TEXTID;
public Text Data;
public Text State;

private User user;

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

// Start is called before the first frame update
void Start()
{

    FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(" I hide the url");
    reference = FirebaseDatabase.DefaultInstance.RootReference;
    State.text = "Checking state";
    InitializeFirebase();
    LogInUser();
}

// Update is called once per frame
void Update()
{

}



// Handle initialization of the necessary firebase modules:
void InitializeFirebase()
{
    Debug.Log("Setting up Firebase Auth");
    auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
    auth.StateChanged += AuthStateChanged;
    AuthStateChanged(this, null);
}

// Track state changes of the auth object.
void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
    if (auth.CurrentUser != Player)
    {
        bool signedIn = Player != auth.CurrentUser && auth.CurrentUser != null;
        if (!signedIn && user != null)
        {
            Debug.Log("Signed out " + Player.UserId);
            State.text = "Sign out";
        }
        Player = auth.CurrentUser;
        if (signedIn)
        {
            Debug.Log("Signed in " + Player.UserId);
            State.text = "Sign in";
        }
    }
}

void OnDestroy()
{
    auth.StateChanged -= AuthStateChanged;
    auth = null;
}

public void SignIn()
{

    FirebaseAuth auth = FirebaseAuth.DefaultInstance;
    auth.SignInWithEmailAndPasswordAsync(TEXTID.text, TEXTName.text).ContinueWith(task =>
    {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
            return;
        }


            FirebaseUser newUser = task.Result;
            Debug.LogFormat("User signed in successfully: {0} ({1})",
                newUser.DisplayName, newUser.UserId);


    });
}

【问题讨论】:

    标签: c# firebase unity3d firebase-authentication


    【解决方案1】:

    我有好消息,there is a samplevideo tutorial

    话虽如此,让我快速进行即兴代码审查:

    你正确注册了事件,甚至在 OnDestroy 中取消注册(我发现很多习惯UnityEvent 的人会省略这一步)。

    我不认为对玩家的检查正在做你想要的。我会做出两个改变:

    首先,我将ContinueWith 更改为ContinueWithOnMainThread。如果您最终对 UnityEngine 命名空间(而不是 Debug.Log)进行了任何调用,那么您就是在自找麻烦。

    接下来,FirebaseUser 现在以一种奇怪的方式实现。它在 C++ 端引用全局静态对象,而不是典型的 PIMPL 构造。造成这种情况的原因有很多,并且正在研究中,但是除了与 null 的比较之外,我不会指望其他比较来节省您的任何时间。除了添加一些冗余检查之外,这可能不会导致您发布的代码出现任何问题(并且Player.UserId 在您的注销逻辑中可能不可靠),但如果您打算缓存FirebaseUser 以便稍后进行比较,它不会像你想象的那样工作。

    【讨论】:

      猜你喜欢
      • 2018-01-19
      • 1970-01-01
      • 2016-04-07
      • 2017-12-09
      • 1970-01-01
      • 2020-07-13
      • 2021-03-04
      • 2019-09-20
      • 2023-01-21
      相关资源
      最近更新 更多