[英]Firebase - How to write/read data per user after authentication
I have tried to understand but not able to see how and where might be the data I am storing after login is going. 我试图理解但是无法看到登录后我存储的数据的方式和位置。
public static final String BASE_URL = "https://xyz.firebaseio.com";
Firebase ref = new Firebase(FirebaseUtils.BASE_URL);
ref.authWithPassword("xyz@foo.com", "some_password", new Firebase.AuthResultHandler() {
@Override
public void onAuthenticated(AuthData authData) {
Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
@Override
public void onAuthenticationError(FirebaseError firebaseError) {
}
}
At this point I am successfully authenticated and landed onto MainActivity
. 此时,我已成功通过身份验证并登陆MainActivity
。 Next in onCreate
of MainActivity
. 接下来是onCreate
of MainActivity
。 I initialize Firebase
我初始化Firebase
firebase = new Firebase(FirebaseUtils.BASE_URL).child("box");
// adapter below is an ArrayAdapter feeding ListView
firebase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue(Box.class) instanceof Box)
adapter.add(dataSnapshot.getValue(Box.class).getName());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
adapter.remove(dataSnapshot.getValue(Box.class).getName());
}
// other callbacks
}
There is a add button that I used to push new records from Android to Firebase
. 我有一个添加按钮用于将新记录从Android推送到Firebase
。
final Button button = (Button) findViewById(R.id.addButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Box box = new Box("Box " + System.currentTimeMillis(), "Location " + System.currentTimeMillis());
Firebase fbBox = firebase.child("" + System.currentTimeMillis());
fbBox.setValue(box);
}
});
But above code doesn't add any record (evident from ListView
that is not updated) or atleast I might not know where to look for the data. 但是上面的代码没有添加任何记录(从未更新的ListView
中显而易见)或至少我可能不知道在哪里查找数据。 I checked opening Firebase
in browser, but I am not sure how to check for user specific data? 我在浏览器中检查了打开Firebase
,但我不确定如何检查用户特定数据?
I modified my Firebase Rules
like 我修改了我的Firebase Rules
{
"rules": {
"users": {
"$uid": {
".write": "auth != null && auth.uid == $uid",
".read": "auth != null && auth.uid == $uid"
}
}
}
}
I tried to open URLs such as https://xyz.firebaseio.com/xxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxxxxx but it wouldn't show any data. 我尝试打开https://xyz.firebaseio.com/xxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxxxxx等网址,但不会显示任何数据。
I would like to have some information on: 我想了解一些信息:
How to add user specific data after authentication. 如何在身份验证后添加用户特定数据。 Can't it be seamless like when we don't have any restriction on read/write on user basis, because I could easily read/write data. 当我们在用户的基础上对读/写没有任何限制时,它不能是无缝的,因为我可以轻松地读/写数据。
Is there any Firebase
web view to visualize the database or see JSON data, where I can see/modify the data to/from Android device? 是否有任何Firebase
Web视图可视化数据库或查看JSON数据,我可以在哪里查看/修改Android设备的数据?
First, modify the Firebase rules as follows: 首先,修改Firebase规则,如下所示:
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
}
}
}
Then, in Java Code: 然后,在Java代码中:
Firebase rootRef = new Firebase("https://user-account.firebaseio.com/");
// Assuming the user is already logged in.
Firebase userRef = rootRef.child("users/" + rootRef.getAuth().getUid());
userRef.child("message1").setValue("Hello World");
In the end, the data would look something like this: 最后,数据看起来像这样:
For web end 对于网络结束
Javascript code: Javascript代码:
var user = firebase.auth().currentUser;
if (user != null) {
uid = user.uid;
firebase.database().ref('/users/'+uid).push({
foo: 'abc',
bar: 'pqr'
});
}
Read https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user for more information. 有关详细信息,请参阅https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.