Supreet Patil
Supreet Patil

Reputation: 21

how to validate whether a specific email exist in the firebase database and how to fetch it for log in validation

Actually I have created a registration form but having a little logical problem in the login page. In the log-in page whenever a user enters data and presses the submit button he/she should be directed to their specific page by validating whether that specific email entered on the input fields is present in the firebase.

  <div class="row">
        <div class="container">
            <h2>Contact Form</h2>
            <div class="alert alert-success success-message" style="display:none;">Form submitted successfully.</div>
            <form id="contactForm">

                <div class="form-group">
                    <label for="exampleEmail">Email</label>
                    <input type="email" class="form-control email" id="exampleEmail" placeholder="Enter Email" required>
                </div>
                <div class="form-group">
                    <label for="exampleEmail">password</label>
                    <input type="password" class="form-control email" id="examplePass" placeholder="Enter Email" required>
                </div>

                <button type="submit" class="btn btn-primary">Submit</button>
            </form>
            <div id ="data"></div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <script src="https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.1.0/firebase-database.js"></script>
    <script>
var email, password ;
        var firebaseConfig = {
          apiKey: "AIzaSyDTMz1qgSStS6b6NLkikEVDeI-ZTkCLyEM",
          authDomain: "venderapp-c42cf.firebaseapp.com",
          databaseURL: "https://venderapp-c42cf.firebaseio.com",
          projectId: "venderapp-c42cf",
          storageBucket: "venderapp-c42cf.appspot.com",
          messagingSenderId: "882847151210",
          appId: "1:882847151210:web:6b7392dbc76b437386c613"
        };
        firebase.initializeApp(firebaseConfig);

      $('#contactForm').submit(function(e) {
          e.preventDefault();
             email=document.getElementById("exampleEmail").value;
          password=document.getElementById("examplePass").value;
            firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {

      console.log("logged in");
      var errorCode = error.code;
      var errorMessage = error.message;
      // ...
    });
    // ...
  });

      firebase.auth().onAuthStateChanged(user => {
        console.log(user);
        if(user) {
        console.log(user);
        }
      });

    </script>

Upvotes: 0

Views: 377

Answers (1)

bijaykumarpun
bijaykumarpun

Reputation: 707

I had the exact same problem when I was developing an Android app with Java. I could authenticate users with the Firebase Authentication SDK but was unable to check if the user existed in the database, and frankly they DO NOT exist in the database unless you explicitly write it there.

So what I did was as soon as the user's email was validated, say using Google Login or Facebook Login, I would retrieve the current user's email or user token and save it inside Users node. Firebase Realtime Database allows data querying, which was quite fast for the Free plan, so I guess it's ok doing so.

Sad to say the authentication part does not automatically save users data in the database. Unless the whole authentication system is based on the Firebase Realtime Database instead of something else.

Upvotes: 1

Related Questions