123456789101112131415161718192021222324252627282930313233343536373839 |
- package middleware
- import (
- "github.com/gogf/gf/v2/net/ghttp"
- "gxt-api-frame/app/errors"
- "gxt-api-frame/library/auth"
- "gxt-api-frame/library/gplus"
- "gxt-api-frame/library/utils"
- )
- func UserAuthMiddleware(skippers ...SkipperFunc) ghttp.HandlerFunc {
- jwt := auth.New()
- return func(r *ghttp.Request) {
- if len(skippers) > 0 && skippers[0](r) {
- r.Middleware.Next()
- return
- }
- var userId string
- if t := gplus.GetToken(r); t != "" {
- id, err := jwt.ParseUserID(t)
- if err != nil {
- gplus.ResError(r, err)
- }
- userId = id
- }
- if userId != "" {
- gplus.SetUserId(r, userId)
- }
- if userId == "" {
- if utils.GetConfig("common.RunMode").String() == "debug" {
- gplus.SetUserId(r, utils.GetConfig("root.user_name").String())
- r.Middleware.Next()
- return
- }
- gplus.ResError(r, errors.ErrNoPerm)
- }
- r.Middleware.Next()
- }
- }
|