2

I'm building a react native app that uses Firebase for authentication. The problem arises due to the fact that Firebase automatically signs in a user after the account is created, and this leads to an issue with setting the user's data.

Firebase recommends using the onAuthStateChanged callback to observe when users log in and out. The cycle occurs because I need to set the user's local redux state after the account is created, but onAuthStateChanged runs immediately after the user is created and triggers the loading of the user's state before it is created.

When a user logs in or the app is restarted, I need to load their existing account information stored in the database. This requires a loadUserLocal method to be triggered at some point after the onAuthStateChanged callback notices a change in authentication status, but this seems to conflict with the way the creation of a new user is handled. The onAuthStateChanged callback basically hijacks the process leading to a call to loadUserLocal before the user's new account has had its initial data set.

It seems to me the easiest way to solve this would be if I could avoid Firebase's automatic sign in after the creation of a new account, but the only methods I could find to do this are rather complex and involve using Cloud Functions and/or creating a second admin account for the app that basically acts as a dummy admin. This seems like overkill.

The other methods I've tried to solve this end up calling loadUserLocal multiple times to basically ignore the first call that is useless because the data has not be saved yet.

The signUp method looks like this:

export async function signUp(dispatch, email, password) {
  const userData = {
    <...initial user data is set here...>
  }

  await auth.createUserWithEmailAndPassword(email, password)
  await saveUserLocal(userData)
  setUserData(dispatch, userData)
}

The problem arises because the auth.createUserWithEmailAndPassword method triggers the onAuthStateChanged method before saveUserLocal has a chance to run. And the change in authentication leads to loadUserLocal being called before saveUserLocal, so userData does not contain the necessary information.

I can think of ways to avoid this, but then I run into issues with the logging in and refreshing processes. They both need a call to loadUserLocal to be triggered in order to retrieve the necessary information for an existing user. Every time I try a method to solve one of these, the other appears to break. And it all looks like it is connected to the fact Firebase forces a log in after user creation, which means I cannot set up the user's account data before it is logged in and requires the data. I've tried saving the user's account details before creating the user with Firebase, but to save their information I need the user UID that is created by Firebase. This leads to the same dependency cycle as before.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

0 Answers0