Firebase:针对同一个项目中的每个应用程序进行自定义验证 [英] Firebase: Customized authenication for each application in the same project

查看:116
本文介绍了Firebase:针对同一个项目中的每个应用程序进行自定义验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我的项目有2个应用程序。一个应用程序只运行在我们知道的特定移动设备上,它不需要验证就可以与Firebase进行交互。另一个是运行在许多移动设备上,它必须登录到firebase才能相互通信。那么,我想要做的是如何在firebase中配置数据库规则,以使2个应用程序可以使用相同的数据库?非常感谢大家!为我的坏英语道歉。

解决方案

您无法保护数据库访问权限,以允许特定的应用程序或特定设备。请参阅如何防止其他人访问我的Firebase

但是,如果您使用Firebase身份验证,则设备可以轻松映射到属于特定用户。如果您不想要求用户登录,则甚至可以使用匿名身份验证。对于Firebase身份验证,每个用户都有唯一的用户ID(Firebase条款中的UID)。当你知道用户的UID时,你可以根据该UID保证对数据库的访问。



最近一个项目的例子:


$ b $

  {
rules:{
.read:true,
.write:auth != null&&
root.child('config / whitelist')。child(auth.uid).exists()
}
}
/ config / whitelist下,我们允许编写它
。例如:
$ b $ pre $ code $ config
白名单
jn0BrHQqUEYSjqvqfqzbJTMOlZ82:true
ytEtWqOfLkRk3OUjTKBtZnTehZc2true


My problem is my project has 2 application. One application just run in a specific mobile device which we known, it interact with firebase without authenication. The another is run in many mobile devices, it must sign-in into firebase to communicate each other.

So, what I want to do is how to configure database rule in firebase to 2 app can use the same the database? Thank everyone very much! Appologize for my bad english.

解决方案

You cannot secure database access to allow a specific app or a specific device. See How to prevent other access to my firebase.

But a device can easily be mapped to belong to a specific user, if you use Firebase Authentication. You could even use anonymous authentication if you don't want to require that the user signs in. With Firebase Authentication each user has a unique user id (UID in Firebase terms). And when you know the UID for the user, you can secure access to the database based on that UID.

An example from a recent project:

{
  "rules": {
    ".read": true,
    ".write": "auth != null && 
               root.child('config/whitelist').child(auth.uid).exists()"
  }
}

So here, we allow writing if the signed-in user's UID is present under a node /config/whitelist. E.g.

config
   whitelist
     "jn0BrHQqUEYSjqvqfqzbJTMOlZ82": true
     "ytEtWqOfLkRk3OUjTKBtZnTehZc2" true

这篇关于Firebase:针对同一个项目中的每个应用程序进行自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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