feat: init repo
This commit is contained in:
37
backend/app/middlewares/m_jwt_parse.go
Normal file
37
backend/app/middlewares/m_jwt_parse.go
Normal file
@@ -0,0 +1,37 @@
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user