I've been using the Firebase JS sdk for web development without any issues for a year now. However, after recently updating my code from version 5.3.1
to the latest 6.5.0
SDK version, I encountered the following error:
TypeError: firebase.auth is not a function
Although I understand what this error message means, I'm puzzled as to why it's occurring. I cross-referenced the updated documentation, and all functions and method names appear to be the same.
This was my previous solution:
// HTML
<script src="https://www.gstatic.com/firebasejs/5.3.1/firebase-app.js"></script>
// JS
var config = {
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
};
firebase.initializeApp(config);
const email = txtEmail.value;
const pass = txtPassword.value;
const auth = firebase.auth();
firebase.auth().createUserWithEmailAndPassword(email, pass).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
showAlert(errorMessage, 'Error!');
}
if (errorCode == 'auth/email-already-in-use') {
showAlert(errorMessage, 'Error!');
}
if (errorCode == 'auth/invalid-email') {
showAlert(errorMessage, 'Error!');
}
else {
// alert(errorMessage);
//alert(error);
}
});
firebase.auth().onAuthStateChanged(firebaseUser => {
if (firebaseUser) {
$.ajax({
type: "POST",
contentType: "application/json",
url: "/user/",
data: JSON.stringify({""),
success: function(data) {
},
async: false
});
The part of the code that has changed which is causing issues is as follows:
// HTML
<script src="https://www.gstatic.com/firebasejs/6.5.0/firebase-app.js"></script>
// JS
var config = {
apiKey: "...",
authDomain: "...",
databaseURL: "...",
projectId: "...",
storageBucket: "...",
messagingSenderId: "...",
appid: "...."
};
Only the version number has been updated to 6.5.0 and an appid has been added to the config variable. Prior to this update, there were no appids in use, so this change seems related to a new feature in Firebase.
My question is whether there have been changes to how the SDK initializes, such as requiring additional linked SDKs or modifications to JavaScript function names. I find the situation confusing since everything worked smoothly until I switched to the new SDK version, indicating that the issue lies with the update itself.