I'm trying to find out the cookie value that is being saved to the clients browser via s.Save(r, w). That way I can store the cookie value in a database before calling renderTemplate(). Is there a way to accomplish this using the gorilla/sessions api?
func login(w http.ResponseWriter, r *http.Request, db *sql.DB, store *sessions.CookieStore, t *template.Template){
if r.Method == "POST" {
r.ParseForm()
username, password, remember := r.FormValue("user[name]"), r.FormValue("user[password]"), r.FormValue("remember_me")
User, err := users.Login(db, username, password, remember, r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
s, _ := store.Get(r, "rp-session") //returns a new session
s.Save(r, w)
//fmt.Println(s.Values)
renderTemplate(w, "user_nav_info", t, User)
}
}
This is how gorilla's sessions api saves the session - https://github.com/gorilla/sessions/blob/master/store.go#L104-L109
So, you can essentially include the below code to get the exact cookie value being set in client's machine.
encoded, err := securecookie.EncodeMulti(s.Name(), s.Values, store.Codecs...)
if err != nil {
return err
}
cookieVal := NewCookie(s.Name(), encoded, s.Options)
fmt.Println(cookieVal) // cookie value that is sent to client
Although, I'd not suggest you to save the cookie that way. gorilla's sessions api is supposed to the job for you.