Exemplo n.º 1
0
//RequiresLogin is a middleware which will be used for each httpHandler to check if there is any active session
func RequiresLogin(handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		if !sessions.IsLoggedIn(r) {
			http.Redirect(w, r, "/login/", 302)
			return
		}
		handler(w, r)
	}
}
Exemplo n.º 2
0
//ShowCategoryFunc will populate the /category/<id> URL which shows all the tasks related
// to that particular category
func ShowCategoryFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" && sessions.IsLoggedIn(r) {
		category := r.URL.Path[len("/category/"):]
		username := sessions.GetCurrentUserName(r)
		context, err := db.GetTasks(username, "", category)
		categories := db.GetCategories(username)

		if err != nil {
			http.Redirect(w, r, "/", http.StatusInternalServerError)
		}
		if message != "" {
			context.Message = message
		}
		context.CSRFToken = "abcd"
		context.Categories = categories
		message = ""
		expiration := time.Now().Add(365 * 24 * time.Hour)
		cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
		http.SetCookie(w, &cookie)
		homeTemplate.Execute(w, context)
	}
}