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

edqdpe6u  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(181)

作为Unity和C#的新手,我在我的项目中使用Firebase电子邮件认证。2我想使用响应的JWT令牌。3下面是我的相关代码:

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.
i7uq4tfw

i7uq4tfw1#

我也经历过类似的事情,希望这能有所帮助:
要从Unity中的Firebase身份验证响应获取JWT令牌,您可以使用SignInWithEmailAndPasswordAsync()方法返回的User对象并访问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小时,超过此期限后您必须刷新它。

相关问题