gorilla/sessions

FilesystemStore: securecookie: the value is not valid

flux-i opened this issue · 4 comments

Sample code picked from :#78 (comment)
I modified the code a bit using FilesystemStore, i am getting securecookie error: value is not valid.
PS: This is my first time submitting a issue on git, have mercy if there are any problems in the post

import (
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	"github.com/gorilla/sessions"
)

type Handler struct {
	Fstore *sessions.FilesystemStore
}

func main() {
	mux := mux.NewRouter()

	h := Handler{}
	h.Fstore = sessions.NewFilesystemStore("", []byte("key-1"), nil)
	mux.HandleFunc("/set", h.SetHandler)
	mux.HandleFunc("/delete", h.DeleteHandler)

	log.Fatal(http.ListenAndServe("localhost:8010", mux))
}

func (h *Handler) SetHandler(w http.ResponseWriter, r *http.Request) {
	sess, err := h.Fstore.Get(r, "app")
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	log.Printf("value before: %v", sess.Values["user"])

	sess.Values["user"] = "gorilla!"
	err = sess.Save(r, w)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	fmt.Fprintf(w, "Cookie set to %v", sess.Values)
}

func (h *Handler) DeleteHandler(w http.ResponseWriter, r *http.Request) {
	sess, err := h.Fstore.Get(r, "app")
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	sess.Options.MaxAge = -1

	err = sess.Save(r, w)
	if err != nil {
		http.Error(w, err.Error(), 400)
		return
	}
	log.Println("deleted")
}
stale commented

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.

That error is normal if you change the secret key and you didn't clear your browser's cookies.

Correct. You likely want to persist the key outside of source code - changing the key will result in old session cookies being invalid.

stale commented

This issue has been automatically marked as stale because it hasn't seen a recent update. It'll be automatically closed in a few days.