From 03d151f75a06badfd9f710aa14f10a70914a2aa0 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 14 Apr 2024 18:33:17 -0700 Subject: [PATCH] Moved the cookie consts to a domain package --- domain/consts.go | 9 +++++++++ handlers/handler.go | 15 +++++---------- 2 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 domain/consts.go diff --git a/domain/consts.go b/domain/consts.go new file mode 100644 index 0000000..9e67900 --- /dev/null +++ b/domain/consts.go @@ -0,0 +1,9 @@ +package domain + +const ( + CookieToken = "token" + CookieRefreshToken = "refresh" + CookieUser = "user" + + CookieSettingsDarkMode = "darkmode" +) diff --git a/handlers/handler.go b/handlers/handler.go index 4cbfa70..d3c9f0f 100644 --- a/handlers/handler.go +++ b/handlers/handler.go @@ -3,6 +3,7 @@ package handlers import ( "errors" "templ-test/client" + "templ-test/domain" "templ-test/models" "templ-test/services" "time" @@ -13,12 +14,6 @@ import ( "github.com/labstack/echo/v4" ) -const ( - CookieToken = "token" - CookieRefreshToken = "refresh" - CookieUser = "user" -) - type Handlers struct { api client.ApiClient cfg services.EnvConfig @@ -76,7 +71,7 @@ func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error if !token.Valid { return jwtToken{}, errors.New("invalid jwt token") } - + claims := token.Claims.(*jwtToken) if !claims.Exp.After(time.Now()) { return jwtToken{}, errors.New("the jwt token has expired") @@ -91,17 +86,17 @@ func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error func GetCookieValues(ctx echo.Context) models.AllCookies { m := models.AllCookies{} - token, err := ctx.Cookie(CookieToken) + token, err := ctx.Cookie(domain.CookieToken) if err == nil { m.Token = token.Value } - user, err := ctx.Cookie(CookieUser) + user, err := ctx.Cookie(domain.CookieUser) if err == nil { m.Username = user.Value } - refresh, err := ctx.Cookie(CookieRefreshToken) + refresh, err := ctx.Cookie(domain.CookieRefreshToken) if err == nil { m.RefreshToken = refresh.Value }