all repos

onasty @ 327757c9371a333d3436f0a204677742a04236cf

a one-time notes service

onasty/mailer/template.go(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"errors"
	"fmt"
)

type Template struct {
	Subject string
	Body    string
}

type TemplateFunc func(args map[string]string) Template

func getTemplate(appURL string, templateName string) (TemplateFunc, error) {
	switch templateName {
	case "email_verification":
		return emailVerificationTemplate(appURL), nil
	case "reset_password":
		return passwordResetTemplate(appURL), nil
	default:
		return nil, errors.New("failed to get template") //nolint:err113
	}
}

func emailVerificationTemplate(appURL string) TemplateFunc {
	return func(opts map[string]string) Template {
		return Template{
			Subject: "Onasty: verify your email",
			Body: fmt.Sprintf(`To verify your email, please follow this link:
<a href="%[1]s/api/v1/auth/verify/%[2]s">%[1]s/api/v1/auth/verify/%[2]s</a>
<br />
<br />
This link will expire after 24 hours.`, appURL, opts["token"]),
		}
	}
}

func passwordResetTemplate(appURL string) TemplateFunc {
	return func(opts map[string]string) Template {
		return Template{
			Subject: "Onasty: reset your password",
			// TODO: when ui is ready, change the link to the ui
			Body: fmt.Sprintf(`To reset your password, use this api:
<a href="%[1]s/api/v1/auth/reset-password/%[2]s">%[1]s/api/v1/auth/reset-password/%[2]s</a>
<br />
<br />
This link will expire after an hour.`, appURL, opts["token"]),
		}
	}
}