added InitMiddlewares and delete jwtFromBody
This commit is contained in:
1 parent
90e5863adc
commit
303b9344f8
3 files changed
+30
-71
No files matched your search
@@ -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()
|
|
||||||
// }
|
|
||||||
@@ -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")),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -4,54 +4,21 @@ import (
|
|||||||
"backend/api"
|
"backend/api"
|
||||||
"backend/database"
|
"backend/database"
|
||||||
"backend/utils"
|
"backend/utils"
|
||||||
"os"
|
|
||||||
|
|
||||||
jwtware "github.com/gofiber/contrib/jwt"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/log"
|
"github.com/gofiber/fiber/v2/log"
|
||||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func Configure(app *fiber.App) {
|
func Configure(app *fiber.App) {
|
||||||
utils.LoadEnv("wallet.conf")
|
utils.LoadEnv("wallet.conf")
|
||||||
database.InitDatabase()
|
database.InitDatabase()
|
||||||
log.SetLevel(log.LevelDebug)
|
log.SetLevel(log.LevelDebug)
|
||||||
app.Use(cors.New(cors.Config{
|
|
||||||
AllowOrigins: "http://localhost:3000",
|
|
||||||
AllowHeaders: "*",
|
|
||||||
}))
|
|
||||||
app.Static("/", "../frontend/build")
|
app.Static("/", "../frontend/build")
|
||||||
apiGroup := app.Group("/api")
|
apiGroup := app.Group("/api")
|
||||||
|
|
||||||
apiGroup.Post("/token", api.Login)
|
apiGroup.Post("/token", api.Login)
|
||||||
app.Use(jwtware.New(jwtware.Config{
|
InitMiddlewares(app)
|
||||||
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(),
|
|
||||||
// },
|
|
||||||
// )
|
|
||||||
// },
|
|
||||||
}))
|
|
||||||
|
|
||||||
usersGroup := apiGroup.Group("/users")
|
usersGroup := apiGroup.Group("/users")
|
||||||
usersGroup.Get("/", api.GetAllUsers)
|
usersGroup.Get("/", api.GetAllUsers)
|
||||||
|
|||||||
Reference in new issue
Block a user