summaryrefslogtreecommitdiff
path: root/internal/router/handlers/api-v1-email-verify.go
diff options
from:
to:
context:
space:
mode:
authorGravatar Saya Andy <145215889+SayaAndy@users.noreply.github.com> 2025-10-28 23:15:23 +0700
committerGravatar GitHub <noreply@github.com> 2025-10-28 23:15:23 +0700
commitbda4faeeed4d9859cfe8afd7952a751683d74bd8 (patch)
tree464c18fed0c21b63b65425f48a3a790229461058 /internal/router/handlers/api-v1-email-verify.go
parentdd69b45d3a41be2b5c38892c3b107770a0f859f2 (diff)
parent1862f232eb70105340a91e74a5630c233a411bca (diff)
downloadweb-0.11.1.tar.gz
web-0.11.1.zip
v0.11.1 (#18)v0.11.1
- refactor: make structurized implementation of routes + separate identity of router - feat: update go to 1.25 - feat: update all dependencies - debug: display routes when debug logs turned on
Diffstat (limited to 'internal/router/handlers/api-v1-email-verify.go')
-rw-r--r--internal/router/handlers/api-v1-email-verify.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/internal/router/handlers/api-v1-email-verify.go b/internal/router/handlers/api-v1-email-verify.go
new file mode 100644
index 0000000..c9ec969
--- /dev/null
+++ b/internal/router/handlers/api-v1-email-verify.go
@@ -0,0 +1,62 @@
+package handlers
+
+import (
+ "html/template"
+ "log/slog"
+
+ "github.com/SayaAndy/saya-today-web/internal/router"
+ "github.com/gofiber/fiber/v2"
+)
+
+type VerifyCodeHandler struct {
+ router.BasicHandler
+}
+
+func init() {
+ router.Routes = append(router.Routes, &VerifyCodeHandler{})
+}
+
+func (r *VerifyCodeHandler) Filter() (method string, path string) {
+ return "POST", "/api/v1/email/verify"
+}
+
+func (r *VerifyCodeHandler) IsTemplated() bool {
+ return false
+}
+
+func (r *VerifyCodeHandler) TemplatesToInject() []string {
+ return []string{"views/partials/personal-page-status.html"}
+}
+
+func (r *VerifyCodeHandler) ToCache() router.CacheSetting {
+ return router.Disabled
+}
+
+func (r *VerifyCodeHandler) ToValidateLang() router.LangSetting {
+ return router.InReferer
+}
+
+func (r *VerifyCodeHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
+ verificationCode := c.FormValue("email_code")
+ templateMap["StatusId"] = "verification-message"
+
+ if verificationCode == "" {
+ templateMap["Status"] = "Failed"
+ templateMap["Message"] = supplements.Localization[lang].UserProfile.VerificationEmpty
+ return fiber.StatusUnprocessableEntity, nil
+ }
+
+ if err = supplements.Mailer.Verify(verificationCode, lang); err != nil {
+ slog.Warn("verification code is invalid", slog.String("verification_code", verificationCode), slog.String("error", err.Error()))
+ templateMap["Status"] = "Failed"
+ templateMap["Message"] = supplements.Localization[lang].UserProfile.VerificationFailed
+ return fiber.StatusUnprocessableEntity, nil
+ }
+
+ templateMap["Status"] = "OK"
+ templateMap["Message"] = supplements.Localization[lang].UserProfile.VerificationSuccess
+ templateMap["DataAttributes"] = map[string]any{
+ "hide-verification-panel": template.HTMLAttr("data-code-expiry-time=\"true\""),
+ }
+ return fiber.StatusOK, nil
+}