Firebase Invite user by email and add to a company

Viewed 24

I have an React app where each user belongs to a company. The first person in that company is the manager, who can invite other users. They'll get an email link to sign up.

How do I attach the companyId (or another piece of info to connect them) to the invite so that when a new user is invited and authenticated this person is added to the company?

I tought of 2 possible solutions.

  1. Adding an invitedEmail array to the company collection and run a cloud function to check if a new users email belongs to the company.
  2. Add somewhere the id of the company in the invite and get this id while new user signs up. But don't know where or how.
1 Answers

You can create 2 primary collections - companies and users. When the first user joins, create the company document with companyId as the doc ID and also user's own document (and store companyId as field). Whenever the manager adds a user, you can create another document in users collection with same companyId as of the manager, store a status field as "invited" and send a sign in link to user.

You can use a Callable Cloud Function as shown below:

exports.inviteUser = functions.https.onCall((data, context) => {
    const { email } = data;
    const { uid } = context.auth;

    // 1. Find user's companyId using caller's UID
    // 2. Add a document in users collection invited user with their email
    // 3. Generate a sign in link and email to user.
 
    return { message: "User invited" } 
});

You can generate a sign-in link using the Firebase Admin SDK. You'll need a third party service to send the link via e-mail. Firebase Trigger Email extension should make that easier.

Related