contrib icon indicating copy to clipboard operation
contrib copied to clipboard

session Options is not working

Open suyulin opened this issue 7 years ago • 10 comments

If i customize the options, like this

                session := sessions.Default(c)
		session.Options(sessions.Options{MaxAge: 60})

the cookie is lost

       session := sessions.Default(c)
	val := session.Get("user")
	if val == nil ?

suyulin avatar Jul 18 '17 07:07 suyulin

Did you do session.Save() after session.Options() ?

mrichman avatar Aug 08 '17 13:08 mrichman

yes,I found that the use of cookies alone, expiration time will not take effect, must be with the data can be, is this ?

suyulin avatar Aug 09 '17 01:08 suyulin

@suyulin May you send example source code?

sshaplygin avatar Dec 09 '19 13:12 sshaplygin

hi @mrfoe7 . it seems like i am facing the similar problem. This is the example code

func login(c *gin.Context) { // Save the username in the session session.Set(userkey, form.Username) // In real world usage you'd set this to the users ID session.Options(sessions.Options{MaxAge: 3600 * 12}) if err := session.Save(); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save session"}) return } } and on the middleware func AuthRequired(c *gin.Context) { session := sessions.Default(c) user := session.Get(userkey) log.Println("user", user) // return nil }

davidchandra95 avatar Mar 01 '21 18:03 davidchandra95

I have the same problem as the above, has any one found a solution to the above problem?

pmadjidi avatar Mar 15 '22 10:03 pmadjidi

I have the same problem too, it will lost cookie if I set maxAge.

towelong avatar Jul 29 '22 05:07 towelong

I'm experiencing this too. I'm on the latest versions (at time of writing) of gin, sessions and memcached sessions packages.

ollieparsley avatar Nov 07 '22 23:11 ollieparsley

I have the same problem, please fix it.

hdlinh1808 avatar Dec 12 '22 17:12 hdlinh1808

I have the same problem, I saved a string session key, when I want to get it ,it is nil....

yukk001 avatar Dec 18 '23 10:12 yukk001

//启用session
//store := cookie.NewStore([]byte("xj_boot_golang"))
store := gormsessions.NewStore(global.GVA_DB, true, []byte("secret"))
//store := memstore.NewStore([]byte("xj_boot_golang"))
//store.Options(sessions.Options{MaxAge: 60 * 60 * 24})
Router.Use(sessions.Sessions("mysession", store))

// save session := sessions.Default(c)

session.Set(common.SESSION_ATTR_KEY, captchaId)
_ = session.Save()

//get session := sessions.Default(c) test := session.Get(common.SESSION_ATTR_KEY) test is nil

yukk001 avatar Dec 18 '23 10:12 yukk001