如何使用android firebase数据库在webapp中集成firebase?

时间:2018-02-10 14:15:49

标签: javascript firebase web-applications firebase-authentication

android数据库是创建的,我想使用html页面对其进行身份验证。 它不是工作代码如下。 这里基本上我们有一个html登录页面和脚本来从fire-base数据库验证它。提供解决方案代码或我的代码有什么问题或其中缺少的东西。

 <!DOCTYPE html>
    <html>
    <head>

    <!-- /////////////////firebase method ///////////////////// -->
    <title>
      the login form
    </title>

    <script src="https://www.gstatic.com/firebasejs/4.9.1/firebase.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-database.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-firestore.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-messaging.js"></script>

    <script>
      // Initialize Firebase
      var config = {
        apiKey: "AIzaSyBgFUWzUrmLXVuKtGtChe2I2zvf5sYga54",
        authDomain: "skool-1083c.firebaseapp.com",
        databaseURL: "https://skool-1083c.firebaseio.com",
        projectId: "skool-1083c",
        storageBucket: "skool-1083c.appspot.com",
        messagingSenderId: "911580445409"
      };
      firebase.initializeApp(config);
    </script>

    </head>
    <body>

    <h1>Admin Login</h1>
    <form class="form" action="new_blank">
    <input type="text" placeholder="username" id="email"autofocus><br>
    <input type="password" placeholder="pasword" id="password" ><br></br>
    <input type="submit" value="login" id="sign-in"> 
    <br>



    </form>
    <!-- ///////////////stylesheet///////////// -->


     <script>    
        document.querySelector('#sign-in').addEventListener('click', function(e) {
          e.preventDefault();
          e.stopPropagation();
          var email = document.querySelector('#email').value;
          var password = document.querySelector('#password').value
          var credential = firebase.auth.EmailAuthProvider.credential(email, password);

          window.alert(credential);
            var auth = firebase.auth();
          var currentUser = auth.currentUser;

          // Step 2
          //  Get a credential with firebase.auth.emailAuthProvider.credential(emailInput.value, passwordInput.value)
          //  If there is no current user, log in with auth.signInWithCredential(credential)
          //  If there is a current user an it's anonymous, atttempt to link the new user with firebase.auth().currentUser.link(credential) 
          //  The user link will fail if the user has already been created, so catch the error and sign in.
        });
        </script>
    </body>

    </html>

2 个答案:

答案 0 :(得分:0)

如果您要在Firebase身份验证上使用电子邮件+密码登录用户,请按照文档here进行操作。从那里,登录的例子是:

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});

要检测用户何时登录,请按照文档here进行操作。从那里:

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

答案 1 :(得分:0)

  

在app / build.gradle中实现firebase身份验证

implementation 'com.google.firebase:firebase-auth:18.1.0'
implementation 'com.google.firebase:firebase-database:18.0.1'
  

通过此功能使用输入的用户名和密码

private void login() {
            FirebaseAuth firebaseAuth=FirebaseAuth.getInstance();
            firebaseAuth.signInWithEmailAndPassword(usrId, usrPass).addOnCompleteListener(loginActivity.this, new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if (!task.isSuccessful()) {
                        Toast.makeText(loginActivity.this, "Not sucessfull", Toast.LENGTH_SHORT).show();
                    } else {
                        startActivity(new Intent(loginActivity.this, MainActivity.class));
                    }
                }
            });