features/working-on-scopes #13

Merged
jtom38 merged 28 commits from features/working-on-scopes into main 2024-04-04 15:31:54 -07:00
Showing only changes of commit ec24600269 - Show all commits

View File

@ -2,20 +2,14 @@ package v1
import ( import (
"fmt" "fmt"
"go-cook/api/models" "go-cook/api/domain"
"net/http" "net/http"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
) )
type HelloWhoResponse struct {
Success bool `json:"success"`
Error string `error:"error"`
Message string `json:"message"`
}
func (h *Handler) DemoHello(c echo.Context) error { func (h *Handler) DemoHello(c echo.Context) error {
return c.JSON(http.StatusOK, HelloWhoResponse{ return c.JSON(http.StatusOK, domain.HelloWhoResponse{
Success: true, Success: true,
Message: "Hello world!", Message: "Hello world!",
}) })
@ -23,27 +17,23 @@ func (h *Handler) DemoHello(c echo.Context) error {
func (h *Handler) HelloWho(c echo.Context) error { func (h *Handler) HelloWho(c echo.Context) error {
name := c.Param("who") name := c.Param("who")
return c.JSON(http.StatusOK, HelloWhoResponse{ return c.JSON(http.StatusOK, domain.HelloWhoResponse{
Success: true, Success: true,
Message: fmt.Sprintf("Hello, %s", name), Message: fmt.Sprintf("Hello, %s", name),
}) })
} }
type HelloBodyRequest struct {
Name string `json:"name" validate:"required"`
}
func (h *Handler) HelloBody(c echo.Context) error { func (h *Handler) HelloBody(c echo.Context) error {
request := HelloBodyRequest{} request := domain.HelloBodyRequest{}
err := (&echo.DefaultBinder{}).BindBody(c, &request) err := (&echo.DefaultBinder{}).BindBody(c, &request)
if err != nil { if err != nil {
return c.JSON(http.StatusBadRequest, HelloWhoResponse{ return c.JSON(http.StatusBadRequest, domain.HelloWhoResponse{
Success: false, Success: false,
Error: err.Error(), Error: err.Error(),
}) })
} }
return c.JSON(http.StatusOK, HelloWhoResponse{ return c.JSON(http.StatusOK, domain.HelloWhoResponse{
Success: true, Success: true,
Message: fmt.Sprintf("Hello, %s", request.Name), Message: fmt.Sprintf("Hello, %s", request.Name),
}) })
@ -52,10 +42,12 @@ func (h *Handler) HelloBody(c echo.Context) error {
func (h *Handler) ProtectedRoute(c echo.Context) error { func (h *Handler) ProtectedRoute(c echo.Context) error {
token, err := h.getJwtToken(c) token, err := h.getJwtToken(c)
if err != nil { if err != nil {
return c.JSON(http.StatusForbidden, models.ErrorResponse{ h.ReturnUnauthorizedResponse(c, err.Error())
HttpCode: http.StatusForbidden, }
Message: err.Error(),
}) err = token.IsValid(domain.ScopeRecipeRead)
if err != nil {
h.ReturnUnauthorizedResponse(c, ErrJwtScopeMissing)
} }
return c.JSON(http.StatusOK, token) return c.JSON(http.StatusOK, token)