From 45dd50ccba55502528ef1de339c4bd97d83e6839 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 15 Mar 2025 22:20:33 +0800 Subject: [PATCH] :sparkles: User able to re-send the confirm register email --- pkg/authkit/models/tokens.go | 9 +++++---- pkg/internal/services/tokens.go | 15 +++++++++++++-- pkg/internal/web/api/accounts_api.go | 18 ++++++++++++++++++ pkg/internal/web/api/index.go | 3 ++- pkg/internal/web/api/security_api.go | 2 +- 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/pkg/authkit/models/tokens.go b/pkg/authkit/models/tokens.go index fc97fb6..7132516 100644 --- a/pkg/authkit/models/tokens.go +++ b/pkg/authkit/models/tokens.go @@ -14,8 +14,9 @@ const ( type MagicToken struct { BaseModel - Code string `json:"code"` - Type int8 `json:"type"` - AccountID *uint `json:"account_id"` - ExpiredAt *time.Time `json:"expired_at"` + Code string `json:"code"` + Type int8 `json:"type"` + AccountID *uint `json:"account_id"` + ExpiredAt *time.Time `json:"expired_at"` + LastNotifiedAt *time.Time `json:"last_notified_at"` } diff --git a/pkg/internal/services/tokens.go b/pkg/internal/services/tokens.go index bb51caa..0aa7895 100644 --- a/pkg/internal/services/tokens.go +++ b/pkg/internal/services/tokens.go @@ -2,10 +2,11 @@ package services import ( "fmt" - "git.solsynth.dev/hypernet/nexus/pkg/nex/localize" "strings" "time" + "git.solsynth.dev/hypernet/nexus/pkg/nex/localize" + "git.solsynth.dev/hypernet/passport/pkg/authkit/models" "git.solsynth.dev/hypernet/passport/pkg/internal/gap" "git.solsynth.dev/hypernet/pusher/pkg/pushkit" @@ -46,10 +47,15 @@ func NewMagicToken(mode models.MagicTokenType, assignTo *models.Account, expired } } -func NotifyMagicToken(token models.MagicToken) error { +func NotifyMagicToken(token models.MagicToken, skipCheck ...bool) error { if token.AccountID == nil { return fmt.Errorf("could notify a non-assign magic token") } + if token.LastNotifiedAt != nil && (len(skipCheck) == 0 || !skipCheck[0]) { + if token.LastNotifiedAt.Unix() >= time.Now().Add(-60*time.Minute).Unix() { + return fmt.Errorf("magic token has been notified in an hour") + } + } var user models.Account if err := database.C.Where(&models.Account{ @@ -93,5 +99,10 @@ func NotifyMagicToken(token models.MagicToken) error { HTML: &content, }, }) + + if err == nil { + database.C.Model(&token).Update("last_notified_at", time.Now()) + } + return err } diff --git a/pkg/internal/web/api/accounts_api.go b/pkg/internal/web/api/accounts_api.go index acba1d7..42e18a7 100644 --- a/pkg/internal/web/api/accounts_api.go +++ b/pkg/internal/web/api/accounts_api.go @@ -265,6 +265,24 @@ func doRegisterConfirm(c *fiber.Ctx) error { return c.SendStatus(fiber.StatusOK) } +func reNotifyRegisterConfirm(c *fiber.Ctx) error { + if err := exts.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("user").(models.Account) + + var magicToken models.MagicToken + if err := database.C.Where("account_id = ? AND type = ?", user.ID, models.ConfirmMagicToken).First(&magicToken).Error; err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + + if err := services.NotifyMagicToken(magicToken); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + + return c.SendStatus(fiber.StatusOK) +} + func requestDeleteAccount(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err diff --git a/pkg/internal/web/api/index.go b/pkg/internal/web/api/index.go index 527f7ab..cda08e5 100644 --- a/pkg/internal/web/api/index.go +++ b/pkg/internal/web/api/index.go @@ -71,9 +71,10 @@ func MapControllers(app *fiber.App, baseURL string) { me.Put("/language", updateAccountLanguage) me.Get("/events", getEvents) me.Get("/tickets", getTickets) - me.Delete("/tickets/:ticketId", killTicket) + me.Delete("/tickets/:ticketId", deleteTicket) me.Post("/confirm", doRegisterConfirm) + me.Patch("/confirm", reNotifyRegisterConfirm) me.Get("/status", getMyselfStatus) me.Post("/status", setStatus) diff --git a/pkg/internal/web/api/security_api.go b/pkg/internal/web/api/security_api.go index 8531694..73045f6 100644 --- a/pkg/internal/web/api/security_api.go +++ b/pkg/internal/web/api/security_api.go @@ -39,7 +39,7 @@ func getTickets(c *fiber.Ctx) error { }) } -func killTicket(c *fiber.Ctx) error { +func deleteTicket(c *fiber.Ctx) error { if err := exts.EnsureAuthenticated(c); err != nil { return err }