From b55ad0a99bb032043833183ecd6c0b173d6c0f7b Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 12 May 2024 10:45:16 -0700 Subject: [PATCH] Trying to debug cookies --- internal/handlers/debug.go | 23 +++++++++++++++++++++++ internal/handlers/handler.go | 6 ++++++ internal/handlers/util.go | 14 ++++++++++++++ internal/models/debug.go | 7 +++++++ internal/views/debug/cookies.templ | 12 ++++++++++++ 5 files changed, 62 insertions(+) create mode 100644 internal/handlers/debug.go create mode 100644 internal/handlers/util.go create mode 100644 internal/models/debug.go create mode 100644 internal/views/debug/cookies.templ diff --git a/internal/handlers/debug.go b/internal/handlers/debug.go new file mode 100644 index 0000000..81fc446 --- /dev/null +++ b/internal/handlers/debug.go @@ -0,0 +1,23 @@ +package handlers + +import ( + "net/http" + + "git.jamestombleson.com/jtom38/newsbot-portal/internal/domain" + "git.jamestombleson.com/jtom38/newsbot-portal/internal/models" + "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/debug" + "github.com/labstack/echo/v4" +) + +func (h *Handler) DebugCookies(c echo.Context) error { + user, _ := c.Cookie(domain.CookieUser) + token, _ := c.Cookie(domain.CookieToken) + refresh, _ := c.Cookie(domain.CookieRefreshToken) + + model := models.DebugCookiesViewModel{ + Username: user.Value, + Token: token.Value, + RefreshToken: refresh.Value, + } + return Render(c, http.StatusOK, debug.Cookies(model)) +} diff --git a/internal/handlers/handler.go b/internal/handlers/handler.go index 9f87d41..d7a482c 100644 --- a/internal/handlers/handler.go +++ b/internal/handlers/handler.go @@ -60,6 +60,12 @@ func NewServer(ctx context.Context, configs config.Configs, apiClient apiclient. router.GET("/", s.HomeIndex) router.GET("/about", s.HomeAbout) + debug := router.Group("/debug") + debug.GET("/cookies", s.DebugCookies) + + articles := router.Group("/articles") + articles.GET("", s.ArticlesList) + users := router.Group("/users") users.GET("/login", s.UserLogin) users.POST("/login", s.UserAfterLogin) diff --git a/internal/handlers/util.go b/internal/handlers/util.go new file mode 100644 index 0000000..7eb19a8 --- /dev/null +++ b/internal/handlers/util.go @@ -0,0 +1,14 @@ +package handlers + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +func SetCookie(c echo.Context, key string, value string) { + cookie := new(http.Cookie) + cookie.Name = key + cookie.Value = value + c.SetCookie(cookie) +} diff --git a/internal/models/debug.go b/internal/models/debug.go new file mode 100644 index 0000000..0621d83 --- /dev/null +++ b/internal/models/debug.go @@ -0,0 +1,7 @@ +package models + +type DebugCookiesViewModel struct { + Username string + Token string + RefreshToken string +} diff --git a/internal/views/debug/cookies.templ b/internal/views/debug/cookies.templ new file mode 100644 index 0000000..922627b --- /dev/null +++ b/internal/views/debug/cookies.templ @@ -0,0 +1,12 @@ +package debug + +import "git.jamestombleson.com/jtom38/newsbot-portal/internal/views/layout" +import "git.jamestombleson.com/jtom38/newsbot-portal/internal/models" + +templ Cookies(vm models.DebugCookiesViewModel) { + @layout.WithTemplate() { + Token: { vm.Token } + RefreshToken: { vm.RefreshToken } + UserName: { vm.Username } + } +}