/
auth.go
36 lines (31 loc) · 884 Bytes
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package middleware
import (
"github.com/LyricTian/gin-admin/v10/pkg/logging"
"github.com/LyricTian/gin-admin/v10/pkg/util"
"github.com/gin-gonic/gin"
)
type AuthConfig struct {
AllowedPathPrefixes []string
SkippedPathPrefixes []string
Skipper func(c *gin.Context) bool
ParseUserID func(c *gin.Context) (string, error)
}
func AuthWithConfig(config AuthConfig) gin.HandlerFunc {
return func(c *gin.Context) {
if !AllowedPathPrefixes(c, config.AllowedPathPrefixes...) ||
SkippedPathPrefixes(c, config.SkippedPathPrefixes...) ||
(config.Skipper != nil && config.Skipper(c)) {
c.Next()
return
}
userID, err := config.ParseUserID(c)
if err != nil {
util.ResError(c, err)
return
}
ctx := util.NewUserID(c.Request.Context(), userID)
ctx = logging.NewUserID(ctx, userID)
c.Request = c.Request.WithContext(ctx)
c.Next()
}
}