-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
55 lines (48 loc) · 1.19 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"net/http"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
store := sessions.NewCookieStore([]byte("secret"))
store.Options(sessions.Options{
MaxAge: int(30 * time.Minute), //30min
Path: "/",
})
r.Use(sessions.Sessions("mysession", store))
r.GET("/clear", clear)
r.GET("/pre", preSession)
r.GET("/do", DoSomething)
r.Run(":8000")
}
func preSession(c *gin.Context) {
userAccessToken := "test.access.token"
session := sessions.Default(c)
session.Set("[email protected]", userAccessToken)
session.Save()
fmt.Printf("[preSession] user access token :%s has been saved to session\n", userAccessToken)
c.JSON(http.StatusOK, nil)
}
func DoSomething(c *gin.Context) {
userEmail := c.Query("user_email")
if userEmail == "" {
panic("can not get user email")
}
session := sessions.Default(c)
session.Save()
userAccessToken := session.Get(userEmail)
fmt.Printf("[DoSomethine] user access token is %v\n", userAccessToken)
c.JSON(http.StatusOK, nil)
}
func clear(c *gin.Context) {
session := sessions.Default(c)
session.Clear()
session.Save()
c.JSON(http.StatusOK, gin.H{
"message": "clear session",
})
}