Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session will not be registered on Redis store when setting Options on Redis store #238

Open
Mozuha opened this issue Jun 10, 2023 · 2 comments

Comments

@Mozuha
Copy link

Mozuha commented Jun 10, 2023

When the code like the below is run:

redisStore, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
router.Use(sessions.Sessions("mysession", redisStore))

func CalledFirst(c *gin.Context) {
  session := sessions.Default(c)
  session.Set("count", 1)
  session.Save()
}

func CalledSecond(c *gin.Context) {
  session := sessions.Default(c)
  count := session.Get("count")
  fmt.Println(count)
}

It works fine. I could confirm that the value is correctly retrieved from the session and the session is registered as "session_randomstringkey" in my Redis container.

However, if I add Options to the Redis store as:

redisStore, _ := redis.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
redisStore.Options(sessions.Options{Path: "/"})   // or set any arbitrary field
router.Use(sessions.Sessions("mysession", redisStore))

// same code as above follows...

It does not. The value of the variable count will be nil and nothing is registered in the Redis container.
Any clue?

@emadmrz
Copy link

emadmrz commented Dec 24, 2023

same problem here!!

@emadmrz
Copy link

emadmrz commented Dec 24, 2023

Found the solution.
MaxAge should be added to the sessions.Options

session.Options(sessions.Options{
		Path:     "/",
		Domain:   "localhost",
		SameSite: http.SameSiteLaxMode,
		MaxAge:   86400 * 30, // 30 days
	})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants