Update API

This commit is contained in:
2025-06-22 23:23:26 +07:00
parent b077ef9f06
commit 40705fbf3b
63 changed files with 1976 additions and 81 deletions
+30
View File
@@ -0,0 +1,30 @@
package middlewares
import (
"BE-MiniERP/config"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v4"
)
func JWTProtected() fiber.Handler {
return func(c *fiber.Ctx) error {
authHeader := c.Get("Authorization")
if authHeader == "" {
return c.SendStatus(fiber.StatusUnauthorized)
}
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte(config.GetConfig().JWTSecret), nil
})
if err != nil || !token.Valid {
return c.SendStatus(fiber.StatusUnauthorized)
}
c.Locals("user", token.Claims)
return c.Next()
}
}
+18
View File
@@ -0,0 +1,18 @@
package middlewares
import (
"fmt"
"time"
"github.com/gofiber/fiber/v2"
)
func Logger() fiber.Handler {
return func(c *fiber.Ctx) error {
start := time.Now()
err := c.Next()
duration := time.Since(start)
fmt.Printf("[%s] %s %s (%v)\n", c.Method(), c.Path(), c.IP(), duration)
return err
}
}