简体   繁体   中英

Bulk Auth update transaction in Firebase

When we upload a CSV of new users, we're wanting to create a Firebase account for that user with a custom claim. Currently we are hitting quota limits exceeded when attempting to upload a CSV with many users. Not entirely sure which quota limit this is but suspect it could be the following -

  • New account creation 100 accounts/IP address/hour

The error message:

Exceeded quota for updating account information.

We are currently using createUser and then setCustomUserClaims , in a Promise.all() to create each user concurrently. This is hosted within a cloud function. Is there a way to do this as a bulk operation so that we do not hit quota limits?

Having investigated importUsers , it seems that it by-passes the checks for duplication of email address and the uid; maintaining the integrity of our data is vital.

  • This operation is optimized for bulk imports and will ignore checks on uid ,
  • email and other identifier uniqueness which could result in duplications.

Are there any elegant solutions to this?

await Promise.all(items.map(async user => {
  const userParams = {
    displayName: `${user.firstNames} ${user.lastNames}`,
    phoneNumber: user.mobileNumber,
    email: user.email || ''
  }

  this.logger.debug({ user, role }, 'Creating a new firebase user')
  const member = await this.auth.createUser(userParams)

  this.logger.debug({ member }, 'Attempting to set custom claims for user')
  await this.auth.setCustomUserClaims(member.uid, { role })

  const resetOptions = {
    url: `https://some-url`
  }

  const resetLink = await this.auth.generatePasswordResetLink(user.email, resetOptions)
  const passwordResetData = {
    role,
    link: resetLink,
    firstNames: user.firstNames,
    companyName: 'company name'
  }

  this.logger.debug({ user, passwordResetData }, 'Sending password reset link to user')
  return await this.notificationService.sendPasswordResetLink([user.email], passwordResetData)
}))

If you are trying to use the admin SDK from either a browser environment, or a non-secure server environment - that's when you hit limitations. If you run it however from a secure environment, the following applies:

The Firebase Admin SDK provides an API for managing your Firebase Authentication users with elevated privileges. The admin user management API gives you the ability to programmatically complete the following tasks from a secure server environment:

  • Create new users without any throttling or rate limiting.

https://firebase.google.com/docs/auth/admin/manage-users

Possible solution

The easiest is to use a cloud function . They are serverless functions which you can call either via https or in your app. You can create a callable function :

exports.createUsers = functions.https.onCall((data, context) => {
  const users = data.user
  // ...
});

and pass data in your frontend by calling:

functions.httpsCallable("createUsers").call(["users": [])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM