本地Firebase web应用尝试非本地云功能 总结
我试图通过为本地web应用提供服务并调用它们来测试云函数的授权(因为模拟器无法模拟auth?请参阅:)。 我的问题是,当我点击服务页面上调用云函数的按钮时,它试图在访问云函数时:导致Cors问题。如何让我的web应用程序调用本地云函数进行测试?我怀疑它是index.html中firebaseConfig的一部分 我意识到我可能需要启用Cors,因为云功能所服务的端口是不同的。但这与我所问的问题不同 服务本地Firebase web应用尝试非本地云功能 总结,firebase,google-cloud-functions,cors,Firebase,Google Cloud Functions,Cors,我试图通过为本地web应用提供服务并调用它们来测试云函数的授权(因为模拟器无法模拟auth?请参阅:)。 我的问题是,当我点击服务页面上调用云函数的按钮时,它试图在访问云函数时:导致Cors问题。如何让我的web应用程序调用本地云函数进行测试?我怀疑它是index.html中firebaseConfig的一部分 我意识到我可能需要启用Cors,因为云功能所服务的端口是不同的。但这与我所问的问题不同 服务 我通过cmdfirebase服务提供我的云功能和index.html 其结果是: i f
我通过cmd
firebase服务提供我的云功能和index.html
其结果是:
i functions: Watching "/Users/plum/firebase-test/test1/functions" for Cloud Functions...
i hosting: Serving hosting files from: public
✔ hosting: Local server: http://localhost:5000
✔ functions[returnAuth]: http function initialized (http://localhost:5001/MY_PROJECT/us-central1/returnAuth).
错误
简单云函数
简单索引.html
测试1
$(文档).ready(函数(){
$(“#Test1”)。在('click',function()上{
var testAuth=firebase.functions().httpscalable('returnAuth');
testAuth({text:“Testing”}).then(函数(结果){
var sanitizedMessage=result.data.text;
console.log(净化消息);
})
.catch(函数(错误){
var代码=error.code;
var message=error.message;
var details=error.details;
log(`Code:${Code}\t消息:${Message}\t详细信息:${Details}`)
})
})
})
//您的web应用程序的Firebase配置
var firebaseConfig={
apiKey:“隐藏”,
authDomain:“MY-PROJECT.firebaseapp.com”,
数据库URL:“https://MY-PROJECT.firebaseio.com",
projectId:“我的项目”,
storageBucket:“MY-PROJECT.appspot.com”,
messagingSenderId:“一些数字”,
appId:“1:散列:web:其他\u散列”,
measurementId:“G-HASH”
};
//初始化Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
我尝试过的SO帖子列表:
简单修复,查看了文档并意识到有一个使用仿真器的函数设置
添加
firebase.functions().useFunctionsEmulator("http://localhost:5001");
我的index.html脚本运行正常。模拟器确实支持身份验证。您可以进行身份验证,它可以很好地与仿真器配合使用。他们不能模仿auth。所以你使用的是真正的服务,而不是本地模拟的模拟服务。@Kato是的,我说这个词选得不好,但这就是我的意思
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { exampleDataSnapshotChange } = require('firebase-functions-test/lib/providers/database');
admin.initializeApp();
exports.returnAuth = functions.https.onCall( (data, context) => {
//const uid = context.auth.uid;
//const name = context.auth.token.name || null;
const test = context.auth
return test
})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
</head>
<body>
<div id="app">
<button id="Test1">Test 1</button>
</div>
<script>
$(document).ready(function() {
$("#Test1").on('click', function() {
var testAuth = firebase.functions().httpsCallable('returnAuth');
testAuth({text: "Testing"}).then(function(result) {
var sanitizedMessage = result.data.text;
console.log(sanitizedMessage);
})
.catch(function(error) {
var code = error.code;
var message = error.message;
var details = error.details;
console.log(`Code: ${code}\t Message: ${message}\t Details: ${details}`)
})
})
})
</script>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.19.0/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.19.0/firebase-analytics.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.19.0/firebase-functions.js"></script>
<script src="/__/firebase/7.19.0/firebase-auth.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "HIDDEN",
authDomain: "MY-PROJECT.firebaseapp.com",
databaseURL: "https://MY-PROJECT.firebaseio.com",
projectId: "MY-PROJECT",
storageBucket: "MY-PROJECT.appspot.com",
messagingSenderId: "SOME_NUMBER",
appId: "1:HASH:web:OTHER_HASH",
measurementId: "G-HASH"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
</body>
</html>
firebase.functions().useFunctionsEmulator("http://localhost:5001");