0

I have a Javascript code which I am using in my HTML page. The code get executed when a button is clicked on a form. Then it create a new user in firebase. After that it creates a new user in my database. Finally, it sign-in using firebase.

Form code is as below

<form id="register-form" class="form" action="" method="post">
   <div class="form-group row">
      <input type="text" id="name" name="name" class="form-control" placeholder="Name">
   </div>
   <div class="form-group row">
      <input type="text" id="address" name="address" class="form-control" placeholder="Address">
   </div>
   <div class="form-group row">
      <input type="number" id="phone" name="phone" class="form-control" placeholder="Phone">
   </div>
   <div class="form-group row">
      <input type="email" id="email" name="email" class="form-control" placeholder="Email">
   </div>
   <div class="form-group row">
      <input type="password" id="password" name="password" class="form-control" placeholder="Password">
   </div>
   <div class="form-group row">
      <input type="text" id="language" name="language" class="form-control" placeholder="Language">
   </div>
   <div class="form-group row">
      <button type="button" class="btn btn-primary green max round" onClick="registerUser()">Create Account</button>
   </div>
</form>

Javascript code is as below

<script >

    function registerUser() {
        alert("hi");

        var email = document.getElementById("email").value;
        var password = document.getElementById("password").value;


        //Create User with Email and Password
        firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
            var user = firebase.auth().currentUser;
            alert(user.email); // Optional


            var email = firebase.auth().currentUser.email;
            var name = document.getElementById("name").value;
            var address = document.getElementById("address").value;
            var phone = document.getElementById("phone").value;
            var language = document.getElementById("language").value;


                //alert(email);
                // console.log(email) contains email

                const options = {
                    method: 'POST',
                    url: 'Register',
                    headers: {
                        // set appropriate headers, assuming json here
                        //"Content-Type": "application/json",
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    // form body, assuming json
                    //body: JSON.stringify(email)
                body: `email=${email}&name=${name}&address=$(address}&phone=${phone}&${language}`
                }
                //alert(email);
                url = 'Register';
                fetch(url, options)
                    .then(response => loginUser(`${email}`,`${password}`))
                    .then(data => console.log(data))
                    .catch(e => console.error(e))


        }, function(error) {
            // Handle Errors here.
            var errorCode = error.code;
            var errorMessage = error.message;
            alert(errorMessage);
        });

    }

    function loginUser(email,password)
    {
        firebase.auth().signInWithEmailAndPassword(email, password).then(function(firebaseUser) {
                //var email = firebase.auth().currentUser.email;
                //alert(email);
                // console.log(email) contains email
                const options = {
                    method: 'POST',
                    url: 'LoginValidator',
                    headers: {
                        // set appropriate headers, assuming json here
                        //"Content-Type": "application/json",
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    // form body, assuming json
                    //body: JSON.stringify(email)
                    body: `email=${email}`
                }
                //alert(email);
                url = 'LoginValidator';
                fetch(url, options)
                    .then(response => window.location.href = 'LoadCategoryList')
                    .then(data => console.log(data))
                    .catch(e => console.error(e))


            })

            .catch(function(error) {
                // Handle Errors here.
                var errorCode = error.code;
                var errorMessage = error.message;
                // [START_EXCLUDE]
                if (errorCode === 'auth/wrong-password') {
                    alert('Wrong password.');
                } else {
                    alert(errorMessage);
                }
                console.log(error);
                document.getElementById('quickstart-sign-in').disabled = false;
                // [END_EXCLUDE]
            });
    }
</script>

This works fine as expected. However when I convert this HTML page into a JSP, the Javascript code is malfunctioning. At that stage, it seems the following code is simply delivering some empty content to my servlet. The data is saved in database via the servlet but with blank content.

body: `email=${email}&name=${name}&address=$(address}&phone=${phone}&${language}`

What is going on in here?

UPDATE 1

I tried hardcoding data as below

body: `email=aug10@gmail.com&name=aug10&address=something&phone=0999090909&language=abcd`

The data is saved but still an error the email is badly formatted. Seems it is delivering empty data in body: 'email=${email}' of signInWithEmailAndPassword

PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • The `${...}` is the [JSP Expression Language](https://www.tutorialspoint.com/jsp/jsp_expression_language) - i.e. when you convert your HTML page into JSP file, this expressions are evaluated on the server, and, as there is most probably no variable named `email` on the server side, the `email=${email}` expression is evaluated to `email=`, i.e. empty. – Jozef Chocholacek Aug 05 '19 at 09:23
  • Perhaps https://stackoverflow.com/questions/8271033/how-to-escape-el-dollar-signs might help. – Jozef Chocholacek Aug 05 '19 at 09:24

0 Answers0