feat: add middlewares
This commit is contained in:
34
backend/modules/middlewares/m_jwt_parse.go
Normal file
34
backend/modules/middlewares/m_jwt_parse.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"backend/common/consts"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (f *Middlewares) JwtParse(c fiber.Ctx) error {
|
||||
tokens := c.GetReqHeaders()["Authorization"]
|
||||
if len(tokens) == 0 {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
token := tokens[0]
|
||||
claim, err := f.jwt.Parse(token)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to parse token")
|
||||
}
|
||||
|
||||
// query user
|
||||
user, err := f.userSvc.GetByOpenID(c.Context(), claim.ID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get user")
|
||||
}
|
||||
|
||||
c.SetUserContext(context.WithValue(c.UserContext(), consts.CtxKeyJwt, token))
|
||||
c.SetUserContext(context.WithValue(c.UserContext(), consts.CtxKeySession, user))
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
Reference in New Issue
Block a user