I am writing a web app using the martini-contrib session library. I seem to have run into an issue though. My session don't seem to be moving between requests in the browser.
I have followed the exact guidelines that were outline in the example code, yet my code does not work. Below is the pertinent parts:
Login page:
m.Get("/login", binding.Bind(LoginForm{}), func(r render.Render, session sessions.Session, form LoginForm) string {
// Get info from the database.
conn, err := sql.Open("sqlite3", "ocdns.db")
defer conn.Close()
// Prepare the statement.
stmt, err := conn.Prepare(`
SELECT user_id, username, name_first, name_last, role, team_id
FROM User
WHERE username = ? AND password = ?
LIMIT 1;
`)
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
// Query the database and set appropriate items if a row was actually returned.
var id string
var username string
var name_first string
var name_last string
var role string
var team_id string
err = stmt.QueryRow(form.Username, form.Password).Scan(&id, &username, &name_first, &name_last, &role, &team_id)
if err != nil {
log.Print("!! Bad login from " + form.Username + " with " + form.Password)
log.Fatal(err)
} else {
log.Print(">" + id + "<")
log.Print(">" + username + "<")
log.Print(">" + name_first + "<")
log.Print(">" + name_last + "<")
log.Print(">" + role + "<")
log.Print(">" + team_id + "<")
session.Set("id", id)
session.Set("username", username)
session.Set("name_first", name_first)
session.Set("name_last", name_last)
session.Set("role", role)
session.Set("team_id", team_id)
v := session.Get("name_first")
if v == nil {
log.Print("!! Uh oh.")
}
log.Print(v.(string))
return "OK"
}
return "Bad"
})
Session check page:
m.Get("/session", func(session sessions.Session) string {
var c Context
i := session.Get("id")
if i == nil {
c.Id = -1
}
c.Id, _ = strconv.Atoi(i.(string))
i = session.Get("username")
if i == nil {
log.Print("!! username")
}
if vs, ok := i.(string); ok {
c.Username = vs
} else {
log.Print(vs)
}
log.Print(c)
j, _ := json.Marshal(c)
return string(j)
})
Context struct:
type Context struct {
Id int `json:"id"`
Username string `json:"username"`
NameFirst string `json:"name_first"`
NameLast string `json:"name_last"`
Role string `json:"role"`
TeamId int `json:"team_id"`
}
Finally, my session declaration:
// Create session store.
store := sessions.NewCookieStore([]byte("secret123"))
m.Use(sessions.Sessions("my_session", store))
I have tried emptying my cache to get it to work, but it won't. I did look in Chrome's developer tools to see if storage was being used and I did find that the session moved between two pages, but the output of the session
request was an empty Context
json stucture.
What I need is to figure out why my session data seems to be getting lost between requests and what I can do to fix it. I have looked on here and while nothing fits my exact problem, I did find that there were ones that had problems with their web server configuration.
Thanks in advance!
You should change your first function call as follow to make sure it must goto session first before login.
Here is sample video, hope it help you. Your code should work well. https://gophercasts.io/lessons/8-auth-part-2