Skip to content

Commit

Permalink
feat: add more webhook email types
Browse files Browse the repository at this point in the history
  • Loading branch information
lfleischmann committed Jan 28, 2025
1 parent b8c4771 commit 9a411e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
24 changes: 23 additions & 1 deletion backend/dto/webhook/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ type PasscodeData struct {

type EmailType string

func EmailTypeFromStashedTemplateName(stashedTemplateName string) EmailType {
switch stashedTemplateName {
case "login":
return EmailTypePasscode
case "email_login_attempted":
return EmailTypeLoginAttempt
case "email_verification":
return EmailTypeEmailVerification
case "email_registration_attempted":
return EmailTypeRegistrationAttempt
case "recovery":
return EmailTypeRecovery
default:
return EmailTypeUnknown
}
}

var (
EmailTypePasscode EmailType = "passcode"
EmailTypePasscode EmailType = "passcode"
EmailTypeLoginAttempt EmailType = "login_attempt"
EmailTypeRegistrationAttempt EmailType = "registration_attempt"
EmailTypeEmailVerification EmailType = "email_verification"
EmailTypeRecovery EmailType = "recovery"
EmailTypeUnknown EmailType = "unknown"
)
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ func (a ReSendPasscode) Execute(c flowpilot.ExecutionContext) error {
}
}

passcodeTemplate := c.Stash().Get(shared.StashPathPasscodeTemplate).String()

sendParams := services.SendPasscodeParams{
Template: c.Stash().Get(shared.StashPathPasscodeTemplate).String(),
Template: passcodeTemplate,
EmailAddress: c.Stash().Get(shared.StashPathEmail).String(),
Language: deps.HttpContext.Request().Header.Get("X-Language"),
}
Expand All @@ -71,7 +73,7 @@ func (a ReSendPasscode) Execute(c flowpilot.ExecutionContext) error {
DeliveredByHanko: deps.Cfg.EmailDelivery.Enabled,
AcceptLanguage: sendParams.Language,
Language: sendParams.Language,
Type: webhook.EmailTypePasscode,
Type: webhook.EmailTypeFromStashedTemplateName(passcodeTemplate),
Data: webhook.PasscodeData{
ServiceName: deps.Cfg.Service.Name,
OtpCode: passcodeResult.Code,
Expand Down
6 changes: 4 additions & 2 deletions backend/flow_api/flow/credential_usage/hook_send_passcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ func (h SendPasscode) Execute(c flowpilot.HookExecutionContext) error {

isDifferentEmailAddress := c.Stash().Get(shared.StashPathEmail).String() != c.Stash().Get(shared.StashPathPasscodeEmail).String()

passcodeTemplate := c.Stash().Get(shared.StashPathPasscodeTemplate).String()

if !passcodeIsValid || isDifferentEmailAddress {
sendParams := services.SendPasscodeParams{
Template: c.Stash().Get(shared.StashPathPasscodeTemplate).String(),
Template: passcodeTemplate,
EmailAddress: c.Stash().Get(shared.StashPathEmail).String(),
Language: deps.HttpContext.Request().Header.Get("X-Language"),
}
Expand All @@ -92,7 +94,7 @@ func (h SendPasscode) Execute(c flowpilot.HookExecutionContext) error {
DeliveredByHanko: deps.Cfg.EmailDelivery.Enabled,
AcceptLanguage: sendParams.Language,
Language: sendParams.Language,
Type: webhook.EmailTypePasscode,
Type: webhook.EmailTypeFromStashedTemplateName(passcodeTemplate),
Data: webhook.PasscodeData{
ServiceName: deps.Cfg.Service.Name,
OtpCode: passcodeResult.Code,
Expand Down

0 comments on commit 9a411e7

Please sign in to comment.