Run functions on Auth events
Register functions that listen and react to Firebase Auth events.
Blocking functions
run a function before the user is added to Firebase Auth
The function is blocking: We perform validation, and, if applicable, throw an error to deny the registration. Firebase Auth aborts user creation on throw. The Auth client SDK receives such error and can display it to the user"
export const onRegisterBlocking = beforeUserCreated(options, async (event) => {
const user = event.data // AuthUserRecord === UserRecord
// user.uid
// user.email
if (user?.email?.includes("@hotmail.com")) {
throw new HttpsError("invalid-argument", "don't use hotmail")
}
// create the user in the database first, then return
await createDefaultDataForUser(user)
return
})
Non blocking functions
The non blocking functions run after a user has been created (or deleted) by Firebase Auth.
As of writing, there is no v2 version for the non blocking functions.
export const f = auth.user().onCreate(async (user) => {})
export const g = auth.user().onDelete(async (user) => {})
example: add the user to the Firestore database
We read the auth user's uid and create a user document with it:
export const onRegisterNonBlocking = region("europe-west1")
.auth.user()
.onCreate(async (user) => {
const { uid, email } = user
await db.collection("users").doc(uid).set({ uid, email })
})
example: delete the user from the Firestore database
export const onDeleteAccount = region("europe-west1")
.auth.user()
.onDelete(async function (user) {
const { uid } = user
await db.doc("users/" + uid).delete()
})