Firebase not observing until accessed once [closed]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-25 18:49:33

问题


Firebase is configured in APPDelegate.

Read is set to .true in rules.

But, before I have a user sign into my application, I am checking for usernames, within a separate table of my Firebase DB. It would skip the observation block entirely, until I forced signed-in into another already created account of mine. And then when I would re-build the application the block is no longer skipped and actually accesses the table.

How do I "Wake Up" Firebase when I run the app on a fresh device, so I can check for usernames without having to log onto the app with an existing account before hand?


回答1:


You may want to reconsider how the app is structured.

If you are checking for usernames before being authenticated that means the node is exposed and anyone can grab a copy of all the usernames. Obviously that in itself may not be a huge issue but if your users decide to use an email, whoever grabs the list has a instant list they can spam to.

When you add any observer to a node, that node is read once as soon as the observer is added. When you app starts you can use .childAdded to iterate over an existing node to pre-load some data, a grocery list for example, and then any new grociees added after that will be sent your your app via an event.

Likewise a .value event will read in an entire node and leave an observer attached for any future events.

The username issue is tricky and the way you are doing is now is probably going to get you in trouble in the long run.

A better way is to leverage Firebase Authentication.

Firebase handles all of the usernames and passwords for you. It's very powerful and flexible and avoids the issues you are encountering. It will let you know if user names exist or not, it will do password reset emails and you can manage users from the Firebase Console. It's the way to go.

If you want to add username functionality it can be pretty easily done by adding a username or nickname node to the /users node

/users
   uid_0
    email: "bill@email.com"
    username: "bill_the_cat"
   uid_1
    email: "clark@email.com"
    username: "superman"

Once the user authenticates using Firebase Authentication, from there forward any time the user info needs to be displayed in the app, simply look up the uid that you need (uid_1) and grab the username node (superman) for display.



来源:https://stackoverflow.com/questions/44188405/firebase-not-observing-until-accessed-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!