From 59b5f8f7e90ae73ccd2fbc781ac087e0f6189ed8 Mon Sep 17 00:00:00 2001 From: chan Date: Tue, 13 Jan 2026 10:31:18 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B3=80=EC=88=98=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/internal/handler/auth_handler.go | 33 +++++++++++++++++------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/backend/internal/handler/auth_handler.go b/backend/internal/handler/auth_handler.go index 85a06933..0688d1ab 100644 --- a/backend/internal/handler/auth_handler.go +++ b/backend/internal/handler/auth_handler.go @@ -19,6 +19,19 @@ import ( "github.com/gofiber/fiber/v2" ) +const ( + // Redis Key Prefixes + prefixSession = "enchanted_session:" + prefixToken = "enchanted_token:" + + // Session Statuses + statusPending = "pending" + statusSuccess = "success" + + // Durations + defaultExpiration = 5 * time.Minute +) + type AuthHandler struct { ProjectID string SmsService domain.SmsService @@ -124,8 +137,8 @@ func (h *AuthHandler) InitEnchantedLink(c *fiber.Ctx) error { pendingRef := generateSecureToken(4) // Store in Redis - h.RedisService.Set("enchanted_session:"+pendingRef, `{"status":"pending"}`, 5*time.Minute) - h.RedisService.Set("enchanted_token:"+token, fmt.Sprintf(`{"pendingRef":"%s","loginId":"%s"}`, pendingRef, loginID), 5*time.Minute) + h.RedisService.Set(prefixSession+pendingRef, fmt.Sprintf(`{"status":"%s"}`, statusPending), defaultExpiration) + h.RedisService.Set(prefixToken+token, fmt.Sprintf(`{"pendingRef":"%s","loginId":"%s"}`, pendingRef, loginID), defaultExpiration) // Send SMS // Frontend URL should be dynamic or env based, but restoring hardcoded/env logic @@ -155,22 +168,22 @@ func (h *AuthHandler) PollEnchantedLink(c *fiber.Ctx) error { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request body"}) } - val, err := h.RedisService.Get("enchanted_session:" + req.PendingRef) + val, err := h.RedisService.Get(prefixSession + req.PendingRef) if err != nil || val == "" { - return c.JSON(fiber.Map{"status": "pending"}) + return c.JSON(fiber.Map{"status": statusPending}) } var data map[string]string json.Unmarshal([]byte(val), &data) - if data["status"] == "success" { + if data["status"] == statusSuccess { return c.JSON(fiber.Map{ "sessionJwt": data["jwt"], "status": "ok", }) } - return c.JSON(fiber.Map{"status": "pending"}) + return c.JSON(fiber.Map{"status": statusPending}) } // VerifyMagicLink - Validate token and login (Restored) @@ -180,14 +193,14 @@ func (h *AuthHandler) VerifyMagicLink(c *fiber.Ctx) error { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid request body"}) } - tokenKey := "enchanted_token:" + req.Token + tokenKey := prefixToken + req.Token val, err := h.RedisService.Get(tokenKey) if err != nil || val == "" { return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "Invalid or expired token"}) } var tokenData map[string]string - json.Unmarshal([]byte(val), &data := tokenData) + json.Unmarshal([]byte(val), &tokenData) pendingRef := tokenData["pendingRef"] loginID := tokenData["loginId"] @@ -247,10 +260,10 @@ func (h *AuthHandler) VerifyMagicLink(c *fiber.Ctx) error { // Update Session in Redis for the polling client sessionData, _ := json.Marshal(map[string]string{ - "status": "success", + "status": statusSuccess, "jwt": sessionToken, }) - h.RedisService.Set("enchanted_session:"+pendingRef, string(sessionData), 5*time.Minute) + h.RedisService.Set(prefixSession+pendingRef, string(sessionData), defaultExpiration) return c.JSON(fiber.Map{ "token": sessionToken,