【问题标题】:Firebase - Authenticate user and then write to Database?Firebase - 验证用户然后写入数据库?
【发布时间】:2020-09-06 09:26:05
【问题描述】:

对 Firebase 来说是全新的。我想出了一种方法来验证我的用户,但我现在正在努力将它们添加到实时数据库中。这是我在注册页面上使用的代码。如何将用户添加到我的数据库中?

<style>.row{border: 1px solid blue; padding: 0px;margin-top: 0px;margin-bottom: :0px;;}.numbcell{background-color:#355DB2;color: #FFFFFF;text-align: right;font-family: 'Helvetica Neue', Helvetica, Arial, Verdana;}.cell{padding:0px;background-color: #FFF6A6;}.cellp{line-height: 110%;color: #05007E;padding: 0px;margin-top:0px;margin-bottom: 0px;font-family: Courier, 'Courier New';}.comment{color:#A1A1A1;}</style><script src='//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script><div><table class="codetable"><tbody><tr class="row"><td class="numbcell">1</td><td class="cell"><p class="cellp" style="margin-left: 0px;"><script></p></td></tr><tr class="row"><td class="numbcell"> </td><td class="cell"><p class="cellp" style="margin-left: 0px;"></p></td></tr><tr class="row"><td class="numbcell">2</td><td class="cell"><p class="cellp" style="margin-left: 30px;"> var signUpButton = document.getElementById("signUpButton")</p></td></tr><tr class="row"><td class="numbcell">3</td><td class="cell"><p class="cellp" style="margin-left: 30px;"> signUpButton.addEventListener('click', signup);</p></td></tr><tr class="row"><td class="numbcell"> </td><td class="cell"><p class="cellp" style="margin-left: 0px;"></p></td></tr><tr class="row"><td class="numbcell">4</td><td class="cell"><p class="cellp" style="margin-left: 30px;"> function signup() {</p></td></tr><tr class="row"><td class="numbcell">5</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> signUpButton.style.display = 'none';</p></td></tr><tr class="row"><td class="numbcell">6</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> signUpError.style.display = 'block';</p></td></tr><tr class="row"><td class="numbcell">7</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> var email = signUpEmail.value;</p></td></tr><tr class="row"><td class="numbcell">8</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> var password = signUpPassword.value;</p></td></tr><tr class="row"><td class="numbcell"> </td><td class="cell"><p class="cellp" style="margin-left: 30px;"> </p></td></tr><tr class="row"><td class="numbcell">9</td><td class="cell"><p class="cellp" style="margin-left: 30px;"> firebase.auth().createUserWithEmailAndPassword(email, password)</p></td></tr><tr class="row"><td class="numbcell">10</td><td class="cell"><p class="cellp" style="margin-left: 30px;"> .then(function () {</p></td></tr><tr class="row"><td class="numbcell">11</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> window.location.replace('./dashboard');</p></td></tr><tr class="row"><td class="numbcell"> </td><td class="cell"><p class="cellp" style="margin-left: 60px;"> </p></td></tr><tr class="row"><td class="numbcell">12</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> <span class="comment">// this is where I try and add uid and email to realtime database</span></p></td></tr><tr class="row"><td class="numbcell">13</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> function writeUserData(uid, email) {</p></td></tr><tr class="row"><td class="numbcell">14</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> firebase.database().ref('users/' + uid).update({</p></td></tr><tr class="row"><td class="numbcell">15</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> uid: uid,</p></td></tr><tr class="row"><td class="numbcell">16</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> email: email</p></td></tr><tr class="row"><td class="numbcell">17</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> })}</p></td></tr><tr class="row"><td class="numbcell"> </td><td class="cell"><p class="cellp" style="margin-left: 0px;"></p></td></tr><tr class="row"><td class="numbcell">18</td><td class="cell"><p class="cellp" style="margin-left: 30px;"> })</p></td></tr><tr class="row"><td class="numbcell">19</td><td class="cell"><p class="cellp" style="margin-left: 30px;"> .catch(function(error) {</p></td></tr><tr class="row"><td class="numbcell">20</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> var errorCode = error.code;</p></td></tr><tr class="row"><td class="numbcell">21</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> var errorMessage = error.message;</p></td></tr><tr class="row"><td class="numbcell">22</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> console.log('Error code: ' + errorCode);</p></td></tr><tr class="row"><td class="numbcell">23</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> console.log('Error message: ' + errorMessage);</p></td></tr><tr class="row"><td class="numbcell">24</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> signUpButton.style.display = 'block';</p></td></tr><tr class="row"><td class="numbcell">25</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> signUpError.innerText = errorMessage;</p></td></tr><tr class="row"><td class="numbcell">26</td><td class="cell"><p class="cellp" style="margin-left: 60px;"> signUpError.style.display = 'block';</p></td></tr><tr class="row"><td class="numbcell">27</td><td class="cell"><p class="cellp" style="margin-left: 0px;">});</p></td></tr><tr class="row"><td class="numbcell"> </td><td class="cell"><p class="cellp" style="margin-left: 0px;"></p></td></tr><tr class="row"><td class="numbcell">28</td><td class="cell"><p class="cellp" style="margin-left: 0px;">}</p></td></tr><tr class="row"><td class="numbcell">29</td><td class="cell"><p class="cellp" style="margin-left: 0px;"></script></p></td></tr></tbody></table><input type="button" value="Toggle line numbers" onclick="$(this).parent().parent().find('.numbcell').toggle(300)"></div>

【问题讨论】:

  • 请添加html代码完成sn-p。
  • @RayeesAC 你的意思是在帖子中还是上面的实际sn-p?
  • 你能添加html代码吗。
  • @RayeesAC 它已经在上面的帖子中了——你能看到吗?
  • 不行,问题只显示js。

标签: html firebase firebase-realtime-database firebase-authentication


【解决方案1】:

不要将要添加到实时数据库的代码包装在一个函数中,而是尝试立即调用代码,使用.set() 方法写入数据库,然后仅在该操作成功后重定向:

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(function() {
    const user = firebase.auth().currentUser;
    const uid = user.uid;
    const email = user.email;
    firebase.database().ref('users/' + uid).set({
      uid: uid,
      email: email
    })
      .then(() => window.location.replace('./dashboard'))
      .catch(console.error);
  });

但是,如果您只想存储电子邮件等信息,Firebase Auth 已将这些信息存储在 user's profile 中,因此您可以像这样访问它:

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    const uid = user.uid;
    const email = user.email;
    // ...
  }
});

【讨论】:

  • 你先生,让我度过了一个美好的夜晚。在过去的几个小时里,我一直在尝试这么多不同的迭代,因为我没有编码背景。
  • 快速提问。出于某种原因,当我尝试通过 Safari 注册新用户时,这不起作用 -> 没有新用户被添加到实时数据库中。任何想法为什么会这样?
  • 嗯,这很奇怪。浏览器开发工具的控制台中是否显示任何错误?还有一些其他的questions 可能有类似的问题,所以在那里寻找解决方案可能会很好。否则,在新问题中提出这可能是件好事?
猜你喜欢
  • 2017-11-11
  • 2017-06-16
  • 1970-01-01
  • 2018-07-02
  • 2017-11-15
  • 1970-01-01
  • 2016-06-19
  • 2020-12-07
  • 1970-01-01
相关资源
最近更新 更多