繁体   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