【问题标题】:Automatically create a value for every User in Firebase Database自动为 Firebase 数据库中的每个用户创建一个值
【发布时间】:2017-06-07 06:00:27
【问题描述】:

我已经创建了一个用户登录并注册了 Android 应用程序。我使用 Firebase Auth 进行用户注册。我希望当用户注册一个计数器值时,该用户将添加到我的数据库中。它将为所有用户创建。计数器值为“0”。

【问题讨论】:

  • 你好蒙塔西尔。这看起来不错。有什么问题?

标签: android firebase firebase-realtime-database firebase-authentication


【解决方案1】:

也许FirebaseAuth.AuthStateListener 是您需要的东西。见https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener

FirebaseAuth fa = FirebaseAuth.getInstance();

fa.addAuthStateListener(new FirebaseAuth.AuthStateListener() { 

    @Override
    onAuthStateChanged(FirebaseAuth auth) {
        //not sure how to check for the login event. but this should work too
        FirebaseUser currentUser = auth.getCurrentUser()
        if(currentUser != null) {
            //add your data in your database.
        }
    }
});

【讨论】:

  • 亲爱的 Rockernaap,我希望当用户注册成功后,该用户的名称和值将添加到我的 Firebase 数据库中。名称将为点数,值将为“0”。跨度>
【解决方案2】:

我认为 Firebase 云功能对此非常有用。你能看看authentication triggers吗?

你的 nodeJS 代码应该是这样的,看看入门部分here

exports.incrementUserount = functions.auth.user().onCreate(event => { // increment the counter in the database here });

【讨论】:

  • 请您解释一下吗?还是根据需要编写代码?
  • 它的 javascript 代码,看看入门部分,你应该可以自己做 :D 如果你真的做不到,我会为你写,但它真的超级简单您需要的所有代码都在文档中。
  • 我可以做到,但我希望你写下代码.. 提前致谢:D
  • @MuntasirAonik 今天晚些时候我会研究一下,但基本上你需要一个用户计数器。对吗?
  • 我希望当用户注册成功时,名称和值将被添加到我的数据库中。名称将是点,值将是 0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多