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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package handlers
import (
"fmt"
"html/template"
"log/slog"
"strings"
"github.com/SayaAndy/saya-today-web/internal/router"
"github.com/gofiber/fiber/v2"
)
type SendVerificationCodeHandler struct {
router.BasicHandler
}
func init() {
router.Routes = append(router.Routes, &SendVerificationCodeHandler{})
}
func (r *SendVerificationCodeHandler) Filter() (method string, path string) {
return "POST", "/api/v1/email/send-verification-code"
}
func (r *SendVerificationCodeHandler) IsTemplated() bool {
return false
}
func (r *SendVerificationCodeHandler) TemplatesToInject() []string {
return []string{"views/partials/personal-page-status.html"}
}
func (r *SendVerificationCodeHandler) ToCache() router.CacheSetting {
return router.Disabled
}
func (r *SendVerificationCodeHandler) ToValidateLang() router.LangSetting {
return router.InReferer
}
func (r *SendVerificationCodeHandler) RateLimiter() *fiber.Handler {
return &router.RateLimiterStrict
}
func (r *SendVerificationCodeHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
id := c.IP()
templateMap["StatusId"] = "email-message"
email := c.FormValue("email")
if email == "" {
templateMap["Status"] = "Failed"
templateMap["Message"] = supplements.Localization[lang].UserProfile.EmailEmpty
return fiber.StatusUnprocessableEntity, nil
}
isTaken, err := supplements.Mailer.MailIsTaken(email)
if err != nil {
templateMap["Status"] = "Failed"
templateMap["Message"] = supplements.Localization[lang].UserProfile.VerificationCodeSendingError
slog.Error("failed to check if address is already taken", slog.String("error", err.Error()))
return fiber.StatusUnprocessableEntity, nil
}
if isTaken {
templateMap["Status"] = "Failed"
templateMap["Message"] = supplements.Localization[lang].UserProfile.EmailTaken
return fiber.StatusUnprocessableEntity, nil
}
if isAllowed, whenAllowed, _ := supplements.Mailer.IsAllowedToRetryVerification(id); !isAllowed {
templateMap["Status"] = "Failed"
templateMap["Message"] = strings.ReplaceAll(supplements.Localization[lang].UserProfile.DelayTilVerification, "{}", whenAllowed.Format("2006-01-02 15:04:05 MST"))
templateMap["DataAttributes"] = map[string]any{
"striked-end-time": template.HTMLAttr(fmt.Sprintf("data-striked-end-time=\"%d\"", whenAllowed.UnixMilli())),
}
return fiber.StatusUnprocessableEntity, nil
}
if previousEmail, _, _ := supplements.Mailer.GetInfo(supplements.Mailer.GetHash(id)); previousEmail == email {
templateMap["Status"] = "Failed"
templateMap["Message"] = supplements.Localization[lang].UserProfile.EmailAlreadyValidated
return fiber.StatusUnprocessableEntity, nil
}
if err = supplements.Mailer.SendVerificationCode(id, email, lang); err != nil {
templateMap["Status"] = "Failed"
templateMap["Message"] = supplements.Localization[lang].UserProfile.VerificationCodeSendingError
slog.Error("failed to send a verification code", slog.String("error", err.Error()))
return fiber.StatusUnprocessableEntity, nil
}
_, endTime, codeExpiry := supplements.Mailer.IsAllowedToRetryVerification(id)
templateMap["Status"] = "OK"
templateMap["Message"] = supplements.Localization[lang].UserProfile.VerificationCodeSent
templateMap["DataAttributes"] = map[string]any{
"striked-end-time": template.HTMLAttr(fmt.Sprintf("data-striked-end-time=\"%d\"", endTime.UnixMilli())),
"code-expiry-time": template.HTMLAttr(fmt.Sprintf("data-code-expiry-time=\"%d\"", codeExpiry.UnixMilli())),
}
return fiber.StatusOK, nil
}
|