首页 > 解决方案 > Firestore 模拟器和 request.auth.email 生成“对象上未定义属性电子邮件”错误

问题描述

感谢https://www.youtube.com/watch?v=VDulvfBpzZE伟大的资源,我安装了 firestore 模拟器和我的测试环境,除了我来测试以下规则时,一切都很好:

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {

    function invitationIsForMe(destEmail) {
        return request.auth.email == destEmail; 
    }

    match /invitations/{inviteId} {
        allow read, delete: if invitationIsForMe(resource.destEmail);
    }
}

这会在测试中产生错误: FirebaseError: Property email is undefined on object。

测试如下:

const me = {uid: 'myuserid', email: 'me@foo.bar'};

const getDb = (auth) => {
    return firebase.initializeTestApp({projectId: YORGA_PROJECT_ID, auth}).firestore();
}

describe('firestore structure and rules', () => {
    it("Can read an invitation sent to me", async () => {
        const db = getDb(me);
        const testDoc = db.collection('invitations').where('destEmail', '==', me.email);
        await firebase.assertSucceeds(testDoc.get());
    })
}

如果我用 request.auth.uid 测试它,它就可以工作(但这不是我要寻找的逻辑)。所以我的问题是:它是 Firestore 模拟器限制吗?我希望不是,只是我自己,但如果是这种情况,我认为在初始化测试库时给出的带有 uid 和用于身份验证的电子邮件的示例有点令人困惑,因此值得警告。

标签: google-cloud-firestorefirebase-authenticationfirebase-securityfirebase-cli

解决方案


推荐阅读