我正在尝试使用firebase auth和firebase函数实现基于角色的身份验证。现在,我有一个按预期方式工作的注册表格,现在我正尝试添加一个表格,以便您提交一封电子邮件,该电子邮件调用firebase函数,该函数将向用户附加自定义声明。我已经通过终端将函数添加到了Firebase中,并且正在项目中调用函数,但是每当我通过电子邮件提交表单时,都可以使用httpscallable并不是函数错误。
这是我的文件
函数文件夹中的index.js
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.addAdminRole = functions.https.onCall((data, context) => {
//get user and add custom claim (admin)
return admin
.auth()
.getUserByEmail(data.email)
.then(user => {
return admin.auth().setCustomUserClaims(user.uid, {
admin: true
});
})
.then(() => {
return {
message: `Success! ${data.email} has been made admin`
};
})
.catch(err => {
return err;
});
});
我的firebaseInit.js配置文件,在其中我将所有与Firebase相关的内容称为
import firebase from "firebase/app";
import "firebase/firestore";
import "@firebase/functions";
import firebaseConfig from "./firebaseConfig";
const firebaseApp = firebase.initializeApp(firebaseConfig);
export const fc = firebase.functions();
export const db = firebase.firestore();
export const fv = firebase.firestore.FieldValue;
export default firebaseApp.firestore();
表单所在的Vue组件
<template>
<div class="home">
<h3>Welcome to Site</h3>
<h3>Add user to admin</h3>
<div class="row">
<form @submit.prevent="addAdmin()" class="col s12">
<div class="row">
<div class="input-field col s12">
<input id="email" type="email" class="validate" v-model="email" />
<label for="email">Email</label>
</div>
</div>
<button type="submit" class="btn">Submit</button>
<router-link to="/members" class="btn grey">Cancel</router-link>
</form>
</div>
</div>
</template>
<script>
import firebase from "firebase/app";
import fc from "../data/firebaseInit";
export default {
name: "home",
data() {
return {
email: ""
};
},
methods: {
addAdmin() {
const addAdminRole = fc.httpsCallable("addAdminRole");
addAdminRole(this.email).then(result => {
console.log(result);
});
}
}
};
</script>
有人可以阐明为什么我会收到此错误吗?我忘了导入与Firebase相关的东西吗?
答案 0 :(得分:2)
您的问题来自这样的事实
export const fc = firebase.functions();
export const db = firebase.firestore();
export const fv = firebase.firestore.FieldValue;
export default firebaseApp.firestore();
在您的firebaseInit.js
文件中,您没有导出fc
(顺便说一下,也没有导出fv
)
以下应该可以工作
import firebase from "firebase/app";
import "firebase/firestore";
import "firebase/functions"; // <-- Note that the @ was removed
import firebaseConfig from "./firebaseConfig";
const firebaseApp = firebase.initializeApp(firebaseConfig);
const fc = firebase.functions();
const db = firebase.firestore();
const fv = firebase.firestore.FieldValue;
export { fc, db, fv };
然后,在您的组件中执行以下操作:
import {fc, db, fv} from "../data/firebaseInit";
//....
或者下面的内容,您仅需要fc
和db
,例如:
import {fc, db} from "../data/firebaseInit";
//....
答案 1 :(得分:1)
我可以用不同的方式解决这个问题。我正在使用 Vue 2.6。在我的组件中,我只需要:
import firebase from "firebase/app";
const functions = firebase.functions();
var addMessage = functions.httpsCallable("addMessage");
在我的 main.js 文件中,我有:
import Vue from "vue";
import App from "./App.vue";
import vuetify from "./plugins/vuetify";
import router from "./router";
import firebase from "firebase/app";
import "firebase/functions";
import store from "./store";
Vue.config.productionTip = false;
Vue.config.devtools = true;
Vue.prototype.$firebase = firebase;
// Your web app's Firebase configuration
const firebaseConfig = {
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
let app;
firebase.auth().onAuthStateChanged(user => {
if (!app) {
app = new Vue({
router,
store,
render: h => h(App)
}).$mount("#app");
}
在依赖项中:
"dependencies": {
"core-js": "^3.6.5",
"firebase": "^8.0.2",
"firebase-functions": "^3.13.2",
...
}
这基本上就是应用于 Vue.js 的文档 https://firebase.google.com/docs/functions/callable 的说明