firebase-functions icon indicating copy to clipboard operation
firebase-functions copied to clipboard

Auth blocking function beforeUserCreated doesn't return within 7 seconds

Open tzappia opened this issue 6 months ago • 6 comments

Related issues

None

[REQUIRED] Version info

node: v22.6.0

firebase-functions: 5.0.1

firebase-tools: 13.15.2

firebase-admin: 12.3.1

[REQUIRED] Test case

Create blocking function:

export const beforeCreated = beforeUserCreated({
  region: 'us-west2',
}, async (event) => {
  const firestore = getFirestore();

  const newUser = {
    email: event.data.email,
    uid: event.data.uid,
  };

  const userDocRef = firestore.collection('users').doc();
  await userDocRef.create(newUser)
    .catch((error) => {
      return `Failed adding new user with email ${newUser.email}: '${error}'`;
    });

  return {
    customClaims: { fs_user_id: userDocRef.id },
  };
});

Create a new user in an app:

createUserWithEmailAndPassword(firebaseAuth, email, password);

[REQUIRED] Steps to reproduce

After deploying the blocking function, create a new user.

[REQUIRED] Expected behavior

Expect the user to be created.

[REQUIRED] Actual behavior

Auth user fails to create Error: Firebase: Cloud function deadline exceeded. (auth/internal-error).

According to the documentation, beforeUserCreated must return within 7 seconds, otherwise the error above will be encountered. My functions code doesn't have anything in global scope that would slow down cold starts. Functions and Firestore are in the same region. I suspect the fact that I am trying to write a Firestore document during beforeUserCreated may be slowing things down (perhaps the very first usage of Firestore is slow?) Subsequent requests are usually successful (once the cold start is complete.)

If there were a way to extend the 7 seconds that would avoid the error (better, but still not a great user experience because that is slow!) I could probably re-architect the solution to avoid the Firestore write inside beforeUserCreated if I were able to pass some custom properties either in the AuthBlockingEvent that beforeUserCreated receives or in what the function returns.

Note: the document that I'm writing to Firestore is actually written, so beforeUserCreated does complete successfully, just not within the required 7 seconds.

Were you able to successfully deploy your functions?

Yes

tzappia avatar Aug 21 '24 23:08 tzappia