首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用Firebase登录用户时,“auth/ user - signing”

在使用Firebase登录用户时,“auth/ user - signing”
EN

Stack Overflow用户
提问于 2016-08-30 13:34:48
回答 2查看 13.3K关注 0票数 6

我有一个防火墙应用程序连接到monaca和OnsenUI。我正在尝试创建一个用户并将他们登录到相同的操作中。我可以成功地创建一个用户,但是我无法登录。

代码语言:javascript
复制
auth/user-not-found 

代码语言:javascript
复制
There is no user record corresponding to this identifier. The User may have been deleted

我确认新用户在db...Here中是我注册和签名的代码。

代码语言:javascript
复制
//signup function stuff
var login = function() {
  console.log('got to login stuff');
  var email = document.getElementById('username').value;
  var password = document.getElementById('password').value;

  //firebases authentication code
  firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;
    console.log('User did not sign up correctly');
    console.log(errorCode);
    console.console.log(errorMessage);
  });

  firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
     console.log(error.code);
     console.log(error.message);
  });

  fn.load('home.html');


};
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-30 14:31:57

你的流动中有一种所谓的种族状况。

当您调用createUserWithEmailAndPassword() Firebase时,启动,创建用户帐户。但是这可能需要一些时间,所以浏览器中的代码继续执行。

它立即在signInWithEmailAndPassword()中继续进行。由于Firebase可能仍在创建用户帐户,此调用将失败。

在这种情况下,通常的解决方案是将调用链接在一起,例如使用then()

代码语言:javascript
复制
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
  firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
     console.log(error.code);
     console.log(error.message);
  });
}).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  console.log('User did not sign up correctly');
  console.log(errorCode);
  console.console.log(errorMessage);
});

但是正如AndréKool已经评论过的:创建一个用户已经自动注册了,所以在本例中,您可以这样做:

代码语言:javascript
复制
firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
  // User is created and signed in, do whatever is needed
}).catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  console.log('User did not sign up correctly');
  console.log(errorCode);
  console.console.log(errorMessage);
});

当他们到达您的页面时,您可能很快也想要检测用户是否已签名。。为此,您将使用onAuthStateChanged。从医生那里:

代码语言:javascript
复制
firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});
票数 6
EN

Stack Overflow用户

发布于 2020-11-06 13:52:00

异步/等待工作也是如此。

代码语言:javascript
复制
(async () => {

        try {
            const result = await auth().createUserWithEmailAndPassword(email, password).signInWithEmailAndPassword(email, password);
            console.log(result);

        } catch (error) {
            console.error(error);
        }

    })()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39229014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档