From 0d90db89e554d0db3c17f985938f5e9fd1cd0395 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Sun, 31 Mar 2024 17:48:23 -0700 Subject: [PATCH] added config to the Handler and trying out a standard payload return for unauthorized --- api/handlers/v1/handler.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/api/handlers/v1/handler.go b/api/handlers/v1/handler.go index 9de5b13..6ca447c 100644 --- a/api/handlers/v1/handler.go +++ b/api/handlers/v1/handler.go @@ -2,8 +2,10 @@ package v1 import ( "database/sql" + "go-cook/api/domain" "go-cook/api/repositories" "go-cook/api/services" + "net/http" "github.com/golang-jwt/jwt/v5" echojwt "github.com/labstack/echo-jwt/v4" @@ -11,16 +13,19 @@ import ( ) type Handler struct { + Config domain.EnvConfig + UserService services.UserService userRepo repositories.IUserTable recipeRepo repositories.IRecipeTable } -func NewHandler(conn *sql.DB) *Handler { +func NewHandler(conn *sql.DB, cfg domain.EnvConfig) *Handler { return &Handler{ + Config: cfg, UserService: services.NewUserService(conn), - userRepo: repositories.NewUserRepository(conn), - recipeRepo: repositories.NewRecipeRepository(conn), + userRepo: repositories.NewUserRepository(conn), + recipeRepo: repositories.NewRecipeRepository(conn), } } @@ -29,7 +34,7 @@ func (h *Handler) Register(v1 *echo.Group) { NewClaimsFunc: func(c echo.Context) jwt.Claims { return new(JwtToken) }, - SigningKey: []byte("ThisIsABadSecretDontReallyUseThis"), + SigningKey: []byte(h.Config.JwtSecret), } v1.POST("/login", h.AuthLogin) @@ -53,3 +58,10 @@ func (h *Handler) Register(v1 *echo.Group) { //users.POST("/login", h.LoginUser) //users.POST("/update/password", h.UpdatePassword) } + +func (h *Handler) ReturnUnauthorizedResponse(c echo.Context, message string) error { + return c.JSON(http.StatusUnauthorized, domain.ErrorResponse{ + HttpCode: http.StatusUnauthorized, + Message: message, + }) +}