Initial commit including .env
This commit is contained in:
69
middlewares/auth.go
Normal file
69
middlewares/auth.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"doormile/config"
|
||||
"doormile/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func AuthMiddleware(cfg *config.Config) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
authHeader := c.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "authorization header is required",
|
||||
})
|
||||
}
|
||||
|
||||
parts := strings.Split(authHeader, " ")
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "authorization header must be in format: Bearer <token>",
|
||||
})
|
||||
}
|
||||
|
||||
tokenString := parts[1]
|
||||
claims, err := utils.ParseToken(tokenString, cfg.JWTSecret)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "invalid or expired token",
|
||||
})
|
||||
}
|
||||
|
||||
c.Locals("userid", claims.UserID)
|
||||
c.Locals("email", claims.Email)
|
||||
c.Locals("roleid", claims.RoleID)
|
||||
c.Locals("tenantid", claims.TenantID)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func RoleCheckMiddleware(allowedRoles ...int) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
roleID, ok := c.Locals("roleid").(int)
|
||||
if !ok {
|
||||
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "access denied",
|
||||
})
|
||||
}
|
||||
|
||||
for _, role := range allowedRoles {
|
||||
if roleID == role {
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "insufficient permissions for this resource",
|
||||
})
|
||||
}
|
||||
}
|
||||
41
middlewares/logger.go
Normal file
41
middlewares/logger.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"doormile/utils"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func ZapLogger() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
start := time.Now()
|
||||
|
||||
err := c.Next()
|
||||
|
||||
latency := time.Since(start)
|
||||
status := c.Response().StatusCode()
|
||||
method := c.Method()
|
||||
path := c.Path()
|
||||
|
||||
if err != nil {
|
||||
utils.Error("API request error",
|
||||
"method", method,
|
||||
"path", path,
|
||||
"status", status,
|
||||
"latency", latency,
|
||||
"error", err.Error(),
|
||||
)
|
||||
} else {
|
||||
utils.Info("API request success",
|
||||
"method", method,
|
||||
"path", path,
|
||||
"status", status,
|
||||
"latency", latency,
|
||||
)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user