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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

sess.Regenerate() issue 馃悰 #1395

Closed
EugeneTorap opened this issue Jun 21, 2021 · 2 comments 路 Fixed by #1407
Closed

sess.Regenerate() issue 馃悰 #1395

EugeneTorap opened this issue Jun 21, 2021 · 2 comments 路 Fixed by #1407

Comments

@EugeneTorap
Copy link

Fiber version
v2.13.0

Issue description
I want to write a refresh api for regenerating a session but it doesn't regenerate session every second call.
When call refresh in first time the fresh of sess is true and I have a session. In second time the fresh of sess is false and sess.Save() doesn't save a new session. In third time the fresh of sess is true and I have a session.
I think issue is sess.Regenerate() does't set s.fresh = true.

Code snippet

package main

import (
	"fmt"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/session"
	"log"
)

var store = session.New()

func main() {
	app := fiber.New()

	app.Get("/", func(ctx *fiber.Ctx) error {
		sess, err := store.Get(ctx)
		if err != nil {
			return err
		}
		name := sess.Get("name")
		str := fmt.Sprintf("%v", name)
		return ctx.SendString(str)
	})
	app.Get("/refresh", func(ctx *fiber.Ctx) error {
		sess, err := store.Get(ctx)
		if err != nil {
			return err
		}

		fmt.Println(sess.ID(), sess.Fresh())
		if err = sess.Regenerate(); err != nil {
			return err
		}

		sess.Set("name", "bob")

		fmt.Println(sess.ID(), sess.Fresh())
		if err = sess.Save(); err != nil {
			return err
		}

		fmt.Println("The session is refreshed")
		return ctx.SendString("The session is refreshed")
	})
	log.Fatal(app.Listen(":8080"))
}
@welcome
Copy link

welcome bot commented Jun 21, 2021

Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@hi019
Copy link
Contributor

hi019 commented Jun 27, 2021

You're right, the issue is with Session.fresh. The session is only saved if it's new:

https://github.com/gofiber/fiber/blob/master/middleware/session/session.go#L141-L143

Calling Session.Regenerate and then Session.Save doesn't update the cookie, so in the next request the browser sends the previous session ID which was deleted.

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

Successfully merging a pull request may close this issue.

3 participants