From 4805f574b560e35bcacb4adec10e3e2bee359fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Eduardo=20Jer=C3=A9z=20Gir=C3=B3n?= Date: Thu, 29 Aug 2024 12:55:32 -0600 Subject: [PATCH] Refactor request context handling: consolidate context keys into a single key for simplicity and improve data retrieval --- internal/view/reqctx/ctx.go | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/internal/view/reqctx/ctx.go b/internal/view/reqctx/ctx.go index 175e9f2..ec50275 100644 --- a/internal/view/reqctx/ctx.go +++ b/internal/view/reqctx/ctx.go @@ -6,11 +6,8 @@ import ( "github.com/labstack/echo/v4" ) -// Context keys to avoid typos const ( - isAuthedKey = "isAuthed" - sessionIDKey = "sessionId" - userKey = "user" + ctxKey = "PGBackWebCTX" ) // Ctx represents the values passed through a single request context. @@ -22,30 +19,14 @@ type Ctx struct { // SetCtx inserts values into the Echo request context. func SetCtx(c echo.Context, ctx Ctx) { - c.Set(isAuthedKey, ctx.IsAuthed) - c.Set(sessionIDKey, ctx.SessionID) - c.Set(userKey, ctx.User) + c.Set(ctxKey, ctx) } // GetCtx retrieves values from the Echo request context. func GetCtx(c echo.Context) Ctx { - var isAuthed bool - var sessionID uuid.UUID - var user dbgen.User - - if ia, ok := c.Get(isAuthedKey).(bool); ok { - isAuthed = ia - } - if sid, ok := c.Get(sessionIDKey).(uuid.UUID); ok { - sessionID = sid - } - if au, ok := c.Get(userKey).(dbgen.User); ok { - user = au - } - - return Ctx{ - IsAuthed: isAuthed, - SessionID: sessionID, - User: user, + ctx, ok := c.Get(ctxKey).(Ctx) + if !ok { + return Ctx{} } + return ctx }