使用 Firebase CLI 部署 Firebase 函数时,默认会将 Cloud Functions Invoker 权限授予
为什么
allUsers
。在这种设置下,下面的代码可以正常运行。
Cloud Functions Invoker 权限也可以授予 allAuthenticatedUsers
。但是,当我为 addMessage
实现此更改时,只会收到 UNAUTHENTICATED
的错误响应。为什么
allAuthenticatedUsers
不能用于这个 Firebase 云函数?
注意: 这个问答是由Furkan Yurdakul发布的一个已被删除的问题引起的,涉及他的 Firebase 应用中 allAuthenticatedUsers
无法与 Firebase 可调用函数一起使用的原因。
基于文档的最小工作示例,addMessage
在此处定义:这里
firebase.auth().signInAnonymously() // for the sake of the MWE, this will normally be Facebook, Google, etc
.then((credential) => {
// logged in successfully, call my function
const addMessage = firebase.functions().httpsCallable('addMessage');
return addMessage({ text: messageText });
})
.then((result) => {
// Read result of the Cloud Function.
const sanitizedMessage = result.data.text;
alert('The sanitized message is: ' + sanitizedMessage);
})
.catch((error) => {
// something went wrong, keeping it simple for the MWE
const errorCode = error.code;
const errorMessage = error.message;
if (errorCode === 'auth/operation-not-allowed') {
alert('You must enable Anonymous auth in the Firebase Console.');
} else {
console.error(error);
}
});