使用社交媒体反应异步登录

时间:2018-08-24 18:31:43

标签: javascript reactjs firebase firebase-authentication

我正在尝试在我的网站上添加社交媒体登录名,但是出现一些错误,因为该案例在我的HTTP请求(Firebase)之前先返回:

firebase.auth().signInWithPopup

如何以某种方式使用异步功能来case等待请求完成?

代码如下:

    login(provider, info) {
      switch (provider) {
        case this.EMAIL:
          return firebaseAuth().signInWithEmailAndPassword(
          info.email,
          info.password
          ); 
          break;
        case this.EMAILREGISTER:
          break;
        case this.GOOGLE:
          var providerr = new firebase.auth.GoogleAuthProvider();
            firebase.auth().signInWithPopup(providerr).then(function(result) {
            // This gives you a Facebook Access Token. You can use it to access the Facebook API.
            var token = result.credential.accessToken;
            // The signed-in user info.
            var user = result.user;
            console.log(result);
            // ...
          }).catch(function(error) {
            // Handle Errors here.
            var errorCode = error.code;
            var errorMessage = error.message;
            // The email of the user's account used.
            var email = error.email;
            // The firebase.auth.AuthCredential type that was used.
            var credential = error.credential;
            console.log(error);
           // ...
          });  
          //Right here I need to return the token and user async
        break;
      }
    }
    //here is the function who is calling:
    handleLogin = () => {
        const { email, password } = this.state;
        if (!(email && password)) {
          notification('error', 'Favor informar o email e senha');
          return;
        }
        this.setState({
          confirmLoading: true
        });
        const self = this;
        let isError = false;
        Firebase.login(Firebase.Google, '')
          .catch(result => {
            const message =
              ......
       });
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用async / await,这样它将一直等到返回数据。这样的事情,只是为了让您入门:

       async login(provider, info) {
             try {
              switch (provider) {
                case this.EMAIL:
                  return firebaseAuth().signInWithEmailAndPassword(
                  info.email,
                  info.password
                  ); 
                  break;
                case this.EMAILREGISTER:
                  break;
                case this.GOOGLE:
                  var providerr = new firebase.auth.GoogleAuthProvider();
     // This gives you a Facebook Access Token. You can use it to access the Facebook API.
                    var result = await firebase.auth().signInWithPopup(providerr);
                    var token = result.credential.accessToken;
                    // The signed-in user info.
                    var user = result.user;
                    console.log(result);
                    // ...
                     break;
                  }
    }
    catch(error) {
                    // Handle Errors here.
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    // The email of the user's account used.
                    var email = error.email;
                    // The firebase.auth.AuthCredential type that was used.
                    var credential = error.credential;
                    console.log(error);
                   // ...
                  } 
              }
            }