feat: docs api design
This commit is contained in:
23
internal/router/base.go
Normal file
23
internal/router/base.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
var userClaimsKey = struct{}{}
|
||||
|
||||
func JWTRequired(c *fiber.Ctx) error {
|
||||
jwt := c.Get("Authorization")
|
||||
if jwt == "" {
|
||||
return c.Status(401).JSON(fiber.Map{"msg": "Unauthorized"})
|
||||
}
|
||||
claims, err := casdoorsdk.ParseJwtToken(jwt)
|
||||
if err != nil {
|
||||
log.Ctx(c.UserContext()).Error().Err(err).Msg("Unauthorized user")
|
||||
return c.Status(401).JSON(fiber.Map{"msg": "Unauthorized"})
|
||||
}
|
||||
c.Locals(userClaimsKey, claims)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user