Moved the cookie consts to a domain package

This commit is contained in:
James Tombleson 2024-04-14 18:33:17 -07:00
parent bd82bfeca0
commit 03d151f75a
2 changed files with 14 additions and 10 deletions

9
domain/consts.go Normal file
View File

@ -0,0 +1,9 @@
package domain
const (
CookieToken = "token"
CookieRefreshToken = "refresh"
CookieUser = "user"
CookieSettingsDarkMode = "darkmode"
)

View File

@ -3,6 +3,7 @@ package handlers
import ( import (
"errors" "errors"
"templ-test/client" "templ-test/client"
"templ-test/domain"
"templ-test/models" "templ-test/models"
"templ-test/services" "templ-test/services"
"time" "time"
@ -13,12 +14,6 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
const (
CookieToken = "token"
CookieRefreshToken = "refresh"
CookieUser = "user"
)
type Handlers struct { type Handlers struct {
api client.ApiClient api client.ApiClient
cfg services.EnvConfig cfg services.EnvConfig
@ -76,7 +71,7 @@ func ValidateJwt(ctx echo.Context, sharedSecret, issuer string) (jwtToken, error
if !token.Valid { if !token.Valid {
return jwtToken{}, errors.New("invalid jwt token") return jwtToken{}, errors.New("invalid jwt token")
} }
claims := token.Claims.(*jwtToken) claims := token.Claims.(*jwtToken)
if !claims.Exp.After(time.Now()) { if !claims.Exp.After(time.Now()) {
return jwtToken{}, errors.New("the jwt token has expired") 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 { func GetCookieValues(ctx echo.Context) models.AllCookies {
m := models.AllCookies{} m := models.AllCookies{}
token, err := ctx.Cookie(CookieToken) token, err := ctx.Cookie(domain.CookieToken)
if err == nil { if err == nil {
m.Token = token.Value m.Token = token.Value
} }
user, err := ctx.Cookie(CookieUser) user, err := ctx.Cookie(domain.CookieUser)
if err == nil { if err == nil {
m.Username = user.Value m.Username = user.Value
} }
refresh, err := ctx.Cookie(CookieRefreshToken) refresh, err := ctx.Cookie(domain.CookieRefreshToken)
if err == nil { if err == nil {
m.RefreshToken = refresh.Value m.RefreshToken = refresh.Value
} }