0

I followed an online video tutorial on how to make a login system with fire base and Java script. I have copied it work for word but it does not work. I should be able to hit sign up and an alert should appear but non of the onclick functions seem to work. Help would be appreciated. I have looked in the console on inspect element as well but I cant seem to rectify the errors they are putting forward

<!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://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";></script>
<script src="https://www.gstatic.com/firebasejs/9.0.1/firebase-app.js";></script>
<script src="form.js"></script>
    <title>Login System</title>
</head>
<body>
    <div class="formContainer">
    <h1>Enter Credentials Here:</h1>
    <input type="email" placeholder="email here" id="email"></input>
    <input type="password" placeholder="password here" id="password"></input>
    <button onclick="signUp()" id="signUp">SignUp</button>
    <button onclick="signIn()" id="signIp">SignIn</button>
    <button onclick="signOut()" id="signOut">SignOut</button>
</div>


    
</body>
</html>
var firebaseConfig = {
    apiKey: "AIzaSyAlbLt0YTa41lbutVo1-ic5nPV9Yri9-vE",
    authDomain: "loginv2-b2f0e.firebaseapp.com",
    projectId: "loginv2-b2f0e",
    storageBucket: "loginv2-b2f0e.appspot.com",
    messagingSenderId: "144798003676",
    appId: "1:144798003676:web:257d3f649f532bb6d51f19",
    measurementId: "G-X6QG246ZF6"
  };

  const app = initializeApp(firebaseConfig);;

  const auth = firebase.auth();

   //signup function
   function signUp(){
    var email = document.getElementById("email");
    var password = document.getElementById("password");

    const promise = auth.createUserWithEmailAndPassword(email.value,password.value);
    //
    promise.catch(e=>alert(e.message));
    alert("SignUp Successfully");
  }
  
  
  //signIN function
  function  signIn(){
    var email = document.getElementById("email");
    var password  = document.getElementById("password");
    const promise = auth.signInWithEmailAndPassword(email.value,password.value);
    promise.catch(e=>alert(e.message));
    
  }


   //signOut

   function signOut(){
    auth.signOut();
    alert("SignOut Successfully from System");
  }



    //active user to homepage
    firebase.auth().onAuthStateChanged((user)=>{
        if(user){
          var email = user.email;
          alert("Active user "+email);
    
        }else{
          alert("No Active user Found")
        }
      })
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Have you tried setting the button type to `type="button"`? Also can you make sure the function is running by adding a log statement in it ? – Dharmaraj Sep 06 '21 at 13:43
  • @Dharmaraj i added a console.log("test") and that does appear in the console along side these errors Uncaught ReferenceError: initializeApp is not defined at form.js:11 8form.js:21 Uncaught ReferenceError: Cannot access 'auth' before initialization at signUp (form.js:21) at HTMLButtonElement.onclick (index.html:18) form.js:21 Uncaught ReferenceError: Cannot access 'auth' before initialization at signUp (form.js:21) at HTMLButtonElement.onclick (index.html:18) – Ellis Conrad Sep 06 '21 at 13:56
  • Try `const app = firebase.initializeApp(firebaseConfig)` – Dharmaraj Sep 06 '21 at 13:58
  • @Dharmaraj that gives an error saying firebase is not defined uncaught SyntaxError: Unexpected token 'export' firebase-app.js:1590 Uncaught SyntaxError: Unexpected token 'export' form.js:10 Uncaught ReferenceError: firebase is not defined at form.js:10 (anonymous) @ form.js:10 form.js:20 Uncaught ReferenceError: Cannot access 'auth' before initialization at signUp (form.js:20) at HTMLButtonElement.onclick (index.html:18) – Ellis Conrad Sep 06 '21 at 14:11
  • You're importing `firebase-app` twice. One of these should be `` So that's `-auth` because you also need to import the auth SDK, and `-compat` because you're using the older style `auth.method(...)` for the authentication calls. – Frank van Puffelen Sep 06 '21 at 14:34
  • You should probably remove your firebase config values, for security reasons. Not a big deal, we won't be using your info, but someone out there may. – Matt U Sep 06 '21 at 14:35
  • @FrankvanPuffelen i have changed one of the app imports to the compact auth however i still receive errors as followed firebase-auth-compat.js:1 Uncaught TypeError: Cannot read property 'INTERNAL' of undefined at firebase-auth-compat.js:1 at firebase-auth-compat.js:1 (anonymous) @ firebase-auth-compat.js:1 (anonymous) @ firebase-auth-compat.js:1 form.js:10 Uncaught ReferenceError: firebase is not defined at form.js:10 – Ellis Conrad Sep 06 '21 at 15:23
  • 1
    @MattU: https://stackoverflow.com/questions/37482366/is-it-safe-to-expose-firebase-apikey-to-the-public – Frank van Puffelen Sep 06 '21 at 15:32
  • @FrankvanPuffelen ah thanks! I'm clearly not very familiar with firebase, I just assumed API keys and other config info shouldn't be shared. Thanks for the clarification. – Matt U Sep 06 '21 at 16:43

0 Answers0