我正在制作一个使用firebase进行后端数据存储和用户身份验证的网站。我把所有的firebase设置和导入都放在一个文件中。网站的每个页面都有自己的脚本文件,这些文件将使用不同的firebase功能。
现在我正在开发一个需要使用firebaseauth函数的登录页面。
有没有办法从一个单独的文件访问auth函数(和其他firebase函数),或者我需要在所有文件中包含firebase-sdk.js的所有内容?
我使用的是一个基本的html环境,没有框架或bundler
在我的代码中,我有一个名为firebase-sdk.js的文件,其中包含了我将要使用的firebase功能的所有导入语句。
//Import firebase
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.5.0/firebase-app.js";
//Import firebase features
import { getFirestore,
collection,
addDoc,
onSnapshot,
doc,
updateDoc,
deleteDoc
} from "https://www.gstatic.com/firebasejs/10.5.0/firebase-firestore.js"
import { getAuth,
onAuthStateChanged,
signInWithEmailAndPassword
} from "https://www.gstatic.com/firebasejs/10.5.0/firebase-auth.js"
//Firebase configuration
const firebaseConfig = { --- };
//Initialise firestore
const app = initializeApp(firebaseConfig);
//Initialise features
const auth = getAuth(app)
const db = getFirestore(app)
我有另一个文件login.js,我想在其中使用firebase auth函数,但程序找不到auth函数的定义。
const loginBtn = document.getElementById('id_submit_login_btn')
const loginBtnPressed = async () => {
const loginEmail = document.getElementById('id_email_entry').value
const loginPassword = document.getElementById('id_password_entry').value
const userCredentials = await signInWithEmailAndPassword(auth, loginEmail, loginPassword)
console.log(userCredentials.user)
}
loginBtn.addEventListener('click', loginBtnPressed)