func tryLogin(username, password string) (http.Cookie, error) {
if exists := db.UserExists(username, password); !exists {
return http.Cookie{},
errors.New("The username or password you entered isn't correct.")
}
sid, err := randString(32)
if err != nil {
return http.Cookie{}, err
}
sessions[sid] = true
loginCookie := http.Cookie{
Name: "id",
Value: sid,
MaxAge: int((time.Hour * 12).Seconds()),
HttpOnly: true,
Domain: "mydomain.com",
Path: "/admin/",
}
return loginCookie, nil
}
func randString(size int) (string, error) {
buf := make([]byte, size)
if _, err := rand.Read(buf); err != nil {
log.Println(err)
return "", errors.New("Couldn't generate random string")
}
return base64.URLEncoding.EncodeToString(buf)[:size], nil
}