skip to Main Content

Currently, when the user goes through the Social auth (via redirects), it successfully creates a user under Firebase Authentication, but fails to retrieve the Google/Facebook API’s data. Perplexed as to how it’s failing to retrieve the data when the Auth is successful.

I used to only have the SignInWithPopup (which works perfectly fine), but am trying to get getRedirectResults to work too (for mobile).

Given the behavior I’ve been seeing, the problem is most likely with the getRedirectResults. Most likely not the social API setup, because otherwise the user would never get created in Authentication.

The following code resides in componentDidMount (this is a React.js app):

if (this.state.device === 'mobile') {
  firebase.auth().getRedirectResult().then(function(result) {
    console.log('login successful! Data is:');
    console.log(result);

    if (result.credential) {
      var provider = result.credential.providerId;
      self.handleSocialAuth(provider, result);
    }
  }).catch(function(error) {
    var errorCode = error.code;
    var errorMessage = error.message;
  });
}

results is supposed to contain the user’s data, keeps returning:

login successful! Data is:
{user: null}

I have not deviated from official docs examples, which is why I’m confused.

2

Answers


  1. firebase.auth().getRedirectResult() – is called after the page loads.
    Official doc link: https://firebase.google.com/docs/auth/web/google-signin

    Use the below method to retrieve the user that is logged in.

    firebase.auth().onAuthStateChanged(function(user) {
              console.log(user);
    });
    

    If no user is logged in the user will be null.

    Live example:

    checkAuthStatus(){
            firebase.auth().onAuthStateChanged(user => {
                this.setState({ btnWithImg: user });
    
                if(user !== null){
                    this.setState({ userIsLoggedIn: true });
                    this.props.toggleLogIn();
                }
            });
        }
    
    Login or Signup to reply.
  2. You should call firebase.auth().getRedirectResult() only when user has been authenticated. Example

    firebase.auth().onAuthStateChanged(user => {
      if (user) {
        const result = await firebase.auth().getRedirectResult();
        assert(result.user, 'user is empty')
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search