Created
September 13, 2019 05:04
-
-
Save shadow1349/8f53ba3ff5becd796d4cca5e26894f6c to your computer and use it in GitHub Desktop.
Firestore trigger functions for users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as admin from "firebase-admin"; | |
import * as functions from "firebase-functions"; | |
import { IUser } from "firebasenoteapptypes"; | |
import { checkStorageItemsAndDeleteOldFile } from "../utilities/storage"; | |
export const UserCreated = functions.firestore | |
.document("Users/{UserId}") | |
.onCreate((snapshot, context) => { | |
const user = snapshot.data() as IUser; | |
return admin.auth().updateUser(user.Id, { | |
displayName: `${user.FirstName} ${user.LastName}` | |
}); | |
}); | |
export const UserUpdated = functions.firestore | |
.document("Users/{UserId}") | |
.onUpdate(async (change, context) => { | |
const before = change.before.data() as IUser; | |
const after = change.after.data() as IUser; | |
if ( | |
before.FirstName !== after.FirstName || | |
before.LastName !== after.LastName || | |
before.ProfilePhoto !== after.ProfilePhoto || | |
before.Disabled !== after.Disabled | |
) { | |
await admin.auth().updateUser(context.params.UserId, { | |
displayName: `${after.FirstName} ${after.LastName}`, | |
photoURL: after.ProfilePhoto, | |
disabled: after.Disabled | |
}); | |
} | |
return checkStorageItemsAndDeleteOldFile({ | |
before: before.ProfilePhoto, | |
after: after.ProfilePhoto | |
}); | |
}); | |
export const UserDeleted = functions.firestore | |
.document("Users/{UserId}") | |
.onDelete((snapshot, context) => { | |
const user = snapshot.data() as IUser; | |
return admin.auth().deleteUser(user.Id); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment