forked from gorilla/sessions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin_contri.go
33 lines (27 loc) · 879 Bytes
/
gin_contri.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
package sessions
import (
"fmt"
"net/url"
"github.com/gin-gonic/gin"
"github.com/niwho/chains"
)
func AuthRequered(loaders *chains.DataLoaderManager, cookeID string, loginUri string, keyPairs ...[]byte) gin.HandlerFunc {
store := NewMultiStore(loaders, keyPairs...)
return func(c *gin.Context) {
session, _ := store.Get(c.Request, cookeID)
c.Set("session", session)
_, found := session.GetInt("user_id")
if !found {
// 重定向
c.Redirect(302, fmt.Sprintf("%s%s/%s/?next=%s", c.Request.URL.Scheme, c.Request.Host, loginUri, url.QueryEscape(c.Request.URL.RequestURI())))
return
}
}
}
func AuthOptional(loaders *chains.DataLoaderManager, cookeID string, keyPairs ...[]byte) gin.HandlerFunc {
store := NewMultiStore(loaders, keyPairs...)
return func(c *gin.Context) {
session, _ := store.Get(c.Request, cookeID)
c.Set("session", session)
}
}