我有一个从RN应用程序调用的Google Cloud Function,但它正在返回
[[Error:Internal]
我已将权限设置为未经身份验证的用户,因此任何人都可以调用它-仅用于测试目的。当我设置为“通过身份验证的用户”权限时,即使我已通过身份验证,它也会引发另一个错误[错误:未经身份验证],并且可以在我的应用程序中获取currentUser ID。
曾尝试搜索此错误,但没有将我带到任何可能的解决方案,因此决定在此处发布并希望收到可以帮助我解决此问题的回复。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.createUser = functions.region('europe-west1').https.onCall(async (data, context) => {
try {
//Checking that the user calling the Cloud Function is authenticated
if (!context.auth) {
throw new UnauthenticatedError('The user is not authenticated. Only authenticated Admin users can create new users.');
}
const newUser = {
email: data.email,
emailVerified: false,
password: data.password,
disabled: false
}
const role = data.role;
const userRecord = await admin
.auth()
.createUser(newUser);
const userId = userRecord.uid;
const claims = {};
claims[role] = true;
await admin.auth().setCustomUserClaims(userId, claims);
return { result: 'The new user has been successfully created.' };
} catch (error) {
if (error.type === 'UnauthenticatedError') {
throw new functions.https.HttpsError('unauthenticated', error.message);
} else if (error.type === 'NotAnAdminError' || error.type === 'InvalidRoleError') {
throw new functions.https.HttpsError('failed-precondition', error.message);
} else {
throw new functions.https.HttpsError('internal', error.message);
}
}
});
在我的RN应用中,我这样称呼它:
var user = {
role: role
}
const defaultApp = firebase.app();
const functionsForRegion = defaultApp.functions('europe-west1');
const createUser = await functionsForRegion.httpsCallable('createUser');
createUser(user)
.then((resp) => {
//Display success
});
console.log(resp.data.result);
})
.catch((error) => {
console.log("Error on register patient: ", error)
});
我认为我在RN应用程序中调用它的方式是正确的,因为我已经用testFunction测试了它,并且返回了一个简单的字符串。因此,我认为问题出在函数本身中。
EDIT:我只是通过简单地调用函数并返回上下文进行测试,并且它始终返回内部错误:
exports.registerNewPatient = functions.region('europe-west3').https.onCall((data, context) => {
return context; //this is returned as INTERNAL error.
}
我只是不明白这里发生了什么,当我作为用户进行身份验证时,为什么它返回内部错误,并且应该返回经过身份验证的用户数据,不是吗?
尝试在您的registerNewPatient函数中使用某些console.log(context) ; console.log(data)
语句,然后查看日志。他们怎么说?
要考虑的其他事项可能包括:在客户代码中使用europe-west1
,而功能代码具有europe-west3
。试着让那些排队,看看是否可行?根据我的经验,如果找不到指定的函数,则客户端会收到内部错误。