Example #1
1
func (h *Handler) handleVerify(w http.ResponseWriter, r *http.Request) {
	segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
	vid := segments[2]

	u, err := user.Verify(vid)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	s, err := session.New(u)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	// Drop cookie
	err = s.Save(w, true)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	url := fmt.Sprintf("%s/u/%s", config.Get("baseurl"), u.Username)
	http.Redirect(w, r, url, http.StatusFound)
}
Example #2
0
func (h *Handler) handleLoginPost(w http.ResponseWriter, r *http.Request) {
	username := strings.TrimSpace(r.FormValue("username"))
	password := strings.TrimSpace(r.FormValue("password"))

	// Validate
	v := validation.New()
	v.Required("username", username, "username is required")
	v.Required("password", password, "password is required")

	if len(v.Errors) == 0 {
		u, err := user.Login(username, password)
		if err == nil {
			s, err := session.New(u)
			if err != nil {
				logger.Error(w, err)
				h.serveServerError(w, r)
				return
			}

			err = s.Save(w, true)
			if err != nil {
				logger.Error(w, err)
				h.serveServerError(w, r)
				return
			}

			url := fmt.Sprintf("%s/u/%s", config.Get("baseurl"), u.Username)
			http.Redirect(w, r, url, http.StatusFound)
			return
		} else {
			if err.Error() == "sql: no rows in result set" {
				v.Errors["username"] = "******"
			}
		}
	}

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
		"form": map[string]string{
			"username": username,
		},
		"errors": v.Errors,
	}

	h.Templates.ExecuteTemplate(w, "login.html", m)
}