added auth validate route

This commit is contained in:
2025-01-28 18:09:26 +01:00
parent f7efee56f0
commit 86c43fffc0
4 changed files with 31 additions and 5 deletions

View File

@@ -3,7 +3,9 @@ package middleware
import (
jwtware "github.com/gofiber/contrib/jwt"
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
"os"
"relay-server/model"
)
func Protected() fiber.Handler {
@@ -11,6 +13,15 @@ func Protected() fiber.Handler {
SigningKey: jwtware.SigningKey{Key: []byte(os.Getenv("JWT_SECRET"))},
ErrorHandler: jwtError,
TokenLookup: "cookie:token",
Claims: &model.UserClaims{},
SuccessHandler: func(c *fiber.Ctx) error {
user := c.Locals("user").(*jwt.Token)
claims := user.Claims.(*model.UserClaims)
c.Locals("userId", claims.UserId)
c.Locals("username", claims.Username)
return c.Next()
},
})
}