Files
warmbly/internal/notify/templates/reset_password.go
T

76 lines
3.1 KiB
Go

package templates
import (
"bytes"
"fmt"
"html/template"
"time"
"github.com/warmbly/warmbly/internal/observability/errs"
)
const resetPasswordContent = `
<p style="margin:0 0 4px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;font-size:11px;color:#94a3b8;letter-spacing:0.14em;text-transform:uppercase;font-weight:500;">
Account
</p>
<h2 style="margin:0 0 8px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;font-weight:600;font-size:18px;color:#0f172a;letter-spacing:-0.01em;">
Reset your password
</h2>
<p style="margin:0 0 24px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;font-size:13px;color:#475569;line-height:20px;">
We received a request to reset your password. Click the button to choose a new one. If you didn't request this, you can safely ignore this email.
</p>
<table cellpadding="0" cellspacing="0" border="0" align="center" role="presentation" style="margin:0 0 24px;">
<tr>
<td align="center" style="border-radius:6px;background:#0f172a;">
<a href="{{.ResetURL}}" target="_blank" style="display:inline-block;padding:10px 22px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;font-size:13px;font-weight:600;color:#ffffff;text-decoration:none;letter-spacing:0.01em;">Reset password</a>
</td>
</tr>
</table>
<div style="margin:0 0 16px;height:1px;background:#e2e8f0;"></div>
<p style="margin:0 0 6px;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;font-size:11px;color:#94a3b8;letter-spacing:0.08em;text-transform:uppercase;font-weight:500;">
Link expires in {{.Expiry}}
</p>
<p style="margin:0;font-family:'SF Mono','Fira Mono','Roboto Mono','Courier New',monospace;font-size:11px;color:#64748b;word-break:break-all;line-height:18px;">
<a href="{{.ResetURL}}" style="color:#0f172a;text-decoration:none;">{{.ResetURL}}</a>
</p>
`
var resetPasswordTmpl = template.Must(template.New("reset_password_content").Parse(resetPasswordContent))
// GenerateResetPasswordHTML takes the link's lifetime rather than quoting one,
// because the two drifted: the mail promised four hours against a token that
// died in one, so a link that read as good for the afternoon was refused.
func GenerateResetPasswordHTML(firstName, url string, ttl time.Duration) (string, error) {
data := struct {
ResetURL string
Expiry string
}{ResetURL: url, Expiry: humanExpiry(ttl)}
var buf bytes.Buffer
if err := resetPasswordTmpl.Execute(&buf, data); err != nil {
errs.CaptureException(err)
return "", err
}
return renderEmail("Reset Your Password", buf.String())
}
// humanExpiry renders a whole number of hours or minutes, which is every
// lifetime this is ever handed.
func humanExpiry(d time.Duration) string {
plural := func(n int, unit string) string {
if n == 1 {
return fmt.Sprintf("1 %s", unit)
}
return fmt.Sprintf("%d %ss", n, unit)
}
if d >= time.Hour {
return plural(int(d/time.Hour), "hour")
}
if d >= time.Minute {
return plural(int(d/time.Minute), "minute")
}
return plural(int(d/time.Second), "second")
}