简体   繁体   English

密码注册后 Firebase 云函数未经身份验证的错误

[英]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:工作流程如下:

  1. User registers in app via firebase password authentication用户通过 Firebase 密码身份验证在应用中注册
  2. Firebase Auth Credentials are created (firebase signs in user upon success)创建 Firebase 身份验证凭据(成功后 firebase 登录用户)
  3. Once the credentials have been created, the cloud function is triggered in the firebase auth callback.创建凭据后,将在 firebase auth 回调中触发云函数。

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.

相关问题 防止用户在 Firebase 中注册? - Preventing user sign-up in Firebase? 注册(传递散列)和登录时,如何在数据库中将密码与散列密码进行比较是在2个不同的javascript函数中? - How to compare a password with a hashed one in database when sign-up(pass hashing) and login are in 2 different javascript function? 在 React-js 中验证用户的注册密码 - Validate the Sign-up Password of user in React-js 使用Firebase注册电子邮件/密码时出现“网络错误”错误 - “A network error has occurred” error on email/password sign up with Firebase 如何在注册期间确认重新输入期间修复我的密码验证规则? - How to fix my password validation rule during confirmation of re-type during sign-up? React Native 嵌套导航以在登录/注册(StackNavigator)后实现带有选项卡(bottomTabNavigator)的登陆屏幕 - React Native nested navigation to achieve a landing screen with tabs (bottomTabNavigator) after Sign-In/Sign-Up(StackNavigator) Firebase / React:onAuthStateChanged()是在注册函数的时间之前还是之后触发的? - Firebase/React: is onAuthStateChanged() triggered before or after the then of a sign up function? 如何使用 Firebase 身份验证将身份验证/注册请求限制为自定义电子邮件域 - How to limit authentication/sign-up request to custom email domains with Firebase Authentication QuickBlox用户自动注册 - QuickBlox user auto sign-up 填写注册表后,Bootstrap 模态在同一模态上显示“注册成功”文本 - Bootstrap modal to show "Registration Success" text on the same modal after filling-in the sign-up form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM