javascript - firebase.auth().createCustomToken 在网络应用程序上未定义

标签 javascript firebase firebase-authentication

我正在尝试为使用 Firebase 的网络应用程序创建“记住我”功能。

我的目标是,当用户点击“记住我”并成功登录时,他们的身份验证 token 将使用 localStorage 存储,并且他们会在下次访问时自动登录(为了安全起见,我更喜欢将其原始凭据存储在 localStorage 中目的)。

但是,当我尝试调用 firebase.auth().createCustomToken 时,我收到一条错误消息,提示它不存在。这是错误的方法还是错误的 firebase 函数调用?谢谢!

这是我的代码示例:

var customToken = firebase.auth().createCustomToken(user.uid);
localStorage.setItem("savedToken", customToken);

然后我打算使用这条线重新登录:

firebase.auth().signInWithCustomToken(localStorage.getItem("savedToken")).then(function() {

最佳答案

firebase.auth().createCustomToken仅在服务器 API 中可用。

要使用电子邮件和密码进行身份验证并获取 session token ,请尝试以下操作:

firebase.auth().signInWithEmailAndPassword(email, password)
  .then(function(user) {
    user.getToken().then(function(token) {
      localStorage.setItem("savedToken", token); // store the token
    });
  })
  .catch(function(error) {
    // handle error...
  });

这也适用于其他身份验证方法;只需使用 User.getToken .稍后,如果您有一个 token (仍在客户端上),并且您想要使用它进行身份验证,只需执行您当前正在执行的操作即可:

var token = localStorage.getItem("savedToken"); // get stored token
firebase.auth().signInWithCustomToken(token).catch(function(error) {
  // handle error...
});

以上代码不起作用,因为 signInWithCustomToken 仅适用于您的服务器使用 createCustomToken 生成的 token 。我不确定如何使用电子邮件/密码 token 进行身份验证。

关于javascript - firebase.auth().createCustomToken 在网络应用程序上未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39197279/

相关文章:

javascript - 尝试写入 firestore 时“RestConnection 提交失败并出现错误”

android - 名称为 [DEFAULT] 的 FirebaseApp 不存在并出现错误

javascript - 每次使用javascript在其他页面上单击按钮时如何自动增加一个数字

php - 动态 CSS 和 Javascript

firebase - 为什么 Firebase 功能部署极其缓慢?

angular - 检索新创建的用户 Firebase 的 ID

firebase - 无法从 createUserWithEmailAndPassword 的 firebase 响应访问 stsTokenManager

android - 如何从 FCM token 获取 Firebase 用户 ID? (在服务器上的管理员代码中)

javascript - 通过 Javascript 关闭 iFrame

javascript - 百分比分布 - JavaScript Math.random()