Home > Blockchain >  Firebase authentication error in javascript
Firebase authentication error in javascript

Time:10-19

I'm encountering an error when trying to add a user to firebase authentication. The const auth is defined before the script is loaded. I've tried calling with and without the app parameter. The user is not being added and I can't seem to figure this error out. I've cut out only the code I think is relevant. Apologies if this is a stupid question, I'm just diving into HTML and JS.

Error: Uncaught ReferenceError: auth is not defined

at HTMLFormElement. (auth.js:12)

From html file:

import { getAuth, createUserWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/9.1.3/firebase-auth.js";
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.1.3/firebase-app.js";
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
<script src="js\auth.js"></script>

From js file:

auth.createUserWithEmailAndPassword(email, password).then(cred => {
        console.log(cred);
    });

CodePudding user response:

You're using the modular SDK, which means that imported names are no longer added to a global namespace. If your include file (js.auth.js) needs access to the auth service, you will need to import getAuth into that file too and initialize it there as well.

  • Related