我可以使用JavaScript在Firebase Firestore中成功检索文档。但是想针对用户的输入来验证该文档,以便将该人验证为进入页面。

使用控制台日志,我看到我已经检索到该记录,但是它与最终用户输入的文本不匹配。

这是如何完成的?

我正在使用Vuejs。并想用密码保护一页。

在jsfiddle中:https://jsfiddle.net/oL4bondy/4/

的HTML

        <div v-if="!isLoggedIn">
      <h2>Please log in first.</h2>
      <div class="form-group">
        <form @submit.prevent="validatePin">
          <div class="form-group">
            <input v-model="password" type="text" class="form-control" placeholder="Enter password">
          </div>
            <div class="form-group">
              <button type="submit" class="btn btn-primary text-center">Submit</button>
            </div>
        </form>
      </div>
    </div>


JS

    export default {
  name: "add-post",

  data() {
    return {
      password: "",
      isLoggedIn: ""
    };
  },
  methods: {
    validatePin() {
      const docRef = db.collection("passwords").doc("user");
      docRef
        .get()
        .then(function(doc) {
          if (doc.exists) {
            console.log("Document data:", doc.data());
          } else {
            console.log("No such document!");
          }
        })
        .catch(function(error) {
          console.log("Error getting document:", error);
        });
      let password = this.doc.data;
      if (this.userInput === password) {
        this.isLoggedIn = true;
      }
    }
  }
};

最佳答案

为此使用Firebase.auth()。特别是,.onAuthStateChanged方法/侦听器将在页面加载后触发。那是您要处理用户/ UI访问的地方。

关于javascript - 在Firestore中检索文档并根据用户输入进行验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49332338/

10-11 10:52
查看更多