如何在Firebase中验证用户和数据库之间进行链接? [英] How to link between Authenticated users and Database in Firebase?

查看:103
本文介绍了如何在Firebase中验证用户和数据库之间进行链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Android应用拥有Firebase身份验证。我的问题是,如何在经过身份验证的用户(Firebase控制台中的 Firebase :: Auth 选项卡)和数据库( Firebase ::数据库选项卡)。



我需要将数据库中的数据与 Auth 用户UID 出现在 Auth 中,并最终发送关于特定 UID 获取数据库中的相关信息。



内置的这样做,还是我必须在数据库中的用户创建一个新的条目,每次我认证一个用户,然后用它的UID创建一个ID字段出现在 Auth



中的值在用户管理文档只解释了如何将用户添加到 Auth 标签并重置密码。 p>

解决方案

<身份验证后,使用Firebase提供的UID创建一个子项,并将其值设置为您的用户类:

  //获取firebase用户
FirebaseUser user = FirebaseAuth.getInstance()。getCurrentUser();

//获取引用
DatabaseReference ref = FirebaseDatabase.getInstance()。getReference(USERS_TABLE);
$ b $ // build child
ref.child(user.getUid())。setValue(user_class);

USERS_TABLE是根的直接子元素。

<然后当你想要检索数据时,通过它的UID获得对用户的引用,监听 addListenerForSingleValueEvent()(只调用一次),然后迭代结果使用反射:

  //获取firebase用户
FirebaseUser user = FirebaseAuth.getInstance()。getCurrentUser();

//获取引用
DatabaseReference ref = FirebaseDatabase.getInstance()。getReference(USERS_TABLE).child(user.getUid());
//重要:.getReference(user.getUid())将不起作用,尽管user.getUid()是唯一的。你需要一个完整的路径!
$ b $ //获取信息
ref.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
final Profile tempProfile = new Profile(); //这是我的user_class类
final Field [] fields = tempProfile.getClass()。getDeclaredFields(); $ b $ for(Field field:fields){
Log .i(TAG,field.getName()+:+ dataSnapshot.child(field.getName())。getValue());
}
}

@覆盖
public void onCancelled(DatabaseError databaseError){
}
});

编辑



  @Override 
public void onDataChange(DataSnapshot dataSnapshot){
final个人资料p = dataSnapshot.getValue(Profile.class);
}


I have Firebase Authentication in my Android app. My question is how do I link between authenticated users (that appear in Firebase::Auth tab in the Firebase console) and the database (Firebase::Database tab).

I need to link data in the database with the relevant user in Auth by the User UID that appears in Auth, and eventually send queries about a specific UID to get the relevant info in Database.

Is there something automatic / built-in that does that or do I have to create a new entry for a user in Database each time I authenticate a user and then create an ID field for it with the UID value that appears in Auth?

In the User Management docs it explains only about adding users to the Auth tab and resetting their passwords.

解决方案

After authentication, create a child with the UID given by Firebase, and set its value to your user class:

//get firebase user
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

//get reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(USERS_TABLE);

//build child
ref.child(user.getUid()).setValue(user_class);

USERS_TABLE is a direct child of root.

Then when you want to retrieve the data, get a reference to the user by its UID, listen for addListenerForSingleValueEvent() (invoked only once), and iterate over the result with reflection:

//get firebase user
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

//get reference
DatabaseReference ref = FirebaseDatabase.getInstance().getReference(USERS_TABLE).child(user.getUid());
//IMPORTANT: .getReference(user.getUid()) will not work although user.getUid() is unique. You need a full path!

//grab info
ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        final Profile tempProfile = new Profile(); //this is my user_class Class
        final Field[] fields = tempProfile.getClass().getDeclaredFields();
        for(Field field : fields){
            Log.i(TAG, field.getName() + ": " + dataSnapshot.child(field.getName()).getValue());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

edit:

Or without reflection:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    final Profile p = dataSnapshot.getValue(Profile.class);
}

这篇关于如何在Firebase中验证用户和数据库之间进行链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆