Files
qvyun/backend/app/middlewares/m_jwt_parse.go
2025-01-09 19:11:01 +08:00

38 lines
693 B
Go

package middlewares
import (
"time"
"backend/app/errorx"
"github.com/gofiber/fiber/v3"
log "github.com/sirupsen/logrus"
)
func (f *Middlewares) ParseJWT(c fiber.Ctx) error {
tokens := c.GetReqHeaders()["Authorization"]
if len(tokens) == 0 {
queryToken := c.Query("token")
tokens = []string{queryToken}
if len(tokens) == 0 {
return c.Next()
}
}
token := tokens[0]
claim, err := f.jwt.Parse(token)
if err != nil {
c.Cookie(&fiber.Cookie{
Name: "token",
Value: "",
Expires: time.Now().Add(-1 * time.Hour),
HTTPOnly: true,
})
log.Errorf("failed to parse jwt from token: %s", token)
return errorx.Unauthorized
}
_ = claim
return c.Next()
}