Skip to content

Authentication not consistently persisting between sessions #980

@willbattel

Description

@willbattel

Issue

Some of our users (about 20% at this point) are losing their authentication between sessions. This includes terminating the app, or simply leaving it in the background for a while. The effected users have to re-login every session. For those 20% it is happening very frequently.

We offer both Facebook and Phone authentication. The problem is not discriminating between providers- both are experiencing this.

This is the onAuthStateChanged subscription code in our root component.

componentDidMount() {
    this.unsubscribeFromAuth = Firebase.auth().onAuthStateChanged((user) => {
        if (user && user.uid) {
            Firebase.auth().currentUser.getIdToken(true).then((token) => {
                if (!token) {
                    Helpers.logout(false);
                }
                else {
                    // init rest of app, send user to primary navigation component, etc...
                }
            })
            .catch(() => {
                Helpers.logout(false);
            });
        }
        else
            // send user to login screen
    });
}

componentWillUnmount() {
    if (this.unsubscribeFromAuth) {
        this.unsubscribeFromAuth();
        this.unsubscribeFromAuth = null;
    }
}

This is the logout method. If the parameter passed is false, an error message appears. The users getting logged out are not getting the error message. We have confirmed that the error message is functioning properly for other cases. This means that the logout() function isn't being called.

logout(intentional) {

    const logoutMessage = (intentional) ? 'You have been logged out.' : 'Sorry! Something went wrong and you were signed out.';
    // show logoutMessage

    Firebase.auth().signOut().then(() => {
        console.log('signed out');
        LoginManager.logOut();
        Firebase.analytics().logEvent('logout', { error: (intentional) ? 0 : 1 });
        return Storage.delete('credential');
    })
    .then(() => {
        console.log('deleted credential');
        // go to login screen
    })
    .catch((error) => {
        Log.error(error);
        // go to login screen
    });
}

It is possible that this is related to #962. There seems to be a correlation between the users affected by both issues, but it is not a 100% match by any means.

Environment

  1. Application Target Platform: iOS (we target both but do not have enough Android users to know if it is affecting Android)
  1. Development Operating System: macOS High Sierra
  1. Build Tools: Xcode 9.2, iOS 10.0 through 11.3
  1. React Native version: 0.54.4
  1. RNFirebase Version: 3.3.1
  1. Firebase Module: Authentication

Any ideas? Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions