Firebase身份验证JavaScript获得成功 [英] Firebase Authentication JavaScript get success

查看:93
本文介绍了Firebase身份验证JavaScript获得成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下Firebase电子邮件认证码,您如何知道认证是否成功?

  firebase.auth()。 signInWithEmailAndPassword(电子邮件,密码).catch(功能(错误){
//这里处理错误
变种错误码= error.code;
变种的errorMessage =返回Error.message;

if(errorCode ==='auth / wrong-password'){
alert('Wrong password。');
} else {
alert(errorMessage);
}

console.log(error);
});

据我所知,认证是不是很成功,但是我怎么知道如果用户能够使用他们提供的凭证登录?似乎没有一个成功登录回调。我目前有一个登录表单,我想离开成功登录。

解决方案

这里的firebaser



@ ksav的答案显示了用户登录我们的首选方式进行。



有关完整性我想表明检测到这种第二方式,这是在以 signInWithEmailAndPassword 直接响应:

  firebase.auth()signInWithEmailAndPassword(电子邮件,密码)。然后(功能(用户){
/ /用户登录
})。catch(function(error){
var errorCode = error.code;
var errorMessage = error.message;

if errorCode ==='auth / wrong-password'){
alert('Wrong password。');
} else {
alert(errorMessage);
}
console.log(error);
});

then()用户已经登录。



在我们的文档中,我们不强调这种方法的原因是它会错过许多情况。例如:


  • 当用户重新载入页面时, then()将会不被调用。 用户可以获取 onAuthStateChanged()回调,另一方面 也会被调用。退出(例如,如果他们的短命令不能被刷新), catch()不会被调用。 b

    如果您想要在用户主动登录时明确回应,我的回答中的方法可能会有用。但在大多数情况下,我们建议您在@ ksav的答案中使用这种方法。

    With the following Firebase email authentication code, how do you know whether authentication was successful?

    firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
                // Handle Errors here.
                var errorCode = error.code;
                var errorMessage = error.message;
    
                if (errorCode === 'auth/wrong-password') {
                  alert('Wrong password.');
                } else {
                  alert(errorMessage);         
                }
    
                console.log(error);
            });
    

    I understand that it is easy to realise whether or not authentication was unsuccessful, however, how do I know if the user was able to login with their supplied credentials? There doesn't seem to be a callback for a 'successful' log in. I currently have a login form, and I want to navigate away on a successful log in.

    解决方案

    firebaser here

    @ksav's answer show the preferred way of detecting when a user signs in our out.

    For completeness I want to show the second way to detect this, which is in direct response to signInWithEmailAndPassword:

    firebase.auth().signInWithEmailAndPassword(email, password).then(function(user) {
       // user signed in
    }).catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;
    
        if (errorCode === 'auth/wrong-password') {
            alert('Wrong password.');
        } else {
            alert(errorMessage);         
        }
        console.log(error);
    });
    

    The then() will be invoked when the user has signed in.

    The reason we de-emphasize this approach in our documentation is that it will miss many situations. For example:

    • when a user reloads the page, the then() will not be invoked. The onAuthStateChanged() callback on the other hand will be invoked then too.
    • the a user gets signed out (for example if their short-lived token can't be refreshed), the catch() will not be invoked. The onAuthStateChanged() callback on the other hand will be invoked then too.

    If you want to explicitly respond to when the user actively signed in, the approach in my answer might be useful. But in most cases, we recommend that you that the approach in @ksav's answer.

    这篇关于Firebase身份验证JavaScript获得成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆