Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)))
- }
- 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)
- }
- }
- 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)
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement