Skip to content

Commit

Permalink
chore: use gotemplate for invitation email
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Oct 23, 2024
1 parent a02961c commit e3ff957
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
21 changes: 15 additions & 6 deletions auth/controllers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"bytes"
"fmt"
"net/http"
"os"
Expand Down Expand Up @@ -69,19 +70,27 @@ func (k *KratosHandler) InviteUser(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, dutyAPI.HTTPError{Err: err.Error(), Message: "error creating recovery link"})
}

body := fmt.Sprintf(inviteUserTemplate, reqData.FirstName, recoveryLink, recoveryCode)
inviteMail := mail.New(reqData.Email, "User Invite", body, "text/html")
data := map[string]string{
"firstName": reqData.FirstName,
"link": recoveryLink,
"code": recoveryCode,
}

var body bytes.Buffer
if err := inviteUserTemplate.Execute(&body, data); err != nil {
return err
}

inviteMail := mail.New(reqData.Email, "User Invite", body.String(), "text/html")
if err = inviteMail.Send(); err != nil {
return c.JSON(http.StatusInternalServerError, dutyAPI.HTTPError{
Err: err.Error(),
Message: "Error sending email",
})
}

return c.JSON(http.StatusOK, map[string]string{
"link": recoveryLink,
"code": recoveryCode,
})
delete(data, "firstName")
return c.JSON(http.StatusOK, data)
}

func UpdateAccountState(c echo.Context) error {
Expand Down
21 changes: 18 additions & 3 deletions auth/templates.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package auth

const inviteUserTemplate = `
import (
"fmt"
"html/template"

"github.com/flanksource/duty/shutdown"
)

var inviteUserTemplate *template.Template

func init() {
parsed, err := template.New("email").Parse(`
<b>Welcome to Mission Control</b>
<br><br>
Hello %s,
Hello {{.firstName}},<br>
Please visit <a href="{{.link}}">{{.link}}</a> to complete registration and use the code: <code>{{.code}}</code>`)
if err != nil {
shutdown.ShutdownAndExit(1, fmt.Sprintf("failed to parse invitation email template: %v", err))
}

please visit <a href="%s">this link</a> to complete registration and use the code: <code>%s</code>`
inviteUserTemplate = parsed
}

0 comments on commit e3ff957

Please sign in to comment.