-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
45 lines (36 loc) · 1.03 KB
/
handler.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
37
38
39
40
41
42
43
44
45
package auth
import "github.com/gin-gonic/gin"
type GinAuthHandler func(c *gin.Context, p AuthProvider, r AuthRender)
type GinAuthHandlers struct {
GetLoginHandler GinAuthHandler
PostLoginHandler GinAuthHandler
LogoutHandler func(c *gin.Context, a *GinAuth)
LoginCallbackHandler GinAuthHandler
}
func NewGinHandler(h GinAuthHandler, a *GinAuth) func(c *gin.Context) {
return func(c *gin.Context) {
var (
pName = c.Param("name")
authP = a.GetProvider(pName)
authR = a.GetRender(pName)
)
c.Set(CtxKeyGinAuth, a)
c.Set(CtxKeyResultType, a.Config.Response.DefaultResultType)
if authP == nil || authR == nil {
if a.Config.Response.NotFoundHandler != nil {
a.Config.Response.NotFoundHandler(c)
} else {
DefaultNotFoundHandler(c)
}
return
}
h(c, authP, authR)
}
}
func NewHandler(h func(c *gin.Context, a *GinAuth), a *GinAuth) func(c *gin.Context) {
return func(c *gin.Context) {
c.Set(CtxKeyGinAuth, a)
c.Set(CtxKeyResultType, a.Config.Response.DefaultResultType)
h(c, a)
}
}