I'm stuck on getting sessions to work in GoLang. Specifically within Iris.
Calling a route with "Login" will print out a token; then when I call a route with Restricted, the token is empty. Am I doing something wrong or do I misunderstand the concept behind sessions in GoLang?
func Login(c *iris.Context) {
username := c.FormValueString("email")
password := c.FormValueString("password")
test := "unauthorized"
if username == "jon@snow.com" && password == "123abc!@#" {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"name": "Jon Snow",
"exp": time.Now().Add(time.Hour * 72).Unix(),
})
t, err := token.SignedString([]byte("SecretKey"))
if err != nil {
panic(err)
}
c.Session().Set("token", t)
test = c.Session().GetString("token")
}
c.Text(200, test)
}
func Restricted(c *iris.Context) {
tokenString := c.Session().GetString("token")
if tokenString != "" {
c.Text(iris.StatusOK, tokenString)
} else {
c.Text(iris.StatusOK, "no Token")
}
}