Skip to content

Commit

Permalink
revise the cookie store and gin implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeee committed Apr 20, 2019
1 parent 1689c46 commit b6613f2
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions README.md
Expand Up @@ -13,34 +13,38 @@ $ go get github.com/utrack/gin-csrf
## Usage

``` go
import (
"errors"
package main

"github.com/gin-gonic/gin"
"github.com/gin-contrib/sessions"
"github.com/utrack/gin-csrf"
import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/utrack/gin-csrf"
)

func main(){
r := gin.Default()
store := sessions.NewCookieStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.Use(csrf.Middleware(csrf.Options{
Secret: "secret123",
ErrorFunc: func(c *gin.Context){
c.String(400, "CSRF token mismatch")
func main() {
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.Use(csrf.Middleware(csrf.Options{
Secret: "secret123",
ErrorFunc: func(c *gin.Context) {
c.String(400, "CSRF token mismatch")
c.Abort()
},
}))
},
}))

r.GET("/protected", func(c *gin.Context){
c.String(200, csrf.GetToken(c))
})
r.GET("/protected", func(c *gin.Context) {
c.String(200, csrf.GetToken(c))
})

r.POST("/protected", func(c *gin.Context){
c.String(200, "CSRF token is valid")
})
r.POST("/protected", func(c *gin.Context) {
c.String(200, "CSRF token is valid")
})

r.Run(":8080")
}

```

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

0 comments on commit b6613f2

Please sign in to comment.