I'm trying to decode the cookie encoded by securecookie. Below is how it is encoded:
func SessionHandler(w http.ResponseWriter, accessToken string, hash []byte) { sc := securecookie.New(hash, nil) if encoded, err := sc.Encode("access_token", accessToken); err == nil { cookie := &http.Cookie{ Name: "access_token", Value: encoded, HttpOnly: true, Expires: time.Now().Local().Add(time.Hour * time.Duration(1)), } http.SetCookie(w, cookie) } }
And below is how it is decoded.
func TokenExtractor(r *http.Request, hash []byte) (string, error) { sc := securecookie.New(hash, nil) cookie, err := r.Cookie("access_token") if err != nil { return "", err } value := make(map[string]string) err = sc.Decode("access_token", cookie.Value, &value) return value["access_token"], err }
Using the TokenExtractor
if hashKey, err := cfg.CookieKey("hash"); err == nil { tkn, err := TokenExtractor(r, hashKey) if err != nil { log.Println(err) } log.Println(tkn) }
I'm getting this error:
securecookie: error - caused by: securecookie: error - caused by: gob: decoding into local type *map[string]string, received remote type string
P.S: I'm sorry I'm not sure how to format the code above properly here.