[question] response always set-cookie
marktohark opened this issue · 2 comments
marktohark commented
Describe the problem you're having
every response reset session.
is this normal?
Versions
go version go1.12.5 windows/amd64
github.com/gorilla/mux v1.7.3
github.com/gorilla/sessions v1.2.0
…
"Show me the code!"
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
)
var store = sessions.NewCookieStore([]byte("something-very-secret"))
func MyHandler(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "s1")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("vv => ", session)
session.Values["name"] = "kingeastensun"
session.Save(r, w)
}
func MultiSessionHandler(w http.ResponseWriter, r *http.Request) {
session1, err := store.Get(r, "s1")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("kk => ", session1)
session1.Values["name"] = "kingeastensun"
session2, err := store.Get(r, "s2")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("kk => ", session2)
session2.Values["name"] = "kingeastensunyx"
sessions.Save(r, w)
}
func main() {
routes := mux.NewRouter()
routes.HandleFunc("/session", MyHandler)
routes.HandleFunc("/musession", MultiSessionHandler)
http.Handle("/", routes)
http.ListenAndServe(":5655", nil)
}
…
elithrar commented
Yes: if you call sessions.Save(r, w) every time, it will write a cookie to
the response.
This is typically OK: you are keeping the user logged in. If you don’t want
to do that, wrap Save with logic that suits your application.
…On Fri, Jul 12, 2019 at 9:51 AM marktohark ***@***.***> wrote:
*Describe the problem you're having*
every response reset session.
is this normal?
[image: 未命名]
<https://user-images.githubusercontent.com/19359934/61144681-20324e80-a508-11e9-95e4-015c0ddb1a84.png>
[image: 未命名]
<https://user-images.githubusercontent.com/19359934/61144698-30e2c480-a508-11e9-97b2-a2c51ed62b11.png>
*Versions*
go version go1.12.5 windows/amd64
github.com/gorilla/mux v1.7.3
github.com/gorilla/sessions v1.2.0
…
*"Show me the code!"*
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
)
var store = sessions.NewCookieStore([]byte("something-very-secret"))
func MyHandler(w http.ResponseWriter, r *http.Request) {
session, err := store.Get(r, "s1")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("vv => ", session)
session.Values["name"] = "kingeastensun"
session.Save(r, w)
}
func MultiSessionHandler(w http.ResponseWriter, r *http.Request) {
session1, err := store.Get(r, "s1")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("kk => ", session1)
session1.Values["name"] = "kingeastensun"
session2, err := store.Get(r, "s2")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Println("kk => ", session2)
session2.Values["name"] = "kingeastensunyx"
sessions.Save(r, w)
}
func main() {
routes := mux.NewRouter()
routes.HandleFunc("/session", MyHandler)
routes.HandleFunc("/musession", MultiSessionHandler)
http.Handle("/", routes)
http.ListenAndServe(":5655", nil)
}
…
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#198?email_source=notifications&email_token=AAAEQ4HFOHBUZSDO6QYZCY3P7CZB3A5CNFSM4ICNM5TKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G65TXOA>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAEQ4DSWO4DDKSVXSID223P7CZB3ANCNFSM4ICNM5TA>
.
marktohark commented
ok,thanks.