added InitMiddlewares and delete jwtFromBody
Build and Test / build-and-test-backend (push) Successful in 28s
Build and Test / build-and-test-frontend (push) Successful in 33s

This commit is contained in:
wt
2025-08-10 14:37:07 +07:00
parent 90e5863adc
commit 303b9344f8
3 changed files with 30 additions and 71 deletions
@@ -1,36 +0,0 @@
package internaljwt
// import (
// "backend/model"
// "github.com/gofiber/fiber/v2"
// )
// func JwtFromBody(c *fiber.Ctx) error {
// req := new(model.TokenRequest)
// if err := c.BodyParser(req); err != nil {
// return c.Status(
// fiber.StatusBadRequest,
// ).JSON(
// fiber.Map{
// "error": "Cannot parse request body",
// },
// )
// }
// if req.Token == "" {
// return c.Status(
// fiber.StatusBadRequest,
// ).JSON(
// fiber.Map{
// "error": "Token not found in request body",
// },
// )
// }
// c.Request().Header.Set(
// "Authorization", "Bearer "+req.Token,
// )
// return c.Next()
// }
+28
View File
@@ -0,0 +1,28 @@
package server
import (
"os"
jwtware "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
)
func InitMiddlewares(app *fiber.App) {
app.Use(
cors.New(
cors.Config{
AllowOrigins: "https://localhost:3000,https://localhost:43243",
AllowHeaders: "*",
AllowCredentials: true,
},
),
jwtware.New(
jwtware.Config{
SigningKey: jwtware.SigningKey{
Key: []byte(os.Getenv("JWT_SECRET_KEY")),
},
},
),
)
}
+2 -35
View File
@@ -4,54 +4,21 @@ import (
"backend/api"
"backend/database"
"backend/utils"
"os"
jwtware "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/gofiber/fiber/v2/middleware/cors"
)
func Configure(app *fiber.App) {
utils.LoadEnv("wallet.conf")
database.InitDatabase()
log.SetLevel(log.LevelDebug)
app.Use(cors.New(cors.Config{
AllowOrigins: "http://localhost:3000",
AllowHeaders: "*",
}))
app.Static("/", "../frontend/build")
apiGroup := app.Group("/api")
apiGroup.Post("/token", api.Login)
app.Use(jwtware.New(jwtware.Config{
SigningKey: jwtware.SigningKey{
Key: []byte(os.Getenv("JWT_SECRET_KEY")),
},
// ErrorHandler: func(c *fiber.Ctx, err error) error {
// if err.Error() == "Missing or malformed JWT" {
// return c.Status(
// fiber.StatusBadRequest,
// ).JSON(
// fiber.Map{
// "status": "error",
// "message": "Missing or malformed JWT",
// "data": nil,
// },
// )
// }
// return c.Status(
// fiber.StatusUnauthorized,
// ).JSON(
// fiber.Map{
// "status": "error",
// "message": "Invalid or expired JWT",
// "data": nil,
// "error": err.Error(),
// },
// )
// },
}))
InitMiddlewares(app)
usersGroup := apiGroup.Group("/users")
usersGroup.Get("/", api.GetAllUsers)