gpt4 book ai didi

reactjs - Create React App 中 httpsCallable firebase 上的 CORS 错误

转载 作者:行者123 更新时间:2023-12-04 11:30:50 25 4
gpt4 key购买 nike

已解决 - 请参阅下面的答案。

可调用函数导致 CORS 错误,如下所示:

Access to fetch at 'https://us-central1-careerhub-a50a2.cloudfunctions.net/createUser' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.



功能代码:
exports.createUser = functions.https.onCall((data, context) => {
const email = data.email
const password = data.password

admin.auth().createUser({
email: email,
emailVerified: false,
password: password
}).then((res) => {
return {
response: res
}
}).catch((err) => {
return {err}
})

})

客户端:
    const handleAddUser = () => {
let pswd = passwordGen(7)
const createUser = firebase.functions().httpsCallable('createUser');
createUser({email: email, password: pswd}).then(function(result) {
let res = result.data
console.log(res)
}).catch(function(error) {
console.log(error.code)
})

}

包.json
{
"name": "careerhub-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"antd": "^4.0.2",
"firebase": "^7.10.0",
"node-sass": "^4.13.1",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-email-editor": "^1.0.0",
"react-reveal": "^1.2.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"recharts": "^2.0.0-beta.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

即使我处于离线状态,此错误仍然存​​在。

我尝试过的事情:
  • 更新 Firebase SDK
  • 添加“代理”:'...' 到 package.json
  • 使用云函数指南中的基本函数
  • 删除 firebase 应用程序和删除功能并重新部署
  • 添加 const cors = require('cors')({origin: true});功能
  • 最佳答案

    事实证明,该错误是 Google 端关于 IAM 权限的一个记录不完整的更改,无法通过 Firebase 控制台清楚地访问这些更改。

    原因:

    云函数禁止访问该函数。新创建的函数没有 Cloud Functions Invoker。此更改已于 2020 年 1 月 15 日实现

    解决方案:

    创建 Cloud Functions Invoker 并设置为 allUsers。
    https://cloud.google.com/functions/docs/securing/managing-access-iam

    关于reactjs - Create React App 中 httpsCallable firebase 上的 CORS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60971975/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com