0

I am doing a restaurant app using JavaScript and jQuery mobile. It implements a user authentication using firebase for registering and signing the user. I am able to register a user and his details are stored in the database, but whenever I try to login with the same credentials, I have the error (‘Email address badly formatted’) in the console window.

Also, I want to be able to access the #homePage after successful login of the user.

Index.html

 <div data-role="page" id="login"  data-theme="a" data-rel = "#homePage"> 
                <div data-role="header" data-fullscreen="true" >
                    <a href="#homePage" data-icon="arrow-l" data-iconpos ="notext"></a>
                    <h1></h1>

                </div>
                <div role="main" class="ui-content" >


                        <div id="signup-form">
                          <h4>signup form</h4>
                          <input type="text" id="username" placeholder="enter your username"/><br/>
                          <input type="email" id="email" placeholder="enter your email"><br/>
                          <input type="password" id="password" placeholder="enter your password"/><br/>
                          <button type="button" name="button" id="signup">Signup</button>
                        </div>

                        <div id="signin">
                            <div id="signin">
                                <input type="email" id="signinEmail" placeholder="enter your email"><br/>
                                <input type="password" id="signinPassword" placeholder="enter your password"/><br/>
                                <button type="button" name="button" id="signin">Singin</button>
                                </div>
                        </div>
                        <hr/>                                       

                <div data-role="footer" data-position="fixed" >
                    <h1>&copy; 2018</h1>
                </div> -->

My second screen (where the user needs to be directed after successful login)

<div data-role="page" id="homePage" data-theme="a">
            <div data-role="header" data-fullscreen= "true" >
                <a href="#restoListDetails" data-icon="arrow-r" data-iconpos ="notext" data-transition="pop" >Home</a>                              
                <h1>Restaurant Near You</h1></div>
            <div role="main" class="ui-content" > 
                            <p>
                            <p id="current-email" style="display:none"></p>
                            <button type="button" id="signout" style="display:none">Signout</button>
                          </p>

                    <div id="map"></div> 
                    <br> 
                    <button id="getDetails"  data-role="button" data-icon="bars">Show Details</button>                  
            </div>

            <div data-role="footer" data-position="fixed" >
                    <h1>&copy; 2018</h1> 
            </div>
        </div>
    </div>

This is my app.js file (for authenticating the user)

function initFireBase() {
 // Initialize Firebase
 var config = {
apiKey: "**************************************",
authDomain: "restaurantapp-XXXXXX.firebaseapp.com",
databaseURL: "https://restaurantapp-XXXXXX.firebaseio.com",
projectId: "restaurantapp-XXXXXX",
storageBucket: "restaurantapp-XXXXXX.appspot.com",
messagingSenderId: "00000000000"
};

firebase.initializeApp(config);
var auth = firebase.auth();

// Firebase Variables
var auth = firebase.auth();

firebase.auth().onAuthStateChanged(function(user) {
if (user) {
 // User is signed in.
 } else {
// No user is signed in.
}
});

auth.onAuthStateChanged(firebaseUser => {
 // check email
if(firebaseUser){
currentEmail.innerHTML = auth.currentUser.email
currentEmail.style.display = 'block';
signoutButton.style.display = 'block';
// signupForm.style.display = 'none';
} else{
signoutButton.style.display = 'none';
// signupForm.style.display = 'block';
currentEmail.style.display = 'none';
}
});


//       ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
// signup form
// •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

 var currentEmail = document.querySelector("#current-email");

 var signupForm = document.querySelector("#signup-form");
 var userName = document.querySelector("#username");
 var email = document.querySelector("#email");
 var password = document.querySelector("#password");
 var signupButton = document.querySelector("#signup");

 signupButton.addEventListener("click", clickSignupButton);
 function clickSignupButton(){
 //signup firebase method
  auth.createUserWithEmailAndPassword(email.value, password.value).
 then(function(user){
  console.log(auth.currentUser.email)
 }, function(error) {
 console.log(error.message);
 });
 }

 var signoutButton = document.querySelector("#signout");
 var currentEmail = document.querySelector("#current-email");
 // Signout function
 function clickSignoutButton(){
 auth.signOut()
 }
 // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
 // singin form
 // •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••

var signinEmail = document.querySelector("#signinEmail");
var signinPassword = document.querySelector("#signinPassword");
var signinButton = document.querySelector("#signin");
signinButton.addEventListener("click", clickSigninButton);
function clickSigninButton() {
auth.signInWithEmailAndPassword(signinEmail.value.trim(),                     signinPassword.value).
 then(function(user){
   console.log(user)
 }, function(error) {
  console.log(error.message);
  // error message show that you need to enable that authentication in     firebase
 });
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Welcome to Stack Overflow. A tip: never ever *ever* post credentials here on SO. They will be available to the world. – mvreijn Nov 29 '18 at 20:04
  • Yes, thank you very much!! – K. von Salzen Nov 29 '18 at 20:11
  • 1
    As a basic debugging step, let's make sure the values are being pulled from the DOM correctly. Inside your `function clickSigninButton() { auth.signInWithEmailAndPassword(signinEmail.value.trim(), signinPassword.value).then(function(user){ ...etc..` function, try adding a console.log to check the values you're sending to Firebase.... right here: `function clickSigninButton() { console.log(signinEmail.value.trim()); console.log(signinPassword.value); auth.signInWithEmailAndPass.....etc...` – JeremyW Nov 29 '18 at 20:12
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Nov 30 '18 at 07:50
  • Your API key is still available in the question history, and you should ensure it is invalidated with your provider as soon as you can. – halfer Nov 30 '18 at 07:51
  • I know this isn't the OP's question, but while we're waiting for OP to respond... to everyone discussing the API key security, Firebase handles the API keys differently, check out one of the main Firebase engineers at Google (Frank)'s answer [here](https://stackoverflow.com/questions/37482366/is-it-safe-to-expose-firebase-apikey-to-the-public). Clients are expected to be able to access Firebase API keys - in fact it's necessary. Traditional API keys should be hidden, but Firebase uses it more as an identifier. – JeremyW Nov 30 '18 at 13:05

0 Answers0