I'm having trouble getting Golang Gorilla sessions to work correctly.
I have gotten this as simple as I can to try and reproduce the error.
Main (note that I am using some stuff to take care of CORS with might be an issue)
main(){
<OTHER CODE>
log.Print("Instatiate Cookiestore")
config.Configure_Sessions()
log.Print("Instantiated")
<MORE CODE>
router := NewRouter()
os.Setenv("ORIGIN_ALLOWED", "*")
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type"})
originsOk := handlers.AllowedOrigins([]string{os.Getenv("ORIGIN_ALLOWED")})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
// start server listen
// with error handling
log.Fatal(http.ListenAndServe(":8080", handlers.CORS(originsOk, headersOk, methodsOk)(router)))
}
Sessions
package config
import (
"net/http"
"github.com/gorilla/sessions"
)
var (
// Store is the cookie store
Store *sessions.CookieStore
// Name is the session name
Name string
)
// Session stores session level information
type Session struct {
Options sessions.Options `json:"Options"` // Pulled from: http://www.gorillatoolkit.org/pkg/sessions#Options
Name string `json:"Name"` // Name for: http://www.gorillatoolkit.org/pkg/sessions#CookieStore.Get
SecretKey string `json:"SecretKey"` // Key for: http://www.gorillatoolkit.org/pkg/sessions#CookieStore.New
}
// Configure the session cookie store
func Configure_Sessions() {
Store = sessions.NewCookieStore([]byte("adsads;fja;i4gna;nbeq09bjse"))
// Store.Options = &s.Options
// Name = s.Name
}
func Instance(r *http.Request) (*sessions.Session, error) {
session, err := Store.Get(r, "cookies4dndyo")
return session, err
}
// Empty deletes all the current session values
func Empty(sess *sessions.Session) {
// Clear out all stored values in the cookie
for k := range sess.Values {
delete(sess.Values, k)
}
}
In order to ensure that this functionality would work I copied most of the code from here https://github.com/josephspurrier/gowebapp/blob/master/vendor/app/shared/session/session.go.
Here is the place that I call login. This is where I am putting a bunch of tests to try and see what is going wrong. What I do to test is I hit log in twice. You can see that I can write to the test variable, but when I want to append to a persisting variable it is constantly nil.
func (incomingjson *User) Login(w http.ResponseWriter, r *http.Request) {
log.Print("Inside Login Function")
session, err := config.Instance(r)
log.Print("After session assignment in login function")
log.Print("What is the value of session, err?")
log.Print("Session: ", session)
log.Print("Err: ", err)
log.Print("Set Value and Test")
session.Values["testing"] = "hellotheresailor"
log.Print("session.Values[testing]: ", session.Values["testing"])
log.Print("Test with previous values: ")
if session.Values["testingappend"] == nil {
log.Print("inside testingappen nil if statement")
session.Values["testingappend"] = "hohoho"
log.Print(session.Values["testingappend"])
} else {
log.Print("inside testingappend not nil else statement")
session.Values["testingappend"] = session.Values["testingappend"].(string) + session.Values["testing"].(string)
log.Print("Value of session.Values[testingappend]: ", session.Values["testingappend"])
}
session.Save(r, w)
<CODE CONTINUES - END OF EXAMPLE>
Terminal Output from this:
patientplatypus:~/Documents/golang/src/github.com/patientplatypus/gorest:20:00:18$gorest
Successfully connected~!
2017/10/15 20:00:19 Instatiate Cookiestore
2017/10/15 20:00:19 Instantiated
2017/10/15 20:00:36 username: patientplatypus
2017/10/15 20:00:36 password: Fvnjty0b
2017/10/15 20:00:36 Inside Login Function
2017/10/15 20:00:36 After session assignment in login function
2017/10/15 20:00:36 What is the value of session, err?
2017/10/15 20:00:36 Session: &{ map[] 0xc42015e330 true 0xc4200e1040 cookies4dndyo}
2017/10/15 20:00:36 Err: <nil>
2017/10/15 20:00:36 Set Value and Test
2017/10/15 20:00:36 session.Values[testing]: hellotheresailor
2017/10/15 20:00:36 Test with previous values:
2017/10/15 20:00:36 inside testingappen nil if statement
2017/10/15 20:00:36 hohoho
2017/10/15 20:00:36 loginjson: &{patientplatypus Fvnjty0b 0}
2017/10/15 20:00:36 err: <nil>
2017/10/15 20:00:36 Found username
2017/10/15 20:00:36 username and password match!
2017/10/15 20:00:36 Value of session.Values[authenticated], session.values[username]: true patientplatypus
2017/10/15 20:00:59 username: theGreatDM
2017/10/15 20:00:59 password: theGreatDM
2017/10/15 20:00:59 Inside Login Function
2017/10/15 20:00:59 After session assignment in login function
2017/10/15 20:00:59 What is the value of session, err?
2017/10/15 20:00:59 Session: &{ map[] 0xc4201ba2a0 true 0xc4200e1040 cookies4dndyo}
2017/10/15 20:00:59 Err: <nil>
2017/10/15 20:00:59 Set Value and Test
2017/10/15 20:00:59 session.Values[testing]: hellotheresailor
2017/10/15 20:00:59 Test with previous values:
2017/10/15 20:00:59 inside testingappen nil if statement
2017/10/15 20:00:59 hohoho
2017/10/15 20:00:59 loginjson: &{theGreatDM theGreatDM 0}
2017/10/15 20:00:59 err: <nil>
2017/10/15 20:00:59 Found username
2017/10/15 20:00:59 username and password match!
2017/10/15 20:00:59 Value of session.Values[authenticated], session.values[username]: true theGreatDM
Testingappend should output hohoho
once and hellotheresailorhohoho
once. Instead it outputs hohoho
twice as the second call it does not recognize a persisting sessions and exits.
I've also noticed this :
It appears that the cookies that are supposed to be saved to the browser are not getting entered in. I've tested this on localhost and 127.0.0.1 and both with port numbers as well.
I am very confused.
This is not the simplest example of this package, but it is close. The only major difference is that I'm putting all the sessions handling in its own global file so that the connection can be accessed anywhere. I've been very lost on this for a while. Any help would be appreciated.