Firebase - 如何在认证后为每个用户写入/读取数据 [英] Firebase - How to write/read data per user after authentication
问题描述
<$ c $我试图理解,但无法看到我登录后的数据是如何以及在哪里存储的。 c> public static final String BASE_URL =https://xyz.firebaseio.com;
Firebase ref =新的Firebase(FirebaseUtils.BASE_URL);
ref.authWithPassword(xyz@foo.com,some_password,new Firebase.AuthResultHandler(){
$ b $ @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){
}
}
在这一点上,我成功地进行身份验证,并着陆到 MainActivity
上,接下来在 onCreate
of MainActivity
。我初始化 Firebase
<$ c (); $ c $ firebase = new Firebase(FirebaseUtils.BASE_URL).child(box);
//下面的适配器是一个ArrayAdapter提供的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());
$ b @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){
adapter.remove(dataSnapshot.getValue(Box.class).getName() );
}
//其他回调
}
有一个添加按钮,我用来将新记录从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 Firebox fbBox = firebase.child(+ System.currentTimeMillis());
fbBox.setValue(box);
}
} );
但是上面的代码并没有添加任何记录(从 我修改了我的 我尝试打开网址,例如 I have tried to understand but not able to see how and where might be the data I am storing after login is going. At this point I am successfully authenticated and landed onto There is a add button that I used to push new records from Android to But above code doesn't add any record (evident from I modified my I tried to open URLs such as https://xyz.firebaseio.com/xxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxxxxx but it wouldn't show any data. 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
First, modify the Firstbase rules: Then, in Java Code: In the end, the data would look something like this: 这篇关于Firebase - 如何在认证后为每个用户写入/读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! ListView
没有更新)或者至少我可能不知道在哪里查找数据。我检查了浏览器中的开放
Firebase
,但我不知道如何检查用户特定的数据?
Firebase规则
like
{
rules: {
users:{
$ uid:{
.write:auth!= null&&auth.uid == $ uid,
.read:auth!= null&& auth.uid == $ uid
}
}
}
}
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) {
}
}
MainActivity
. Next in onCreate
of MainActivity
. I initialize 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
}
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);
}
});
ListView
that is not updated) or atleast I might not know where to look for the data. I checked opening Firebase
in browser, but I am not sure how to check for user specific data?Firebase Rules
like {
"rules": {
"users": {
"$uid": {
".write": "auth != null && auth.uid == $uid",
".read": "auth != null && auth.uid == $uid"
}
}
}
}
Firebase
web view to visualize the database or see JSON data, where I can see/modify the data to/from Android device?{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
}
}
}
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");