【发布时间】:2021-02-11 10:23:39
【问题描述】:
我正在尝试构建可调用的云函数,当用户删除帖子时,它也会尝试删除作为帖子的子集合的 cmets。所以我看到了示例并像文档示例一样实现了
const admin = require('firebase-admin');
const firebase_tools = require('firebase-tools');
const functions = require('firebase-functions');
admin.initializeApp({
serviceAccountId: 'xxxxxx-xxxxx@appspot.gserviceaccount.com'
}
);
exports.mintAdminToken = functions.https.onCall(async (data: any, context: any) => {
const uid = data.uid;
const token = await admin
.auth()
.createCustomToken(uid, { admin: true });
return { token };
});
exports.recursiveDelete = functions
.runWith({
timeoutSeconds: 540,
memory: '2GB'
})
.https.onCall(async (data: any, context: any) => {
// Only allow admin users to execute this function.
if (!(context.auth && context.auth.token && context.auth.token.admin)) {
throw new functions.https.HttpsError(
'permission-denied',
'Must be an administrative user to initiate delete.'
);
}
const path = data.path;
console.log(
`User ${context.auth.uid} has requested to delete path ${path}`
);
await firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
});
return {
path: path
};
});
我成功地收到了客户的自定义令牌。但我现在必须做什么?获得令牌后,我从客户端调用了“recursiveDelete”函数,但出现错误PERMISSION_DENIED
- 是否应该使用新的自定义管理员令牌对收到令牌的用户进行初始化? (如果我误解了,请告诉我)
- 在删除这样的子集合时,管理员令牌真的需要吗?它很难使用,所以我问。
【问题讨论】:
-
我认为您根本不需要这些检查,我了解您想在帖子被删除时将其删除。只需在删除帖子时触发一个函数,将帖子 ID 传递给它,查找并删除 cmets。
标签: firebase flutter google-cloud-functions