【问题标题】:firebase.auth().currentUser is returning null when i re-execute a script in node.js cli当我在 node.js cli 中重新执行脚本时,firebase.auth().currentUser 返回 null
【发布时间】:2019-11-01 04:26:07
【问题描述】:

我正在创建一个节点 cli,并在其中使用 firebase 身份验证

一切正常

有一个登录命令,当用户执行它时,它会要求提供凭据。提交后,我从 api 获得自定义身份验证令牌,然后使用 signinwithcustomtoken 并按预期获得“currentUser”的值。

根据此页面Access Firebase in your app 我可以在 node.js 应用程序中使用 firebase 客户端 sdk 进行身份验证

问题

但是当我执行另一个命令来显示配置文件数据时,当我尝试访问 firebase.auth().currentUser 它返回 null :(

我没有创建任何类型的服务器。

我认为每次运行命令时,firebase 都会重新创建一个新实例 但是为什么 firebase 不会在 Angular 中松散实例?

【问题讨论】:

    标签: node.js firebase firebase-authentication


    【解决方案1】:

    在 Node.js 部署中,出于安全原因,默认 Auth state persistence 模式为 firebase.auth.Auth.Persistence.NONE,如 Modifying the ASP 部分底部所述。这意味着无论何时关闭脚本都会清除用户状态。

    在使用您的身份验证令牌登录之前,您需要使用以下代码更改模式以匹配浏览器:

    firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL) // <-- this bit
      .then(function() {
        // Users will now be logged in until they explicitly log out
        return firebase.auth().signInWithCustomToken(token);
      })
      .catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
      });
    

    【讨论】:

    • 这样我得到“错误:当前环境不支持指定的持久性类型。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2020-09-30
    • 2018-11-13
    • 2020-09-26
    • 2014-11-22
    • 2017-11-26
    相关资源
    最近更新 更多