gin-contrib / sessions

Gin middleware for session management
MIT License
1.44k stars 193 forks source link

Session is not working after redirect #152

Open ayman-elmalah opened 2 years ago

ayman-elmalah commented 2 years ago

Session is working fine in the same handler, but when redirecting from route to other one, session is not working

also i used session in routing class like this

store := cookie.NewStore([]byte("secret"))
router.Use(sessions.Sessions("sessions", store))

Here is the controller code

func (controller *TestController) Register(c *gin.Context) {
    var session = sessions.Default(c)

    session.AddFlash(models.ErrorMsg{Field: "name", Message: "invalid input"}, "errors")
    session.Save()

    c.Redirect(http.StatusMovedPermanently, "/register")
    return
}

func (controller *TestController) HandleRegister(c *gin.Context) {
    var session = sessions.Default(c)

    errors := session.Flashes("errors")

    c.JSON(200, gin.H{
        "errors": errors,
    })
}
LianYangCn commented 2 years ago

see #153

hhandhuan commented 2 years ago

You can refer to my project https://github.com/hhandhuan/ku-bbs @ayman-elmalah

jeven2016 commented 1 year ago

Any update for this issue? Does anybody know the final conclusion or workround?

djanderson commented 1 year ago

What solved a similar issue for me was to handle errors in session.Save(), unlike what is shown in the examples.

if err := session.Save(); err != nil {
    log.Print("Error saving session: ", err)
}

Error saving session: securecookie: error - caused by: securecookie: error - caused by: gob: type not registered for interface: base.AuthResult

That indicates the thing I was trying to store on the session couldn't be serialized. It appears one solution includes using gob.Register but I just pulled out the string I needed and it worked fine.