From d5f510ae326f3108c80d0dfe2d61178f17269a77 Mon Sep 17 00:00:00 2001 From: ditatompel Date: Sat, 18 May 2024 20:27:30 +0700 Subject: [PATCH] Remove admin login logout handler and endpoints #2 --- cmd/server/serve.go | 5 ---- handler/middlewares.go | 13 ----------- handler/response.go | 52 ------------------------------------------ handler/routes.go | 5 ---- 4 files changed, 75 deletions(-) diff --git a/cmd/server/serve.go b/cmd/server/serve.go index 1939795..ec1ecc5 100644 --- a/cmd/server/serve.go +++ b/cmd/server/serve.go @@ -14,7 +14,6 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" - "github.com/gofiber/fiber/v2/middleware/encryptcookie" "github.com/gofiber/fiber/v2/middleware/filesystem" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/recover" @@ -62,10 +61,6 @@ func serve() { AllowCredentials: true, })) - // cookie - app.Use(encryptcookie.New(encryptcookie.Config{Key: appCfg.SecretKey})) - - handler.AppRoute(app) handler.V1Api(app) app.Use("/", filesystem.New(filesystem.Config{ Root: frontend.SvelteKitHandler(), diff --git a/handler/middlewares.go b/handler/middlewares.go index 9d6338d..bf324b5 100644 --- a/handler/middlewares.go +++ b/handler/middlewares.go @@ -7,19 +7,6 @@ import ( "github.com/gofiber/fiber/v2" ) -func CookieProtected(c *fiber.Ctx) error { - cookie := c.Cookies("xmr-nodes-ui") - if cookie == "" { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ - "status": "error", - "message": "Unauthorized", - "data": nil, - }) - } - - return c.Next() -} - func CheckProber(c *fiber.Ctx) error { key := c.Get("X-Prober-Api-Key") if key == "" { diff --git a/handler/response.go b/handler/response.go index aae10e0..f53ed32 100644 --- a/handler/response.go +++ b/handler/response.go @@ -1,65 +1,13 @@ package handler import ( - "fmt" "strconv" - "time" "xmr-remote-nodes/internal/database" "xmr-remote-nodes/internal/repo" "github.com/gofiber/fiber/v2" ) -func Login(c *fiber.Ctx) error { - payload := repo.Admin{} - if err := c.BodyParser(&payload); err != nil { - return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{ - "status": "error", - "message": err.Error(), - "data": nil, - }) - } - - repo := repo.NewAdminRepo(database.GetDB()) - res, err := repo.Login(payload.Username, payload.Password) - if err != nil { - return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ - "status": "error", - "message": err.Error(), - "data": nil, - }) - } - - token := fmt.Sprintf("auth_%d_%d", res.Id, time.Now().Unix()) - c.Cookie(&fiber.Cookie{ - Name: "xmr-nodes-ui", - Value: token, - Expires: time.Now().Add(time.Hour * 24), - HTTPOnly: true, - }) - - return c.JSON(fiber.Map{ - "status": "ok", - "message": "Logged in", - "data": nil, - }) -} - -func Logout(c *fiber.Ctx) error { - c.Cookie(&fiber.Cookie{ - Name: "xmr-nodes-ui", - Value: "", - Expires: time.Now(), - HTTPOnly: true, - }) - - return c.JSON(fiber.Map{ - "status": "ok", - "message": "Logged out", - "data": nil, - }) -} - func MoneroNode(c *fiber.Ctx) error { nodeId, err := c.ParamsInt("id", 0) if err != nil { diff --git a/handler/routes.go b/handler/routes.go index cf0591f..71e404d 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -4,11 +4,6 @@ import ( "github.com/gofiber/fiber/v2" ) -func AppRoute(app *fiber.App) { - app.Post("/auth/login", Login) - app.Post("/auth/logout", Logout) -} - func V1Api(app *fiber.App) { v1 := app.Group("/api/v1")