Firebase - 如何在身份验证后为每个用户 write/read 数据
Firebase - How to write/read data per user after authentication
我试图理解但无法看到登录后我存储的数据的方式和位置。
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
。 MainActivity
的 onCreate
中的下一个。我初始化 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
}
有一个添加按钮,我用来将新记录从 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);
}
});
但是上面的代码没有添加任何记录(从 ListView
可以看出没有更新)或者至少我可能不知道去哪里寻找数据。我检查了在浏览器中打开 Firebase
,但我不确定如何检查用户特定数据?
我修改了我的 Firebase Rules
就像
{
"rules": {
"users": {
"$uid": {
".write": "auth != null && auth.uid == $uid",
".read": "auth != null && auth.uid == $uid"
}
}
}
}
我试图打开诸如 https://xyz.firebaseio.com/xxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxxxxx 之类的 URL,但它不会显示任何数据。
我想了解以下方面的一些信息:
如何在身份验证后添加用户特定数据。它不能像我们在用户基础上对 read/write 没有任何限制那样无缝,因为我可以轻松地 read/write data.
是否有任何 Firebase
网页视图可视化数据库或查看 JSON 数据,我可以在哪里 see/modify 数据 to/from Android设备?
首先修改Firebase规则如下:
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
}
}
}
那么,在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");
最后,数据看起来像这样:
网页端
Javascript代码:
var user = firebase.auth().currentUser;
if (user != null) {
uid = user.uid;
firebase.database().ref('/users/'+uid).push({
foo: 'abc',
bar: 'pqr'
});
}
阅读https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user
获取更多信息。
我试图理解但无法看到登录后我存储的数据的方式和位置。
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
。 MainActivity
的 onCreate
中的下一个。我初始化 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
}
有一个添加按钮,我用来将新记录从 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);
}
});
但是上面的代码没有添加任何记录(从 ListView
可以看出没有更新)或者至少我可能不知道去哪里寻找数据。我检查了在浏览器中打开 Firebase
,但我不确定如何检查用户特定数据?
我修改了我的 Firebase Rules
就像
{
"rules": {
"users": {
"$uid": {
".write": "auth != null && auth.uid == $uid",
".read": "auth != null && auth.uid == $uid"
}
}
}
}
我试图打开诸如 https://xyz.firebaseio.com/xxxxxx-xxxxx-xxxx-xxxxx-xxxxxxxxxxxx 之类的 URL,但它不会显示任何数据。
我想了解以下方面的一些信息:
如何在身份验证后添加用户特定数据。它不能像我们在用户基础上对 read/write 没有任何限制那样无缝,因为我可以轻松地 read/write data.
是否有任何
Firebase
网页视图可视化数据库或查看 JSON 数据,我可以在哪里 see/modify 数据 to/from Android设备?
首先修改Firebase规则如下:
{
"rules": {
"users": {
"$uid": {
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
}
}
}
那么,在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");
最后,数据看起来像这样:
网页端
Javascript代码:
var user = firebase.auth().currentUser;
if (user != null) {
uid = user.uid;
firebase.database().ref('/users/'+uid).push({
foo: 'abc',
bar: 'pqr'
});
}
阅读https://firebase.google.com/docs/auth/web/manage-users#get_the_currently_signed-in_user 获取更多信息。