Skip to content

Commit

Permalink
new beforeemailtrigger with merge
Browse files Browse the repository at this point in the history
  • Loading branch information
blidd-google committed Sep 20, 2023
1 parent 74e9148 commit 5f32571
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 37 deletions.
24 changes: 8 additions & 16 deletions src/common/providers/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {
beforeCreate: "providers/cloud.auth/eventTypes/user.beforeCreate",
Expand Down Expand Up @@ -336,14 +336,18 @@ export interface AuthEventContext extends EventContext {

/** Defines the auth event for v2 blocking events */
export interface AuthBlockingEvent extends AuthEventContext {
data: AuthUserRecord;
data?: AuthUserRecord;
}

/**
* The reCAPTCHA action options.
*/
export type RecaptchaActionOptions = "ALLOW" | "BLOCK";

export interface BeforeEmailResponse {
recaptchaActionOverride?: RecaptchaActionOptions;
}

/** The handler response type for beforeCreate blocking events */
export interface BeforeCreateResponse {
displayName?: string;
Expand Down Expand Up @@ -453,23 +457,11 @@ export interface UserRecordResponsePayload
type HandlerV1 = (
user: AuthUserRecord,
context: AuthEventContext
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<void>;
) => BeforeEmailResponse | void | Promise<BeforeEmailResponse> | Promise<void>;

type HandlerV2 = (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<void>;
) => BeforeEmailResponse | void | Promise<BeforeEmailResponse> | Promise<void>;

/**
* Checks for a valid identity platform web request, otherwise throws an HttpsError
Expand Down
65 changes: 44 additions & 21 deletions src/v2/providers/identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
AuthUserRecord,
BeforeCreateResponse,
BeforeSignInResponse,
BeforeEmailResponse,
HttpsError,
wrapHandler,
} from "../../common/providers/identity";
Expand Down Expand Up @@ -240,40 +241,62 @@ 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<BeforeEmailResponse> | void | Promise<void>
): 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<BeforeEmailResponse> | void | Promise<void>
): 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<BeforeEmailResponse> | void | Promise<void>),
handler?: (
event: AuthBlockingEvent
) => BeforeEmailResponse | Promise<BeforeEmailResponse> | void | Promise<void>
): BlockingFunction {
return beforeOperation("beforeSendEmail", optsOrHandler, handler);
}

/** @hidden */
export function beforeOperation(
eventType: AuthBlockingEventType,
optsOrHandler:
| BlockingOptions
| ((
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<void>),
) => BeforeEmailResponse | void | Promise<BeforeEmailResponse> | Promise<void>),
handler: (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<void>
) => BeforeEmailResponse | void | Promise<BeforeEmailResponse> | Promise<void>
): BlockingFunction {
if (!handler || typeof optsOrHandler === "function") {
handler = optsOrHandler as (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<void>;
) => BeforeEmailResponse | void | Promise<BeforeEmailResponse> | Promise<void>;
optsOrHandler = {};
}

Expand Down

0 comments on commit 5f32571

Please sign in to comment.