回答问题和评论。
如您所知,当用户注册 Firebase 时,会在 Firebase 服务器上创建一个用户帐户,并向该用户提供一个用户 ID (uid)。
典型的设计模式是在 Firebase 中有一个 /users 节点,用于存储有关用户的其他信息,例如昵称、地址或电话号码。
我们还可以利用 /users 节点来指示它是什么类型的用户; Worker 或 Client,它将与应用程序的其余部分和 Firebase 联系起来,以便他们获得正确的数据。
例如
users
uid_0
nickname: "John"
user_type: "Worker"
uid_1
nickname: "Paul"
user_type: "Client"
uid_2
nickname: "George"
user_type: "Worker"
uid_3
nickname: "Ringo"
user_type: "Worker"
正如您所看到的,John、George 和 Ringo 都是工人,Paul 是客户。
当用户登录时,Firebase 登录函数将返回用户身份验证数据,其中包含 uid。
Auth.auth().signIn(withEmail: "[email protected] /cdn-cgi/l/email-protection", password: "dog",
completion: { (auth, error) in
if error != nil {
let err = error?.localizedDescription
print(err!)
} else {
print(auth!.uid)
//with the uid, we now lookup their user type from the
//users node, which tells the app if they are a client
//or worker
}
})
如果app数据是这样划分的
app
client_data
...
worker_data
...
可以设置一个简单的规则来验证用户的 user_type 对于worker_data节点是Worker,对于client_data节点是Client。这是一个伪示例,它将允许客户端用户仅访问 client_data 节点中的数据(概念)
rules
client_data
$user_id
".read": "auth != null && root.child(users)
.child($user_id)
.child("user_type") == 'Client'"