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
Add support for beforeSendEmail trigger #1492
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM, only nit comments.
/** Defines the auth event context for blocking events */ | ||
export interface AuthEventContext extends EventContext { | ||
locale?: string; | ||
ipAddress: string; | ||
userAgent: string; | ||
additionalUserInfo?: AdditionalUserInfo; | ||
credential?: Credential; | ||
emailType?: EmailType; | ||
} | ||
|
||
/** Defines the auth event for 2nd gen blocking events */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a comment say that for beforeEmailSent and beforeSmsSent, this field will be undefined?
export type RecaptchaActionOptions = "ALLOW" | "BLOCK"; | ||
|
||
/** The handler response type for `beforeCreate` blocking events */ | ||
export interface BeforeEmailResponse { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a API level comment here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And maybe rename this to BeforeEmailSentResponse.
@@ -391,6 +399,7 @@ interface DecodedPayloadUserRecordEnrolledFactors { | |||
export interface DecodedPayloadUserRecord { | |||
uid: string; | |||
email?: string; | |||
email_type?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we do not need email_type in the userrecord correct?
@@ -853,16 +863,20 @@ export function wrapHandler(eventType: AuthBlockingEventType, handler: HandlerV1 | |||
|
|||
const decodedPayload: DecodedPayload = isDebugFeatureEnabled("skipTokenVerification") | |||
? unsafeDecodeAuthBlockingToken(req.body.data.jwt) | |||
: handler.length === 2 | |||
: handler.platform === "gcfv1" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does gcfv1 stand for? can we add a explanation here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also put this string as a constant?
? await auth.getAuth(getApp())._verifyAuthBlockingToken(req.body.data.jwt) | ||
: await auth.getAuth(getApp())._verifyAuthBlockingToken(req.body.data.jwt, "run.app"); | ||
const authUserRecord = parseAuthUserRecord(decodedPayload.user_record); | ||
let authUserRecord: AuthUserRecord | undefined; | ||
if (decodedPayload.user_record) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be we should use decodedPayload.event_type value in the if statement. This helps eliminate the backend error.
* @param opts - Object containing function options | ||
* @param handler - Event handler that is run before an email is sent to a user. | ||
*/ | ||
export function beforeEmailSent( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think beforeEmaiSent can provide options, since this is not an authentication flow. And the 3 field in options are only coming from an authenticated user. Please see this
@@ -41,6 +41,15 @@ const BEFORE_SIGN_IN_TRIGGER = { | |||
}, | |||
}; | |||
|
|||
const BEFORE_EMAIL_TRIGGER = { | |||
eventType: "providers/cloud.auth/eventTypes/user.beforeSendEmail", | |||
options: { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can remove the options here. See comments in v2/providers/identity.ts
}); | ||
expect(fn.__requiredAPIs).to.deep.equal([ | ||
{ | ||
api: "identitytoolkit.googleapis.com", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put these into constant? "identitytoolkit.googleapis.com", "gcfv1", ["us-west-1"] etc?
GCIP now supports a new event type that triggers before emails are sent using Identity Platform. This PR adds a new trigger to respond to the
beforeSendEmail
event type. Based on changes fromblidd.blocking-fn-email
.