all repos

onasty @ 5003a09

a one-time notes service

onasty/mailer/template.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
feat: reset password (#110)..., 1 year ago
1
package main
2
3
import (
4
	"errors"
5
	"fmt"
6
)
7
8
type Template struct {
9
	Subject string
10
	Body    string
11
}
12
13
type TemplateFunc func(args map[string]string) Template
14
15
func getTemplate(appURL string, templateName string) (TemplateFunc, error) {
16
	switch templateName {
17
	case "email_verification":
18
		return emailVerificationTemplate(appURL), nil
19
	case "reset_password":
20
		return passwordResetTemplate(appURL), nil
21
	default:
22
		return nil, errors.New("failed to get template") //nolint:err113
23
	}
24
}
25
26
func emailVerificationTemplate(appURL string) TemplateFunc {
27
	return func(opts map[string]string) Template {
28
		return Template{
29
			Subject: "Onasty: verify your email",
30
			Body: fmt.Sprintf(`To verify your email, please follow this link:
31
<a href="%[1]s/api/v1/auth/verify/%[2]s">%[1]s/api/v1/auth/verify/%[2]s</a>
32
<br />
33
<br />
34
This link will expire after 24 hours.`, appURL, opts["token"]),
35
		}
36
	}
37
}
38
39
func passwordResetTemplate(appURL string) TemplateFunc {
40
	return func(opts map[string]string) Template {
41
		return Template{
42
			Subject: "Onasty: reset your password",
43
			// TODO: when ui is ready, change the link to the ui
44
			Body: fmt.Sprintf(`To reset your password, use this api:
45
<a href="%[1]s/api/v1/auth/reset-password/%[2]s">%[1]s/api/v1/auth/reset-password/%[2]s</a>
46
<br />
47
<br />
48
This link will expire after an hour.`, appURL, opts["token"]),
49
		}
50
	}
51
}