简体   繁体   English

Firebase 中的批量身份验证更新事务

[英]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.当我们上传新用户的 CSV 文件时,我们希望为该用户创建一个具有自定义声明的 Firebase 帐户。 Currently we are hitting quota limits exceeded when attempting to upload a CSV with many users.目前,我们在尝试上传包含许多用户的 CSV 文件时超出了配额限制。 Not entirely sure which quota limit this is but suspect it could be the following -不完全确定这是哪个配额限制,但怀疑可能如下-

  • New account creation 100 accounts/IP address/hour新账户创建 100 个账户/IP 地址/小时

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.我们目前在Promise.all()使用createUser然后setCustomUserClaims来同时创建每个用户。 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;调查了importUsers ,它似乎绕过了对电子邮件地址和 uid 重复的检查; maintaining the integrity of our data is vital.保持我们数据的完整性至关重要。

  • This operation is optimized for bulk imports and will ignore checks on uid ,此操作针对批量导入进行了优化,并将忽略对uid检查,
  • email and other identifier uniqueness which could result in duplications. email和其他可能导致重复的标识符唯一性。

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.如果您尝试在浏览器环境或非安全服务器环境中使用管理 SDK,那么您就会遇到限制。 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. Firebase Admin SDK 提供了一个 API,用于管理具有提升权限的 Firebase 身份验证用户。 The admin user management API gives you the ability to programmatically complete the following tasks from a secure server environment:管理员用户管理 API 使您能够在安全的服务器环境中以编程方式完成以下任务:

  • Create new users without any throttling or rate limiting.在没有任何节流或速率限制的情况下创建新用户。

https://firebase.google.com/docs/auth/admin/manage-users 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.它们是无服务器函数,您可以通过 https 或在您的应用程序中调用它们。 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": [])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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