Skip to content

Commit b6613f2

Browse files
committed
revise the cookie store and gin implementation
1 parent 1689c46 commit b6613f2

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

README.md

+25-21
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,38 @@ $ go get github.com/utrack/gin-csrf
1313
## Usage
1414

1515
``` go
16-
import (
17-
"errors"
16+
package main
1817

19-
"github.com/gin-gonic/gin"
20-
"github.com/gin-contrib/sessions"
21-
"github.com/utrack/gin-csrf"
18+
import (
19+
"github.com/gin-contrib/sessions"
20+
"github.com/gin-contrib/sessions/cookie"
21+
"github.com/gin-gonic/gin"
22+
"github.com/utrack/gin-csrf"
2223
)
2324

24-
func main(){
25-
r := gin.Default()
26-
store := sessions.NewCookieStore([]byte("secret"))
27-
r.Use(sessions.Sessions("mysession", store))
28-
r.Use(csrf.Middleware(csrf.Options{
29-
Secret: "secret123",
30-
ErrorFunc: func(c *gin.Context){
31-
c.String(400, "CSRF token mismatch")
25+
func main() {
26+
r := gin.Default()
27+
store := cookie.NewStore([]byte("secret"))
28+
r.Use(sessions.Sessions("mysession", store))
29+
r.Use(csrf.Middleware(csrf.Options{
30+
Secret: "secret123",
31+
ErrorFunc: func(c *gin.Context) {
32+
c.String(400, "CSRF token mismatch")
3233
c.Abort()
33-
},
34-
}))
34+
},
35+
}))
3536

36-
r.GET("/protected", func(c *gin.Context){
37-
c.String(200, csrf.GetToken(c))
38-
})
37+
r.GET("/protected", func(c *gin.Context) {
38+
c.String(200, csrf.GetToken(c))
39+
})
3940

40-
r.POST("/protected", func(c *gin.Context){
41-
c.String(200, "CSRF token is valid")
42-
})
41+
r.POST("/protected", func(c *gin.Context) {
42+
c.String(200, "CSRF token is valid")
43+
})
44+
45+
r.Run(":8080")
4346
}
47+
4448
```
4549

4650
[Gin]: http://gin-gonic.github.io/gin/

0 commit comments

Comments
 (0)