Closed happilymarrieddad closed 6 years ago
Hey guys,
How do I access the store in routes after the middleware. Here is my session middleware func that gets called before any route.
func Init() *mysqlstore.MySQLStore { DB_HOST = os.Getenv("DB_HOST") DB_PORT = os.Getenv("DB_PORT") DB_DATABASE = os.Getenv("DB_DATABASE") DB_USER = os.Getenv("DB_USER") DB_PASSWORD = os.Getenv("DB_PASSWORD") maxAge := 3600 tableName := "go_sessions" connectStr := DB_USER + ":" + DB_PASSWORD + "@tcp(" + DB_HOST + ":" + DB_PORT + ")/" + DB_DATABASE + "?parseTime=true&loc=Local" store, err := mysqlstore.NewMySQLStore(connectStr, tableName, "/", maxAge, []byte(os.Getenv("SECRET"))) if err != nil { panic(err) } return store } func Middleware(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { session, err := store.Get(r, "session") if err != nil { // TODO: Handle this gracefully... panic(err) } userId := session.Values["userId"] log.Println(userId) h.ServeHTTP(w, r) }) }
Then on a route after the middleware how do I access the session?
package Router import ( "../session" ) var routes = Routes{ Route{"SessionStore", "POST", "/session/store", Session.Store}, }
package Session ... // TODO: Create some route that does something with session to test it func Store(w http.ResponseWriter, r *http.Request) { // TODO: Do something with session.Get("TEST") }
I solve this.. just doing the same thing I did with the DB package and made a pointer. Thanks!
Hey guys,
How do I access the store in routes after the middleware. Here is my session middleware func that gets called before any route.
Then on a route after the middleware how do I access the session?