Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,56 @@ func main() {
}
```

### multiple sessions with different stores

```go
package main

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

func main() {
r := gin.Default()
cookieStore := cookie.NewStore([]byte("secret"))
redisStore, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
sessionStores := []sessions.SessionStore{
{
Name: "a",
Store: cookieStore,
},
{
Name: "b",
Store: redisStore,
},
}
r.Use(sessions.SessionsManyStores(sessionStores))

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")
}
```

## Backend Examples

### cookie-based
Expand Down
4 changes: 4 additions & 0 deletions cookie/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ func TestCookie_SessionOptions(t *testing.T) {
func TestCookie_SessionMany(t *testing.T) {
tester.Many(t, newStore)
}

func TestCookie_SessionManyStores(t *testing.T) {
tester.ManyStores(t, newStore)
}
8 changes: 8 additions & 0 deletions memcached/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func TestMemcached_SessionMany(t *testing.T) {
tester.Many(t, newStore)
}

func TestMemcached_SessionManyStores(t *testing.T) {
tester.ManyStores(t, newStore)
}

var newBinaryStore = func(_ *testing.T) sessions.Store {
store := NewMemcacheStore(
mc.NewMC(memcachedTestServer, "", ""), "", []byte("secret"))
Expand Down Expand Up @@ -70,3 +74,7 @@ func TestBinaryMemcached_SessionOptions(t *testing.T) {
func TestBinaryMemcached_SessionMany(t *testing.T) {
tester.Many(t, newBinaryStore)
}

func TestBinaryMemcached_SessionManyStores(t *testing.T) {
tester.ManyStores(t, newBinaryStore)
}
4 changes: 4 additions & 0 deletions memstore/memstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ func TestCookie_SessionOptions(t *testing.T) {
func TestCookie_SessionMany(t *testing.T) {
tester.Many(t, newStore)
}

func TestCookie_SessionManyStores(t *testing.T) {
tester.ManyStores(t, newStore)
}
4 changes: 4 additions & 0 deletions mongo/mongomgo/mongomgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ func TestMongoMGO_SessionOptions(t *testing.T) {
func TestMongoMGO_SessionMany(t *testing.T) {
tester.Many(t, newStore)
}

func TestMongo_SessionManyStores(t *testing.T) {
tester.ManyStores(t, newStore)
}
4 changes: 4 additions & 0 deletions postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ func TestPostgres_SessionOptions(t *testing.T) {
func TestPostgres_SessionMany(t *testing.T) {
tester.Many(t, newStore)
}

func TestPostgres_SessionManyStores(t *testing.T) {
tester.ManyStores(t, newStore)
}
4 changes: 4 additions & 0 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func TestRedis_SessionMany(t *testing.T) {
tester.Many(t, newRedisStore)
}

func TestRedis_SessionManyStores(t *testing.T) {
tester.ManyStores(t, newRedisStore)
}

func TestGetRedisStore(t *testing.T) {
t.Run("unmatched type", func(t *testing.T) {
type store struct{ Store }
Expand Down
18 changes: 18 additions & 0 deletions sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ type Session interface {
Save() error
}

// SessionStore named session stores allow multiple sessions with different store types
type SessionStore struct {
Name string
Store Store
}

func Sessions(name string, store Store) gin.HandlerFunc {
return func(c *gin.Context) {
s := &session{name, c.Request, store, nil, false, c.Writer}
Expand All @@ -67,6 +73,18 @@ func SessionsMany(names []string, store Store) gin.HandlerFunc {
}
}

func SessionsManyStores(sessionStores []SessionStore) gin.HandlerFunc {
return func(c *gin.Context) {
sessions := make(map[string]Session, len(sessionStores))
for _, sessionStore := range sessionStores {
sessions[sessionStore.Name] = &session{sessionStore.Name, c.Request, sessionStore.Store, nil, false, c.Writer}
}
c.Set(DefaultKey, sessions)
defer context.Clear(c.Request)
c.Next()
}
}

type session struct {
name string
request *http.Request
Expand Down
51 changes: 51 additions & 0 deletions tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,57 @@ func Many(t *testing.T, newStore storeFactory) {
r.ServeHTTP(res2, req2)
}

func ManyStores(t *testing.T, newStore storeFactory) {
r := gin.Default()

store := newStore(t)
sessionStores := []sessions.SessionStore{
{Name: "a", Store: store},
{Name: "b", Store: store},
}

r.Use(sessions.SessionsManyStores(sessionStores))

r.GET("/set", func(c *gin.Context) {
sessionA := sessions.DefaultMany(c, "a")
sessionA.Set("hello", "world")
_ = sessionA.Save()

sessionB := sessions.DefaultMany(c, "b")
sessionB.Set("foo", "bar")
_ = sessionB.Save()
c.String(http.StatusOK, ok)
})

r.GET("/get", func(c *gin.Context) {
sessionA := sessions.DefaultMany(c, "a")
if sessionA.Get("hello") != "world" {
t.Error("Session writing failed")
}
_ = sessionA.Save()

sessionB := sessions.DefaultMany(c, "b")
if sessionB.Get("foo") != "bar" {
t.Error("Session writing failed")
}
_ = sessionB.Save()
c.String(http.StatusOK, ok)
})

res1 := httptest.NewRecorder()
req1, _ := http.NewRequestWithContext(context.Background(), "GET", "/set", nil)
r.ServeHTTP(res1, req1)

res2 := httptest.NewRecorder()
req2, _ := http.NewRequestWithContext(context.Background(), "GET", "/get", nil)
header := ""
for _, x := range res1.Header()["Set-Cookie"] {
header += strings.Split(x, ";")[0] + "; \n"
}
req2.Header.Set("Cookie", header)
r.ServeHTTP(res2, req2)
}

func copyCookies(req *http.Request, res *httptest.ResponseRecorder) {
req.Header.Set("Cookie", strings.Join(res.Header().Values("Set-Cookie"), "; "))
}
Loading