简体   繁体   English

Firestore用户集合isAdmin实现Angular

[英]Firestore User Collection isAdmin implementation Angular

I'm trying to create an admin interface for the web application I'm developing. 我正在尝试为我正在开发的Web应用程序创建一个管理界面。 I'm using Angular with Angularfire2. 我正在使用Angular和Angularfire2。 I'm using Firebase Firestore as the database. 我正在使用Firebase Firestore作为数据库。

I wanted to see how we can implement an isAdmin flag in the users collection so that specified users can access admin features. 我想看看如何在users集合中实现isAdmin标志,以便指定的用户可以访问管理功能。

Can anyone shed some light on how I can approach to develop this. 任何人都可以阐明我如何开发这个。 I know there are security rules you can set on the users document. 我知道您可以在用户文档上设置安全规则。 If authenticated users are allowed to read and write to their own doc, what's stopping them from changing the isAdmin flag? 如果允许经过身份验证的用户读取和写入自己的doc,那么阻止他们更改isAdmin标志的是什么?

Or is this really as simple as adding a boolean key and toggling that in the admin interface? 或者这是否真的像添加布尔键并在管理界面中切换一样简单?

Thanks, Walter 谢谢,沃尔特

You can use custom auth claims from a custom server using the Firebase Admin SDK . 您可以使用Firebase Admin SDK从自定义服务器使用自定义身份验证声明。 Then you can incorporate these claims into your Firestore security rules rules. 然后,您可以将这些声明合并到Firestore安全规则规则中。 You can find out more about controlling access with custom claims here . 您可以在此处找到有关使用自定义声明控制访问的更多信息 In your Firestore rules, for example, you could limit read access to this document using the admin custom claim like this: 例如,在Firestore规则中,您可以使用admin自定义声明限制对此文档的读取权限,如下所示:

service cloud.firestore {
  match /databases/{database}/documents/adminContent {
    allow read: if request.auth.token.admin == true;
  }
}

Which could be set from a custom server or Cloud Functions for Firebase like this: 可以从自定义服务器或Cloud Functions for Firebase中设置,如下所示:

// Set admin privilege on the user corresponding to uid.
admin.auth().setCustomUserClaims(uid, {admin: true}).then(() => {
  // The new custom claims will propagate to the user's ID token the
  // next time a new one is issued.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM