Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for beforeSmsSent trigger BF #1478

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
35 changes: 33 additions & 2 deletions src/common/providers/identity.ts
@@ -1,4 +1,4 @@
// The MIT License (MIT)

Check failure on line 1 in src/common/providers/identity.ts

View workflow job for this annotation

GitHub Actions / unit (20.x)

Duplicate identifier 'RecaptchaActionOptions'.

Check failure on line 1 in src/common/providers/identity.ts

View workflow job for this annotation

GitHub Actions / unit (20.x)

Duplicate identifier 'RecaptchaActionOptions'.
//
// Copyright (c) 2022 Firebase
//
Expand Down Expand Up @@ -55,11 +55,17 @@
* @hidden
* @alpha
*/
export type AuthBlockingEventType = "beforeCreate" | "beforeSignIn";
export type AuthBlockingEventType =
| "beforeCreate"
| "beforeSignIn"
| "beforeSendEmail"
| "beforeSendSms";

const EVENT_MAPPING: Record<string, string> = {
beforeCreate: "providers/cloud.auth/eventTypes/user.beforeCreate",
beforeSignIn: "providers/cloud.auth/eventTypes/user.beforeSignIn",
beforeSendEmail: "providers/cloud.auth/eventTypes/user.beforeSendEmail",
beforeSendSms: "providers/cloud.auth/eventTypes/user.beforeSendSms",
};

/**
Expand Down Expand Up @@ -337,7 +343,24 @@

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

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

export type SmsType = "SIGNIN" | "MULTI_FACTOR_ENROLLMENT" | "MULTI_FACTOR_SIGN_IN";

export interface BeforeEmailResponse {
recaptchaActionOverride?: RecaptchaActionOptions;
}

export interface BeforeSmsResponse {
recaptchaActionOverride?: RecaptchaActionOptions;
phoneNumber?: string;
smsType?: SmsType;
}

/**
Expand Down Expand Up @@ -457,19 +480,27 @@
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| BeforeEmailResponse
| BeforeSmsResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<BeforeEmailResponse>
| Promise<BeforeSmsResponse>
| Promise<void>;

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

/**
Expand Down
6 changes: 6 additions & 0 deletions src/v1/providers/auth.ts
Expand Up @@ -25,7 +25,9 @@ import {
AuthEventContext,
AuthUserRecord,
BeforeCreateResponse,
BeforeEmailResponse,
BeforeSignInResponse,
BeforeSmsResponse,
HttpsError,
UserInfo,
UserRecord,
Expand Down Expand Up @@ -196,9 +198,13 @@ export class UserBuilder {
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| BeforeEmailResponse
| BeforeSmsResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<BeforeEmailResponse>
| Promise<BeforeSmsResponse>
| Promise<void>,
eventType: AuthBlockingEventType
): BlockingFunction {
Expand Down
94 changes: 94 additions & 0 deletions src/v2/providers/identity.ts
Expand Up @@ -31,8 +31,10 @@ import {
AuthUserRecord,
BeforeCreateResponse,
BeforeSignInResponse,
BeforeEmailResponse,
HttpsError,
wrapHandler,
BeforeSmsResponse,
} from "../../common/providers/identity";
import { BlockingFunction } from "../../v1/cloud-functions";
import { wrapTraceContext } from "../trace";
Expand Down Expand Up @@ -241,6 +243,86 @@ 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);
}

/**
* 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 beforeSmsSent(
handler: (
event: AuthBlockingEvent
) => BeforeSmsResponse | Promise<BeforeSmsResponse> | 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 beforeSmsSent(
opts: BlockingOptions,
handler: (
event: AuthBlockingEvent
) => BeforeSmsResponse | Promise<BeforeSmsResponse> | 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 beforeSmsSent(
optsOrHandler:
| BlockingOptions
| ((
event: AuthBlockingEvent
) => BeforeSmsResponse | Promise<BeforeSmsResponse> | void | Promise<void>),
handler?: (
event: AuthBlockingEvent
) => BeforeSmsResponse | Promise<BeforeSmsResponse> | void | Promise<void>
): BlockingFunction {
return beforeOperation("beforeSendSms", optsOrHandler, handler);
}

/** @hidden */
export function beforeOperation(
eventType: AuthBlockingEventType,
Expand All @@ -251,18 +333,26 @@ export function beforeOperation(
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| BeforeEmailResponse
| BeforeSmsResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<BeforeEmailResponse>
| Promise<BeforeSmsResponse>
| Promise<void>),
handler: (
event: AuthBlockingEvent
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| BeforeEmailResponse
| BeforeSmsResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<BeforeEmailResponse>
| Promise<BeforeSmsResponse>
| Promise<void>
): BlockingFunction {
if (!handler || typeof optsOrHandler === "function") {
Expand All @@ -271,9 +361,13 @@ export function beforeOperation(
) =>
| BeforeCreateResponse
| BeforeSignInResponse
| BeforeEmailResponse
| BeforeSmsResponse
| void
| Promise<BeforeCreateResponse>
| Promise<BeforeSignInResponse>
| Promise<BeforeEmailResponse>
| Promise<BeforeSmsResponse>
| Promise<void>;
optsOrHandler = {};
}
Expand Down