簡體   English   中英

如何在 Unity Firebase 身份驗證中得到響應 JWT?

[英]How to get responded JWT in Unity Firebase Authentication?

作為 Unity 和 C# 的新手,我在我的項目中使用 Firebase email 身份驗證。 我想使用響應的 JWT 令牌采取行動。 這是我的相關代碼:

    private IEnumerator Login(string _email, string _password)
    {
        //Call the Firebase auth signin function passing the email and password
        var LoginTask = auth.SignInWithEmailAndPasswordAsync(_email, _password);
        //Wait until the task completes
        yield return new WaitUntil(predicate: () => LoginTask.IsCompleted);

        
.
.
.

        else
        {
            //User is now logged in
            //Now get the result
            User = LoginTask.Result;
            Debug.LogFormat("User signed in successfully: {0} ({1})", User.DisplayName, User.Email);
            warningLoginText.text = "";
            confirmLoginText.text = "Logged In";
        }
    }

   

I think I need to access it from LoginTask. However, I don't know how to get JWT. I will use it API request operations. Thank you for your help.

我經歷了類似的事情,希望這會有所幫助:

為了從 Unity 中的 Firebase 身份驗證響應中獲取 JWT 令牌,您可以使用 SignInWithEmailAndPasswordAsync() 方法返回的用戶 object 並訪問 User.Token 屬性。

JWT 代幣:

private IEnumerator Login(string _email, string _password)
{
    //Call the Firebase auth signin function passing the email and password
    var LoginTask = auth.SignInWithEmailAndPasswordAsync(_email, _password);
    //Wait until the task completes
    yield return new WaitUntil(predicate: () => LoginTask.IsCompleted);

    if (LoginTask.IsFaulted)
    {
        //Handle error
        Debug.LogError("Error logging in user: " + LoginTask.Exception);
    }
    else
    {
        //User is now logged in
        //Now get the result
        User = LoginTask.Result;
        Debug.LogFormat("User signed in successfully: {0} ({1})", User.DisplayName, User.Email);
        warningLoginText.text = "";
        confirmLoginText.text = "Logged In";

        // Get the JWT token
        string jwt = User.Token;
        Debug.LogFormat("JWT token: {0}", jwt);
        // Do something with the JWT token, such as sending it to a server for verification
    }
}

值得注意的是,令牌的生命周期為 1 小時,之后您必須刷新它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM