✨ User able to re-send the confirm register email
This commit is contained in:
		| @@ -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"` | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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 | ||||
| 	} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user