sessions icon indicating copy to clipboard operation
sessions copied to clipboard

Session is not working after redirect

Open ayman-elmalah opened this issue 2 years ago • 4 comments

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,
	})
}

ayman-elmalah avatar Mar 30 '22 16:03 ayman-elmalah

see #153

LianYangCn avatar May 30 '22 00:05 LianYangCn

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

hhandhuan avatar Sep 11 '22 11:09 hhandhuan

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

jeven2016 avatar Nov 28 '22 07:11 jeven2016

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.

djanderson avatar Apr 22 '23 04:04 djanderson