diff --git a/src/common/providers/identity.ts b/src/common/providers/identity.ts index 63fb298e7..d39d55317 100644 --- a/src/common/providers/identity.ts +++ b/src/common/providers/identity.ts @@ -55,7 +55,7 @@ const CLAIMS_MAX_PAYLOAD_SIZE = 1000; * @hidden * @alpha */ -export type AuthBlockingEventType = "beforeCreate" | "beforeSignIn"; +export type AuthBlockingEventType = "beforeCreate" | "beforeSignIn" | "beforeSendEmail"; const EVENT_MAPPING: Record = { beforeCreate: "providers/cloud.auth/eventTypes/user.beforeCreate", @@ -336,7 +336,7 @@ export interface AuthEventContext extends EventContext { /** Defines the auth event for v2 blocking events */ export interface AuthBlockingEvent extends AuthEventContext { - data: AuthUserRecord; + data?: AuthUserRecord; } /** @@ -344,6 +344,10 @@ export interface AuthBlockingEvent extends AuthEventContext { */ export type RecaptchaActionOptions = "ALLOW" | "BLOCK"; +export interface BeforeEmailResponse { + recaptchaActionOverride?: RecaptchaActionOptions; +} + /** The handler response type for beforeCreate blocking events */ export interface BeforeCreateResponse { displayName?: string; @@ -453,23 +457,11 @@ export interface UserRecordResponsePayload type HandlerV1 = ( user: AuthUserRecord, context: AuthEventContext -) => - | BeforeCreateResponse - | BeforeSignInResponse - | void - | Promise - | Promise - | Promise; +) => BeforeEmailResponse | void | Promise | Promise; type HandlerV2 = ( event: AuthBlockingEvent -) => - | BeforeCreateResponse - | BeforeSignInResponse - | void - | Promise - | Promise - | Promise; +) => BeforeEmailResponse | void | Promise | Promise; /** * Checks for a valid identity platform web request, otherwise throws an HttpsError diff --git a/src/v2/providers/identity.ts b/src/v2/providers/identity.ts index 3a0b1b7fc..3b890c010 100644 --- a/src/v2/providers/identity.ts +++ b/src/v2/providers/identity.ts @@ -31,6 +31,7 @@ import { AuthUserRecord, BeforeCreateResponse, BeforeSignInResponse, + BeforeEmailResponse, HttpsError, wrapHandler, } from "../../common/providers/identity"; @@ -240,6 +241,46 @@ export function beforeUserSignedIn( return beforeOperation("beforeSignIn", optsOrHandler, handler); } +/** + * Handles an event that is triggered before an email is sent to a user. + * @param handler - Event handler that is run before an email is sent to a user. + */ +export function beforeEmailSent( + handler: ( + event: AuthBlockingEvent + ) => BeforeEmailResponse | Promise | void | Promise +): BlockingFunction; + +/** + * Handles an event that is triggered before an email is sent to a user. + * @param opts - Object containing function options + * @param handler - Event handler that is run before an email is sent to a user. + */ +export function beforeEmailSent( + opts: BlockingOptions, + handler: ( + event: AuthBlockingEvent + ) => BeforeEmailResponse | Promise | void | Promise +): BlockingFunction; + +/** + * Handles an event that is triggered before an email is sent to a user. + * @param optsOrHandler- Either an object containing function options, or an event handler that is run before an email is sent to a user. + * @param handler - Event handler that is run before an email is sent to a user. + */ +export function beforeEmailSent( + optsOrHandler: + | BlockingOptions + | (( + event: AuthBlockingEvent + ) => BeforeEmailResponse | Promise | void | Promise), + handler?: ( + event: AuthBlockingEvent + ) => BeforeEmailResponse | Promise | void | Promise +): BlockingFunction { + return beforeOperation("beforeSendEmail", optsOrHandler, handler); +} + /** @hidden */ export function beforeOperation( eventType: AuthBlockingEventType, @@ -247,33 +288,15 @@ export function beforeOperation( | BlockingOptions | (( event: AuthBlockingEvent - ) => - | BeforeCreateResponse - | BeforeSignInResponse - | void - | Promise - | Promise - | Promise), + ) => BeforeEmailResponse | void | Promise | Promise), handler: ( event: AuthBlockingEvent - ) => - | BeforeCreateResponse - | BeforeSignInResponse - | void - | Promise - | Promise - | Promise + ) => BeforeEmailResponse | void | Promise | Promise ): BlockingFunction { if (!handler || typeof optsOrHandler === "function") { handler = optsOrHandler as ( event: AuthBlockingEvent - ) => - | BeforeCreateResponse - | BeforeSignInResponse - | void - | Promise - | Promise - | Promise; + ) => BeforeEmailResponse | void | Promise | Promise; optsOrHandler = {}; }