【问题标题】:Firebase email/password authentication in electron电子中的 Firebase 电子邮件/密码身份验证
【发布时间】:2019-06-08 07:34:55
【问题描述】:

我在电子应用程序中使用电子邮件/密码进行了 Firebase 身份验证,它有效,但仅在第一页上。当我转到第二页时,我不再登录。因为我是 elecetron.js 和 firebase 的新手,所以我使用了这个教程:https://www.youtube.com/watch?v=bWS0ocfszmE

login.js

loginBtn.addEventListener('click', function() {
    var emailField = document.getElementById('email').value;
    var passwordField = document.getElementById('password').value;

    firebase.auth().signInWithEmailAndPassword(emailField, passwordField).then(function() {
        document.location.href = 'mainPage.html';
        console.log(firebase.auth().currentUser);

    }).catch(function(error) {
        if (error != null) {
            console.log(error.message);
            alertify.error(error.message);
            return;
        }
    });

secondpage.js

var firebase = require("firebase/app");
require("firebase/auth");
console.log(firebase.auth().currentUser);

我希望控制台中的输出与我登录的用户一起显示,但结果为空。

【问题讨论】:

  • 您能分享一下您是如何进行身份验证的吗?我无法在互联网上找到任何资源????您分享的视频目前是私人视频。
  • @VikramRayavarapu 我最终将 Angular 封装在那个 Electron 应用程序中。然后我使用文档link 中的Web 应用程序解决方案。最后得到一个登录用户,我使用了 onAuthStateChanged,在link 中提到。

标签: javascript firebase firebase-authentication electron


【解决方案1】:

问题在于,在每个新页面上,Firebase 身份验证都会检查用户的登录令牌是否仍然有效。由于这可能需要一些时间(并且需要调用服务器),因此这是异步发生的。当您的console.log(firebase.auth().currentUser) 运行时,该过程尚未完成。

这就是为什么您需要use an onAuthStateChanged listener 来检测用户的身份验证状态:

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

【讨论】:

    猜你喜欢
    • 2021-11-21
    • 2013-07-11
    • 2016-10-13
    • 2018-11-11
    • 2019-07-13
    • 2019-08-05
    • 2019-03-15
    • 2016-11-13
    • 1970-01-01
    相关资源
    最近更新 更多