【问题标题】:How we set the header values as a authentication in cloud firestore我们如何将标头值设置为 Cloud Firestore 中的身份验证
【发布时间】:2021-01-31 17:21:50
【问题描述】:

我已在 firebase 中应用了该规则。在firestore auth中添加了一个用户并相应地更改了规则。我的自定义firestore规则是

match /{document=**} {
        allow read, write :if request.auth == 'Here user values will add';

从我的nestjs代码中,用户作为头文件添加到控制器类中,并将这个值传递给服务层,但是当在我们的firebase服务类中传递这个值时,这是我在firebase服务类中的以下代码。

下面是我的控制器类

@Get('/list')
async getDocumentList(@Headers('user') user: string): Promise<any> {

 console.log("controller"+user);
    try {
        return await this.incidentService.getIncidentList(user);

    } catch (e) {
        throw new HttpException(e.message, HttpStatus.BAD_REQUEST);
    }
}

firestore 服务类是:-

async getDocumentList(collectionName: string,user ): Promise<any> {

    console.log(user);
    
    const docSnapshot = await firestore().collection(collectionName).get();                    
                          
    const docList = [];

    docSnapshot.forEach(doc => {
        docList.push(doc.data());
    });
    return docList;
}

我想知道如何将用户值传递/通知或设置条件到 firestore,以便将此用户值设为管理员并根据此值应用规则?

Firestore 服务类中的代码是什么?

【问题讨论】:

    标签: typescript google-cloud-firestore firebase-security


    【解决方案1】:

    从您的读取请求传递到 Firestore 安全规则的唯一信息是:

    • 正在请求的路径(和/或查询)。
    • 来自客户端的当前身份验证对象,包括其 ID 令牌中的任何信息。

    您不能传递其他信息并期望它显示在安全规则中。

    因此,如果您想将特定用户标记为应用程序管理员,并基于此在您的安全规则中授予他们特定的访问权限,您必须将该信息嵌入到上述项目之一中。

    典型的方法是add this information as a custom claim to the ID tokenstore it in the database itself and look it up based on the UID

    【讨论】:

    • 这是我在云防火墙中的规则 rules_version = '2';服务 cloud.firestore { 匹配 /databases/{database}/documents { 匹配 /{document=**} { 允许读取;允许写入:if (request.auth.token.admin) == false; }}} -> 我所做的代码更改了 admin.auth().setCustomUserClaims(environment.fireBaseAdminId, {admin: true}).then(() => { } 但仍然能够在 DB.it 中写入。似乎规则不是工作
    • 请记住,自定义声明最多可能需要一个小时才能从服务器传播到您的客户端(并从那里传播到安全规则)。
    • 当我在云 Firestore 中应用规则时,我了解到“Firebase Admin SDK 支持在用户帐户上定义自定义属性”......因为我是从管理员初始化应用程序......规则是即使在申请一小时后也不工作
    • 感谢您的检查。这在 cmets 中很难解决,所以我建议用最少的重现来打开一个新问题。在这种情况下,这将是:1)规则,2)您希望阅读的文档,3)您如何设置声明的代码,3)记录声明然后进行失败调用的客户端代码到数据库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2020-10-16
    • 2015-11-30
    • 2018-05-02
    • 2011-09-11
    • 2021-09-18
    相关资源
    最近更新 更多