1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-25 15:42:01 +02:00

otp changes - added sentTo field, allow e.Record to be nil when requesting OTP, etc.

This commit is contained in:
Gani Georgiev
2024-11-13 18:34:43 +02:00
parent 10a5c685ab
commit 9f606bdeca
12 changed files with 339 additions and 27 deletions

View File

@@ -40,6 +40,8 @@ func SendRecordAuthAlert(app core.App, authRecord *core.Record) error {
}
// SendRecordOTP sends OTP email to the specified auth record.
//
// This method will also update the "sentTo" field of the related OTP record to the mail sent To address (if the OTP exists and not already assigned).
func SendRecordOTP(app core.App, authRecord *core.Record, otpId string, pass string) error {
mailClient := app.NewMailClient()
@@ -72,7 +74,44 @@ func SendRecordOTP(app core.App, authRecord *core.Record, otpId string, pass str
}
return app.OnMailerRecordOTPSend().Trigger(event, func(e *core.MailerRecordEvent) error {
return e.Mailer.Send(e.Message)
err := e.Mailer.Send(e.Message)
if err != nil {
return err
}
var toAddress string
if len(e.Message.To) > 0 {
toAddress = e.Message.To[0].Address
}
if toAddress == "" {
return nil
}
otp, err := e.App.FindOTPById(otpId)
if err != nil {
e.App.Logger().Error(
"Failed to find OTP to update its sentTo field",
"error", err,
"otpId", otpId,
)
return nil
}
if otp.SentTo() != "" {
return nil // was already sent to another target
}
otp.SetSentTo(toAddress)
if err = e.App.Save(otp); err != nil {
e.App.Logger().Error(
"Failed to update OTP sentTo field",
"error", err,
"otpId", otpId,
"to", toAddress,
)
}
return nil
})
}