message: permmissions set on signup
diff: -import { firestore, Change } from "firebase-functions";
  -import { auth } from "../config";
  +import { firestore, Change, auth as FCAuth } from "firebase-functions";
  +import { auth, db } from "../config";
  +
  const email2uid = async (email: string) => {
  const user = await auth.getUserByEmail(email);
  console.log(
  @@ -8,49 +9,88 @@ const email2uid = async (email: string) => {
  return user ? user.uid : undefined;
  };
  -const permissions = (customClaimsFields: string[]) => async (
  - change: Change<FirebaseFirestore.DocumentSnapshot>
  +const setClaims = async (
  + uid: string,
  + doc: any,
  + customClaimsFields: string[]
  ) => {
  - const after = change.after;
  - const afterData = after.data();
  - if (!afterData) {
  - console.log("no after Data");
  - return false;
  - }
  - const { email } = afterData;
  - if (!email) {
  - console.warn(`row has no email`);
  - return false;
  - }
  - const uid = await email2uid(email);
  const customClaimsTokens = customClaimsFields.reduce(
  (token: any, currentField: string) => {
  - if (afterData[currentField])
  - return { ...token, [currentField]: afterData[currentField] };
  + if (doc[currentField])
  + return { ...token, [currentField]: doc[currentField] };
  else return token;
  },
  {}
  );
  - if (uid) {
  +
  await auth
  .setCustomUserClaims(uid, customClaimsTokens)
  .catch(err => console.log(err))
  .then(() => {
  console.info(
  - `${email} has new claims ${JSON.stringify(customClaimsTokens)}`
  + `${uid} has new claims ${JSON.stringify(customClaimsTokens)}`
  );
  })
  .catch(() => "obligatory catch");
  return true;
  - } else {
  - console.warn(`user with email ${email} does not exist`);
  +};
  +export const onSignup = (
  + collectionName: string,
  + customTokenFields: string[]
  +) => async (user: FCAuth.UserRecord) => {
  + if (user.emailVerified) {
  + const email = user.email;
  + const invites = await db
  + .collection(collectionName)
  + .where("email", "==", email)
  + .get();
  + if (invites.docs.length === 0) {
  + console.log("no invites found");
  + return false;
  + }
  +
  + const inviteData = invites.docs[0].data();
  + await setClaims(user.uid, inviteData, customTokenFields);
  + return true;
  + }
  + console.error("email not verified on signup");
  + return false;
  +};
  +
  +const permissions = (customClaimsFields: string[]) => async (
  + snapshot: FirebaseFirestore.DocumentSnapshot
  +) => {
  + const docData = snapshot.data();
  + if (!docData) {
  + console.log("no after Data");
  + return false;
  + }
  + const { email } = docData;
  + if (!email) {
  + console.warn(`row has no email`);
  + return false;
  + }
  + const uid = await email2uid(email);
  + if (!uid) {
  + console.warn(`user ${uid} does not exist`);
  return false;
  }
  + await setClaims(uid, docData, customClaimsFields);
  + return true;
  };
  -const permissionsFnsGenerator = collection =>
  - firestore
  +const permissionsFnsGenerator = collection => ({
  + onCreate: firestore
  + .document(`${collection.name}/{docId}`)
  + .onCreate(permissions(collection.customTokenFields)),
  + onUpdate: firestore
  .document(`${collection.name}/{docId}`)
  - .onUpdate(permissions(collection.customTokenFields));
  + .onUpdate((change: Change<FirebaseFirestore.DocumentSnapshot>) =>
  + permissions(collection.customTokenFields)(change.after)
  + ),
  + onSignup: FCAuth.user().onCreate(
  + onSignup(collection.name, collection.customTokenFields)
  + ),
  +});
  export default permissionsFnsGenerator;
  