我一直在使用Firebase进行身份验证并将用户数据保存到我的数据库中。
首先,我裁定数据库只能由我的用户访问
"rules": {
"MyUsers": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
第二,我使用以下代码登录用户,但是登录后我仍然无法访问我的数据库(因为我被限制为只能由同一用户访问) 那么您认为是什么问题呢?
firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
Firebase.Auth.Credential credential =
Firebase.Auth.FacebookAuthProvider.GetCredential(aToken);
auth.SignInWithCredentialAsync(credential).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInWithCredentialAsync was canceled.");
LoginPanel.SetActive(true);
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInWithCredentialAsync encountered an error: " + task.Exception);
LoginPanel.SetActive(true);
return;
}
// creating the new user using their information (email, name etc)
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId, newUser.Email);
});
我只想访问例如:用户X的数据库X, 如果我尝试向数据库读取或写入数据,则不会得到任何结果,也不会保存任何数据。
[编辑]
我尝试访问数据库并设置值和键的方式;
//I set values for each child in the database this way:
private void writeNewUser() // here we save some data to our databse
{
if (String.IsNullOrEmpty(UserId)) return;
playerDbRef.Child("MyUsers").Child(UserId).Child("User_Info").Child("name").SetValueAsync(UserName);
playerDbRef.Child("MyUsers").Child(UserId).Child("User_Info").Child("email").SetValueAsync(Email);
Debug.Log("User data saved to DATABASE");
}
为了读取数据,我运行以下代码行:
protected void StartListener()
{
FirebaseDatabase.DefaultInstance.GetReference("MyUsers").Child(UserId)
.ValueChanged += HandleValueChanged;
}
void HandleValueChanged(object sender, ValueChangedEventArgs args)
{
if (args.DatabaseError != null)
{
Debug.LogError(args.DatabaseError.Message);
return;
}
string name = args.Snapshot.Child("User_Info").Child("name").Value.ToString();
string email = args.Snapshot.Child("User_Info").Child("email").Value.ToString();
}