@@ -37,17 +37,23 @@ import {
3737 HttpsError ,
3838 wrapHandler ,
3939 MaybeAsync ,
40+ UserInfo ,
41+ UserRecord ,
42+ UserRecordMetadata ,
43+ userRecordConstructor ,
4044} from "../../common/providers/identity" ;
4145import { BlockingFunction } from "../../v1/cloud-functions" ;
4246import { wrapTraceContext } from "../trace" ;
4347import { Expression } from "../../params" ;
44- import { initV2Endpoint } from "../../runtime/manifest" ;
48+ import { CloudEvent , CloudFunction } from "../core" ;
49+ import { initV2Endpoint , ManifestEndpoint } from "../../runtime/manifest" ;
4550import * as options from "../options" ;
4651import { SecretParam } from "../../params/types" ;
4752import { withInit } from "../../common/onInit" ;
4853
4954export { HttpsError } ;
50- export type { AuthUserRecord , AuthBlockingEvent } ;
55+ export { UserRecordMetadata , userRecordConstructor } ;
56+ export type { AuthUserRecord , AuthBlockingEvent , UserRecord , UserInfo } ;
5157
5258/** @hidden Internally used when parsing the options. */
5359interface InternalOptions {
@@ -166,6 +172,84 @@ export interface BlockingOptions {
166172 secrets ?: ( string | SecretParam ) [ ] ;
167173}
168174
175+ /** @internal */
176+ export const userCreatedEvent = "google.firebase.auth.user.v1.created" ;
177+ /** @internal */
178+ export const userDeletedEvent = "google.firebase.auth.user.v1.deleted" ;
179+
180+ /** A CloudEvent that contains a Firebase Auth user record. */
181+ export type UserRecordEvent = CloudEvent < UserRecord > ;
182+
183+ type UserEventHandler = ( event : UserRecordEvent ) => any | Promise < any > ;
184+
185+ /**
186+ * Event handler which triggers when a Firebase Auth user is created.
187+ *
188+ * @param handler - Event handler which is run every time a Firebase Auth user is created.
189+ * @returns A function that you can export and deploy.
190+ */
191+ export function onUserCreated ( handler : UserEventHandler ) : CloudFunction < UserRecordEvent > ;
192+
193+ /**
194+ * Event handler which triggers when a Firebase Auth user is created.
195+ *
196+ * @param opts - Options that can be set on an individual event-handling function.
197+ * @param handler - Event handler which is run every time a Firebase Auth user is created.
198+ * @returns A function that you can export and deploy.
199+ */
200+ export function onUserCreated (
201+ opts : options . EventHandlerOptions ,
202+ handler : UserEventHandler
203+ ) : CloudFunction < UserRecordEvent > ;
204+
205+ /**
206+ * Event handler which triggers when a Firebase Auth user is created.
207+ *
208+ * @param optsOrHandler - Options or an event handler.
209+ * @param handler - Event handler which is run every time a Firebase Auth user is created.
210+ * @returns A function that you can export and deploy.
211+ */
212+ export function onUserCreated (
213+ optsOrHandler : options . EventHandlerOptions | UserEventHandler ,
214+ handler ?: UserEventHandler
215+ ) : CloudFunction < UserRecordEvent > {
216+ return onUserOperation ( userCreatedEvent , optsOrHandler , handler ) ;
217+ }
218+
219+ /**
220+ * Event handler which triggers when a Firebase Auth user is deleted.
221+ *
222+ * @param handler - Event handler which is run every time a Firebase Auth user is deleted.
223+ * @returns A function that you can export and deploy.
224+ */
225+ export function onUserDeleted ( handler : UserEventHandler ) : CloudFunction < UserRecordEvent > ;
226+
227+ /**
228+ * Event handler which triggers when a Firebase Auth user is deleted.
229+ *
230+ * @param opts - Options that can be set on an individual event-handling function.
231+ * @param handler - Event handler which is run every time a Firebase Auth user is deleted.
232+ * @returns A function that you can export and deploy.
233+ */
234+ export function onUserDeleted (
235+ opts : options . EventHandlerOptions ,
236+ handler : UserEventHandler
237+ ) : CloudFunction < UserRecordEvent > ;
238+
239+ /**
240+ * Event handler which triggers when a Firebase Auth user is deleted.
241+ *
242+ * @param optsOrHandler - Options or an event handler.
243+ * @param handler - Event handler which is run every time a Firebase Auth user is deleted.
244+ * @returns A function that you can export and deploy.
245+ */
246+ export function onUserDeleted (
247+ optsOrHandler : options . EventHandlerOptions | UserEventHandler ,
248+ handler ?: UserEventHandler
249+ ) : CloudFunction < UserRecordEvent > {
250+ return onUserOperation ( userDeletedEvent , optsOrHandler , handler ) ;
251+ }
252+
169253/**
170254 * Handles an event that is triggered before a user is created.
171255 * @param handler - Event handler which is run every time before a user is created.
@@ -293,6 +377,53 @@ export function beforeSmsSent(
293377 return beforeOperation ( "beforeSendSms" , optsOrHandler , handler ) ;
294378}
295379
380+ function onUserOperation (
381+ eventType : string ,
382+ optsOrHandler : options . EventHandlerOptions | UserEventHandler ,
383+ handler ?: UserEventHandler
384+ ) : CloudFunction < UserRecordEvent > {
385+ if ( typeof optsOrHandler === "function" ) {
386+ handler = optsOrHandler ;
387+ optsOrHandler = { } ;
388+ }
389+
390+ const baseOpts = options . optionsToEndpoint ( options . getGlobalOptions ( ) ) ;
391+ const specificOpts = options . optionsToEndpoint ( optsOrHandler ) ;
392+
393+ const func : any = ( raw : CloudEvent < unknown > ) => {
394+ const event = convertToUserRecordEvent ( raw ) ;
395+ return wrapTraceContext ( withInit ( handler ) ) ( event ) ;
396+ } ;
397+
398+ func . run = handler ;
399+
400+ const endpoint : ManifestEndpoint = {
401+ ...initV2Endpoint ( options . getGlobalOptions ( ) , optsOrHandler ) ,
402+ platform : "gcfv2" ,
403+ ...baseOpts ,
404+ ...specificOpts ,
405+ labels : {
406+ ...baseOpts ?. labels ,
407+ ...specificOpts ?. labels ,
408+ } ,
409+ eventTrigger : {
410+ eventType,
411+ eventFilters : { } ,
412+ retry : optsOrHandler . retry ?? false ,
413+ } ,
414+ } ;
415+
416+ func . __endpoint = endpoint ;
417+
418+ return func as CloudFunction < UserRecordEvent > ;
419+ }
420+
421+ function convertToUserRecordEvent ( raw : CloudEvent < unknown > ) : UserRecordEvent {
422+ const data = ( raw . data ?? { } ) as Record < string , unknown > ;
423+ const user = userRecordConstructor ( data ) ;
424+ return { ...raw , data : user } as UserRecordEvent ;
425+ }
426+
296427/** @hidden */
297428export function beforeOperation (
298429 eventType : AuthBlockingEventType ,
0 commit comments