[英]Firebase Cloud Function Unauthenticated Error After Password Sign-Up
I am receiving the following error when triggering this cloud function: "Error unauthenticated".触发此云功能时,我收到以下错误:“未经身份验证的错误”。
I do not wish to allow unauthenticated calls to this cloud function.我不希望允许未经身份验证的调用此云功能。
The workflow is as follows:工作流程如下:
At this point, the call should be authenticated, given it's being triggered in the firebase auth response.此时,应该对调用进行身份验证,因为它是在 firebase 身份验证响应中触发的。
However, it keeps erroring with但是,它不断出错
Error: unauthenticated错误:未经身份验证
The user is authenticated at this point.此时用户已通过身份验证。
Any suggestions?有什么建议? CLIENT CODE ->客户代码 ->
const onRegisterPress = () => {
if (password !== confirmPassword) {
alert("Passwords don't match.")
return
}
setLoading(true);
//CREATE'S USER'S AUTH CREDENTIALS
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then((response) => {
const data = {
...
}
//console.log(response);
return new Promise(async function(resolve, reject) {
await firebase.functions().httpsCallable('writeAccountUser')({
data
}).then((response) => {
console.log("Write Account User Response: ", response);
resolve(setLoading(false));
}).catch((error) => {
console.error("Cloud Function Error: ", error);
setLoading(false);
reject(error)
})
});
})
.catch((error) => {
alert(error)
});
}
CLOUD FUNCTION ->云功能 ->
const functions = require("firebase-functions"); const admin = require("firebase-admin"); admin.initializeApp(); const firestore = admin.firestore(); exports.writeAccountUser = functions.https.onCall((data, context) => { console.log("Incoming Data: ", data); console.log("Incoming Context: ", context); const clientData = data.data; console.log("Client Data: ", clientData); console.log("Account ID: ", clientData.accountID); return new Promise(async function (resolve, reject) { const accountsRef = firestore.collection('Accounts'); const usersRef = firestore.collection('users'); const now = new Date(); if (clientData.accountExists === true) { console.log("Account Exists"); await accountsRef .doc(clientData.accountID) .update({ users: admin.firestore.FieldValue.arrayUnion(clientData.uid) }).catch((error) => { console.error(error); reject(false) }); } else { console.log("Account Does Not Exist!"); const account_data = clientData.accountData; const product_lines = clientData.productLines; await accountsRef .doc(clientData.accountID) .set({ account_data, product_lines, users: [clientData.uid], dateCreated: { date: now, timestamp: now.getTime() } }).catch((error) => { console.error(error); reject(false)}); }; const email = clientData.email; const fullName = clientData.fullName; const acceptTerms = clientData.acceptTerms; const userData = { id: clientData.uid, email, fullName, accountID: clientData.accountID, dateCreated: { date: now, timestamp: now.getTime() }, lastUpdateFetch: { date: now, timestamp: now.getTime() }, termsConditionsAccepted: acceptTerms }; await usersRef .doc(clientData.uid) .set(userData) .catch((error) => { console.error(error); reject(false) }); resolve(true); }); });
Error -> [Unhandled promise rejection: Error: unauthenticated] at node_modules/@firebase/firestore/dist/rn/prebuilt.rn-f9cd27ba.js:12199:33 in at http:///node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:169743:29 in _errorForResponse at node_modules/@firebase/firestore/dist/rn/prebuilt.rn-f9cd27ba.js:12747:31 in yu at node_modules/tslib/tslib.js:77:12 in at http://REDACTED/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:120748:21 in at http://REDACTED/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:120702:31 in fulfilled错误 -> [未处理的承诺拒绝:错误:未经身份验证] at node_modules/@firebase/firestore/dist/rn/prebuilt.rn-f9cd27ba.js:12199:33 in at http:///node_modules/expo/AppEntry.bundle? platform=ios&dev=true&hot=false&minify=false:169743:29 in _errorForResponse at node_modules/@firebase/firestore/dist/rn/prebuilt.rn-f9cd27ba.js:12747:31 in yu at node_modules/tslib/tslib.js:77 :12 在 http://REDACTED/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:120748:21 在 http://REDACTED/node_modules/expo/AppEntry.bundle?platform=ios&dev =true&hot=false&minify=false:120702:31 已完成
You can try refactoring the function as shown below:您可以尝试重构函数,如下所示:
const onRegisterPress = async () => {
if (password !== confirmPassword) {
alert("Passwords don't match.")
return
}
setLoading(true);
const response = await firebase.auth().createUserWithEmailAndPassword(email, password)
const data = {...}
const fnResponse = await firebase.functions().httpsCallable('writeAccountUser')({data})
console.log("Write Account User Response: ", response);
}
You can also create the account using the Admin SDK in the same function and log the user on your web app after the response.您还可以在同一函数中使用 Admin SDK 创建帐户,并在响应后将用户登录到您的 Web 应用程序。 That'll ensure the Cloud function's action has been executed as well (just in case the function is not called after user sign up for any reason).这将确保 Cloud 函数的操作也已执行(以防万一用户因任何原因注册后未调用该函数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.