I am new to firebase and I created a very simple app to signup user. After signing up, the user is not shown in the user list on my firebase console. I followed the documentation and some tutorials on Youtube. I feel I did the same thing, but it does not work. Can anyone help me solve this issue?
<!DOCTYPE html>
<html>
<head>
<title>Happy Referrals - Sign Up Free!</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
<div class="signup-container">
<form>
<h3>Sign Up Happy Referrals for Free!</h3>
<label>Email:</label>
<input type="email" name="" id="email" />
<label>Password:</label>
<input type="password" name="" id="pw" />
<button id="signup">Sign Up</button>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/7.2.2/firebase-app.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
https://firebase.google.com/docs/web/setup#available-libraries -->
<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.2.3/firebase-firestore.js"></script>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
// keep secret
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
$('#signup').on('click', function() {
var email = $('#email').val();
var pw = $('#pw').val();
console.log(email);
firebase.auth().createUserWithEmailAndPassword(email, pw).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
console.log(errorMessage);
});
})
</script>
</body>
</html>
2
Answers
As you will read in the the W3 specification your button has a default behavior of
submit
. Therefore your form is submitted before the Firebase method is triggered.So you need to explicitly specify the button type as follows:
Also, note that it is better, with JQUery, to wait for the document to be “ready”. See the doc, that says:
So you may adapt your
script
code as follows: