使用Firebase身份验证存储其他信息

问题描述 投票:-1回答:2

有人知道是否可行,以及如何使用Firebase身份验证提供其他信息,例如用户名?

我创建了此方法,但它只存储电子邮件和密码。

    public void create()
    {
        FirebaseAuth.DefaultInstance.CreateUserWithEmailAndPasswordAsync(emailInput.text, passwordInput.text).ContinueWith((task => {
            if (task.IsCanceled)
            {
                Firebase.FirebaseException e = task.Exception.Flatten().InnerExceptions[0] as Firebase.FirebaseException;
                GetErrorMessage((AuthError)e.ErrorCode);
                return;
            }
            if (task.IsFaulted)
            {
                Firebase.FirebaseException e = task.Exception.Flatten().InnerExceptions[0] as Firebase.FirebaseException;
                GetErrorMessage((AuthError)e.ErrorCode);
                return;
            }
            if (task.IsCompleted)
            {
                print("Created user");
                return;

            }
        }));


    }

我是一个团结的新人,我开始使用Firebase大约一周。抱歉,如果我提出了明显的要求,但是我搜索了几天却没有足够的文档。

谢谢

c# firebase unity3d firebase-authentication
2个回答
1
投票

使用FirebaseUser.UpdateUserProfileAsync

public void SetPlayerNameAndImage(string playerName, string imageUrl)
{
    Firebase.Auth.FirebaseUser user = Firebase.Auth.FirebaseAuth.DefaultInstance.CurrentUser;
    if (user != null)
    {
        Firebase.Auth.UserProfile profile = new Firebase.Auth.UserProfile
        {
            DisplayName = playerName,
            PhotoUrl = new System.Uri(imageUrl),
        };
        user.UpdateUserProfileAsync(profile).ContinueWith(task => {
            if (task.IsCanceled)
            {
                Debug.LogError("UpdateUserProfileAsync was canceled.");
                return;
            }
            if (task.IsFaulted)
            {
                Debug.LogError("UpdateUserProfileAsync encountered an error: " + task.Exception);
                return;
            }

            Debug.Log("User profile updated successfully.");
        });
    }
}

1
投票

Firebase身份验证只能将每个用户存储在user profilecustom claims的专用字段中。自定义声明的数据块限于JSON的1000个字节,并且只能使用后端SDK编写,而不能使用Unity客户端SDK编写。尽管您当然可以在自定义声明中存储有关用户的数据,但这并不是它打算用于的目的(它是使用后端安全机制进行的授予访问权限的目的)。

您应该做的是使用数据库,例如Realtime DatabaseFirestore,以Firbase Auth UID为密钥存储每个用户的信息,并使用安全规则对其进行保护,以便每个用户只能访问数据您希望他们访问。

© www.soinside.com 2019 - 2024. All rights reserved.