【发布时间】:2020-08-06 01:04:07
【问题描述】:
我有一个谷歌云函数,我正在从我的 RN 应用程序调用它,但它正在返回
[错误:内部]
我已将权限设置为未经身份验证的用户,因此任何人都可以调用它 - 仅用于测试目的。当我设置为 Authenticated users 权限时,它会引发另一个错误 [Error: Unauthenticated] 即使我已通过身份验证并且我可以在我的应用程序中获取 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 对其进行了测试,并且我返回了一个简单的字符串。所以,我相信问题出在函数本身的某个地方。
编辑:我只是通过简单地调用函数并返回上下文进行测试,它总是返回内部错误:
exports.registerNewPatient = functions.region('europe-west3').https.onCall((data, context) => {
return context; //this is returned as INTERNAL error.
}
我只是无法理解这里发生了什么,为什么当我作为用户身份验证时它会返回内部错误并且它应该返回经过身份验证的用户数据,不是吗?
【问题讨论】:
标签: javascript react-native firebase-authentication google-cloud-functions react-native-firebase