2021-08-12 19:03:24 +00:00
# sessions
2024-03-25 11:00:36 +00:00
[![Run CI Lint ](https://github.com/gin-contrib/sessions/actions/workflows/lint.yml/badge.svg?branch=master )](https://github.com/gin-contrib/sessions/actions/workflows/lint.yml)
[![Run Testing ](https://github.com/gin-contrib/sessions/actions/workflows/testing.yml/badge.svg?branch=master )](https://github.com/gin-contrib/sessions/actions/workflows/testing.yml)
2021-08-12 19:03:24 +00:00
[![codecov ](https://codecov.io/gh/gin-contrib/sessions/branch/master/graph/badge.svg )](https://codecov.io/gh/gin-contrib/sessions)
[![Go Report Card ](https://goreportcard.com/badge/github.com/gin-contrib/sessions )](https://goreportcard.com/report/github.com/gin-contrib/sessions)
[![GoDoc ](https://godoc.org/github.com/gin-contrib/sessions?status.svg )](https://godoc.org/github.com/gin-contrib/sessions)
Gin middleware for session management with multi-backend support:
- [cookie-based ](#cookie-based )
- [Redis ](#redis )
- [memcached ](#memcached )
- [MongoDB ](#mongodb )
2024-03-25 11:00:36 +00:00
- [GORM ](#gorm )
2021-08-12 19:03:24 +00:00
- [memstore ](#memstore )
2021-11-27 14:26:58 +00:00
- [PostgreSQL ](#postgresql )
2021-08-12 19:03:24 +00:00
## Usage
### Start using it
Download and install it:
```bash
2021-11-27 14:26:58 +00:00
go get github.com/gin-contrib/sessions
2021-08-12 19:03:24 +00:00
```
Import it in your code:
```go
import "github.com/gin-contrib/sessions"
```
## Basic Examples
### single session
```go
package main
import (
2021-11-27 14:26:58 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
2021-08-12 19:03:24 +00:00
)
func main() {
2021-11-27 14:26:58 +00:00
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
2021-08-12 19:03:24 +00:00
2021-11-27 14:26:58 +00:00
r.GET("/hello", func(c *gin.Context) {
session := sessions.Default(c)
2021-08-12 19:03:24 +00:00
2021-11-27 14:26:58 +00:00
if session.Get("hello") != "world" {
session.Set("hello", "world")
session.Save()
}
2021-08-12 19:03:24 +00:00
2021-11-27 14:26:58 +00:00
c.JSON(200, gin.H{"hello": session.Get("hello")})
})
r.Run(":8000")
2021-08-12 19:03:24 +00:00
}
```
### multiple sessions
```go
package main
import (
2021-11-27 14:26:58 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
2021-08-12 19:03:24 +00:00
)
func main() {
2021-11-27 14:26:58 +00:00
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
sessionNames := []string{"a", "b"}
r.Use(sessions.SessionsMany(sessionNames, store))
r.GET("/hello", func(c *gin.Context) {
sessionA := sessions.DefaultMany(c, "a")
sessionB := sessions.DefaultMany(c, "b")
if sessionA.Get("hello") != "world!" {
sessionA.Set("hello", "world!")
sessionA.Save()
}
if sessionB.Get("hello") != "world?" {
sessionB.Set("hello", "world?")
sessionB.Save()
}
c.JSON(200, gin.H{
"a": sessionA.Get("hello"),
"b": sessionB.Get("hello"),
})
})
r.Run(":8000")
2021-08-12 19:03:24 +00:00
}
```
2021-11-27 14:26:58 +00:00
## Backend Examples
2021-08-12 19:03:24 +00:00
### cookie-based
```go
package main
import (
2021-11-27 14:26:58 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
2021-08-12 19:03:24 +00:00
)
func main() {
2021-11-27 14:26:58 +00:00
r := gin.Default()
store := cookie.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
2021-08-12 19:03:24 +00:00
}
```
### Redis
```go
package main
import (
2021-11-27 14:26:58 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin"
2021-08-12 19:03:24 +00:00
)
func main() {
2021-11-27 14:26:58 +00:00
r := gin.Default()
store, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
2021-08-12 19:03:24 +00:00
}
```
### Memcached
#### ASCII Protocol
```go
package main
import (
2021-11-27 14:26:58 +00:00
"github.com/bradfitz/gomemcache/memcache"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memcached"
"github.com/gin-gonic/gin"
2021-08-12 19:03:24 +00:00
)
func main() {
2021-11-27 14:26:58 +00:00
r := gin.Default()
store := memcached.NewStore(memcache.New("localhost:11211"), "", []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
2021-08-12 19:03:24 +00:00
}
```
#### Binary protocol (with optional SASL authentication)
```go
package main
import (
2021-11-27 14:26:58 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memcached"
"github.com/gin-gonic/gin"
"github.com/memcachier/mc"
2021-08-12 19:03:24 +00:00
)
func main() {
2021-11-27 14:26:58 +00:00
r := gin.Default()
client := mc.NewMC("localhost:11211", "username", "password")
store := memcached.NewMemcacheStore(client, "", []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
2021-08-12 19:03:24 +00:00
}
```
### MongoDB
2022-05-02 13:05:18 +00:00
#### mgo
2024-03-25 11:00:36 +00:00
2021-08-12 19:03:24 +00:00
```go
package main
import (
2021-11-27 14:26:58 +00:00
"github.com/gin-contrib/sessions"
2022-05-02 13:05:18 +00:00
"github.com/gin-contrib/sessions/mongo/mongomgo"
2021-11-27 14:26:58 +00:00
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo"
2021-08-12 19:03:24 +00:00
)
func main() {
2021-11-27 14:26:58 +00:00
r := gin.Default()
session, err := mgo.Dial("localhost:27017/test")
if err != nil {
// handle err
}
c := session.DB("").C("sessions")
2022-05-02 13:05:18 +00:00
store := mongomgo.NewStore(c, 3600, true, []byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
```
#### mongo-driver
2024-03-25 11:00:36 +00:00
```go
2022-05-02 13:05:18 +00:00
package main
import (
"context"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/mongo/mongodriver"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func main() {
r := gin.Default()
mongoOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.NewClient(mongoOptions)
if err != nil {
// handle err
}
if err := client.Connect(context.Background()); err != nil {
// handle err
}
c := client.Database("test").Collection("sessions")
store := mongodriver.NewStore(c, 3600, true, []byte("secret"))
2021-11-27 14:26:58 +00:00
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
```
2021-08-12 19:03:24 +00:00
2021-11-27 14:26:58 +00:00
### memstore
2021-08-12 19:03:24 +00:00
2021-11-27 14:26:58 +00:00
```go
package main
import (
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
store := memstore.NewStore([]byte("secret"))
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
2021-08-12 19:03:24 +00:00
}
```
2024-03-25 11:00:36 +00:00
### GORM
2022-05-02 13:05:18 +00:00
```go
package main
import (
"github.com/gin-contrib/sessions"
gormsessions "github.com/gin-contrib/sessions/gorm"
"github.com/gin-gonic/gin"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func main() {
db, err := gorm.Open(sqlite.Open("test.db"), & gorm.Config{})
if err != nil {
panic(err)
}
store := gormsessions.NewStore(db, true, []byte("secret"))
r := gin.Default()
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
}
```
2021-11-27 14:26:58 +00:00
### PostgreSQL
2021-08-12 19:03:24 +00:00
```go
package main
import (
2022-05-02 13:05:18 +00:00
"database/sql"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/postgres"
"github.com/gin-gonic/gin"
2021-08-12 19:03:24 +00:00
)
func main() {
2022-05-02 13:05:18 +00:00
r := gin.Default()
db, err := sql.Open("postgres", "postgresql://username:password@localhost:5432/database")
if err != nil {
// handle err
}
store, err := postgres.NewStore(db, []byte("secret"))
if err != nil {
// handle err
}
r.Use(sessions.Sessions("mysession", store))
r.GET("/incr", func(c *gin.Context) {
session := sessions.Default(c)
var count int
v := session.Get("count")
if v == nil {
count = 0
} else {
count = v.(int)
count++
}
session.Set("count", count)
session.Save()
c.JSON(200, gin.H{"count": count})
})
r.Run(":8000")
2021-08-12 19:03:24 +00:00
}
```