简体   繁体   English

我们如何让一个用户使用 Firebase/Firestore 注册另一个用户?

[英]How can we make a user sign up another user using Firebase/Firestore?

I know it's possible to sign up a user with email and password with the following code:我知道可以使用以下代码使用 email 和密码注册用户:

FirebaseAuth.instance.createUserWithEmailAndPassword(
          email: email,
          password: password;)

That works fine.那很好用。 I can also add documents and collections in Firestore as such:我还可以在 Firestore 中添加文档和 collections,如下所示:

Future<void> getUserDoc() {
  // Initiate an auth instance
  final FirebaseAuth _auth = FirebaseAuth.instance;
  // Initiate a firebase firestore instance
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;
  // Create a current user object
  User user = _auth.currentUser!;
  // Create a document reference based on the 'users'
  DocumentReference ref = _firestore.collection('users').doc(user.uid);

  // Return a collection that sets/updates the following data
  // based on the user.uid
  return ref.set({
    'UID': user.uid,
    'Email': user.email,
    'CreatedUserAt': user.metadata.creationTime,
    'LastSignInAt': user.metadata.lastSignInTime,
    'EmailVerified': user.emailVerified,
  });
}

What I need is that the user that is signed in now - for example a business, can sign up their clients.我需要的是现在登录的用户 - 例如企业,可以注册他们的客户。 Their clients will receive a link in their email to confirm and then they are signed up to the platform.他们的客户将在他们的 email 中收到一个链接以进行确认,然后他们就可以注册到该平台。 However, the tricky part is that those clients should be added to the business that signed them up through the business account when they log in. I'm really stuck with this part of how this logic would work in Firestore.然而,棘手的部分是这些客户在登录时应该被添加到通过企业帐户注册的企业中。我真的很困惑这个逻辑在 Firestore 中的工作方式。

In my understanding, users with business account can invite client via an email and when the client signs up using the link, you need to add that client under this business.据我了解,拥有企业帐户的用户可以通过 email 邀请客户,当客户使用该链接注册时,您需要将该客户添加到该业务下。 You can use a Cloud functions to invite a client along with business account ID of inviter as shown below:您可以使用云功能邀请客户以及邀请人的企业帐户 ID,如下所示:

exports.inviteClient = functions.https.onCall((data, context) => {
  // UID of user inviting client (or the business ID)
  const { uid } = context.auth; 

  // Add data in a database that maps user email with business ID
  // When user signs up, you can refer to this data to find who invited user

  // Send email link to sign up user

  return; 
});

After user signs up, you can then read ID of business who invited user and add it in users collection.用户注册后,您可以读取邀请用户的企业ID并将其添加到用户集合中。 I would recommend running this logic in a Cloud function so no user can change it from client side.我建议在云 function 中运行此逻辑,这样用户就无法从客户端更改它。 Or if you'll use Firestore to store the mapping as well then you can use security rules to verify inviter ID.或者,如果您还将使用 Firestore 来存储映射,那么您可以使用安全规则来验证邀请者 ID。


Another approach could be creating a user in inviteClient function itself and add a custom claim containing business ID.另一种方法是在inviteClient function 本身中创建一个用户,并添加一个包含企业 ID 的自定义声明


If you do not want to use Cloud functions then you can add a user document containing business ID right after someone is invited and use security rules to ensure users cannot change their inviter's ID.如果您不想使用云功能,那么您可以在邀请某人后立即添加包含企业 ID 的用户文档,并使用安全规则确保用户无法更改其邀请者的 ID。 However, you'll need a Cloud function/server to generate sign in links for users and send emails.但是,您需要一个云功能/服务器来为用户生成登录链接并发送电子邮件。

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

相关问题 注册用户并添加到 Firestore - Sign up user and add to firestore 我正在使用电话号码登录 firebase android 中的用户,如何让用户保持登录状态直到用户注销? - I'm using phone number to to sign in user in firebase android ,how can i keep user signed in until user sign out? Firebase 注册前验证用户 - Firebase verify user before sign up 如何使用 Firebase Firestore 在 Futter 中显示用户名和个人资料图片 - How to show user name and profile picture in Futter using Firebase Firestore Firebase / Firestore如何存储用户数据 - Firebase / Firestore how to store user data 如何将当前用户的数据存储到 Firebase Firestore? - How to store data to Firebase Firestore for current user? 如何在 firebase firestore 中保护数组以供用户访问? - How to secure an array for user access in firebase firestore? Firebase 支付成功后才注册用户 - Firebase sign user up only after successful payment 我们如何在 ios swift 上对 firebase / firestore 查询进行分页? - How can we paginate a firebase / firestore query on ios swift? 在 Flutter 中,如果我们在 firestore 中有日期参数,我们如何使用 firebase 获取特定日期范围之间的数据? - In Flutter how can we able to get the data between specific date ranges using firebase if we had date parameter in firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM