简体   繁体   English

如何在 Unity Firebase 身份验证中得到响应 JWT?

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

as a newbie in Unity and C#, I am using Firebase email authentication in my project.作为 Unity 和 C# 的新手,我在我的项目中使用 Firebase email 身份验证。 I want to take an action with using responded JWT token.我想使用响应的 JWT 令牌采取行动。 Here are my related codes:这是我的相关代码:

    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.

I went through something similar and hope this helps:我经历了类似的事情,希望这会有所帮助:

In order to get the JWT token from the Firebase Authentication response in Unity, you can use the User object returned by the SignInWithEmailAndPasswordAsync() method and access the User.Token property.为了从 Unity 中的 Firebase 身份验证响应中获取 JWT 令牌,您可以使用 SignInWithEmailAndPasswordAsync() 方法返回的用户 object 并访问 User.Token 属性。

JWT 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
    }
}

It's worth noting that the token's lifetime is 1 hour, after which you will have to refresh it.值得注意的是,令牌的生命周期为 1 小时,之后您必须刷新它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM