1

I'm currently working with Firebase Authentication and absolutely loving it. The only problem right now is that I'm trying to validate the user's display name using my own rules. What I have tried is to use Cloud Firestore to store the display name and other related info such as address, etc. On registration, the user has to declare his/her display name as well as email and password. Somewhere in my code I'm doing this:

try {
    await firebase.auth().createUserWithEmailAndPassword(email.value, password.value);

    await firebase
      .firestore()
      .collection('/users')
      .doc(firebase.auth().currentUser.uid)
      .set({
        displayName: displayName.value,
      });
  } catch (err) {
    setIsLoading(false);
    setErrorText(err.message);
  }

The problem is that the app also redirects the user when he/she finishes his/her registration to the private area like so:

useEffect(() => {
  if (authUser) history.push('/dashboard');
}, [authUser, history]);

(authUser is stored in Redux and is updated inside onAuthStateChanged()) The display name won't be validated because as soon as firebase.auth().createUserWithEmailAndPassword() resolves, authUser has a value and immediately redirects the user. It's kind of annoying and would love to know whether there's a better way of organising this bit of code.

Any help is greatly appreciated!

Edit: Firestore rules:

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{documentId} {
        allow read: if request.auth != null
      allow write: if request.auth != null &&
        request.resource.data.displayName is string && 
        request.resource.data.displayName.size() >= 6 && 
        request.resource.data.displayName.size() <= 20
    }
  }
}

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.