user4211028
user4211028

Reputation:

Go Gorilla Mux Session Name

I am having a hard time understanding Gorilla mux's session name.

http://www.gorillatoolkit.org/pkg/sessions#CookieStore.Get

var store = sessions.NewCookieStore([]byte("something-very-secret"))

func MyHandler(w http.ResponseWriter, r *http.Request) {
    // Get a session. We're ignoring the error resulted from decoding an
    // existing session: Get() always returns a session, even if empty.
    session, _ := store.Get(r, "session-name")
    // Set some session values.
    session.Values["foo"] = "bar"
    session.Values[42] = 43
    // Save it.
    session.Save(r, w)
}

I want to use session to avoid using global variables between two handlers. So I save the key-value in the shared session and retrieve the value from the session.

And I wonder if I want each user to have its own unique session and its Values, do I need to assign unique session name(session id)? Or the gorilla session handles by itself that each user gets its own session and values?

I wonder if I need to generate session names with unique identifiers.

Thanks

Upvotes: 3

Views: 1491

Answers (1)

sberry
sberry

Reputation: 132138

The session data is stored in the client's cookies. So the session you retrieve with store.Get(r, "session-name") is reading that particular client't (request) cookies. You do not need unique names. The name in this case is the name of the cookie so it will be unique to the request.

Upvotes: 7

Related Questions