init push
This commit is contained in:
25
internal/middleware/auth.go
Normal file
25
internal/middleware/auth.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/gorm"
|
||||
samlauth "wucher/internal/auth"
|
||||
)
|
||||
|
||||
func RequireAuth(c *fiber.Ctx) error {
|
||||
token := c.Cookies("session_token")
|
||||
if token == "" {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "unauthorized"})
|
||||
}
|
||||
session, err := samlauth.GetSession(token)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "unauthorized"})
|
||||
}
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{"error": "unauthorized"})
|
||||
}
|
||||
c.Locals("user", session)
|
||||
return c.Next()
|
||||
}
|
||||
Reference in New Issue
Block a user