Example #1
0
func (h *Handler) handleNewPost(w http.ResponseWriter, r *http.Request) {
	username := strings.TrimSpace(r.FormValue("username"))
	email := strings.TrimSpace(r.FormValue("email"))
	password := strings.TrimSpace(r.FormValue("password"))

	// Validate
	v := validation.New()
	if v.Required("username", username, "username is required") {
		v.Username("username", username, "invalid username")
	}
	if v.Required("email", email, "email is required") {
		v.Email("email", email, "invalid email")
	}
	v.Required("password", password, "password is required")

	if len(v.Errors) == 0 {
		err := user.Add(username, email, password)
		if err == nil {
			msg := `
                Account created.
                Please check your email for your verification link
            `

			flashdata.Set(w, msg)

			url := fmt.Sprintf("%s/message", config.Get("baseurl"))
			http.Redirect(w, r, url, http.StatusFound)

			return
		} else {
			if err.Error() == "UNIQUE constraint failed: user.username" {
				v.Errors["username"] = "******"
			} else if err.Error() == "UNIQUE constraint failed: user.email" {
				v.Errors["email"] = "email already exists"
			} else {
				logger.Error(w, err)
				h.serveServerError(w, r)
				return
			}
		}
	}

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

	h.Templates.ExecuteTemplate(w, "new.html", m)
}
Example #2
0
func (h *Handler) handleUserUpdatePost(w http.ResponseWriter, r *http.Request) {
	segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
	username := segments[2]

	s, err := session.Parse(r)
	if err != nil {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	if s.User.Username != username {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	email := strings.TrimSpace(r.FormValue("email"))
	fullname := strings.TrimSpace(r.FormValue("fullname"))

	// Validate
	v := validation.New()
	if v.Required("email", email, "email is required") {
		v.Email("email", email, "invalid email")
	}

	if len(v.Errors) == 0 {
		err = s.User.Update(email, fullname)
		if err != nil {
			logger.Error(w, err)
			h.serveServerError(w, r)
			return
		}
		flashdata.Set(w, "Profile updated")
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s", config.Get("baseurl"),
			s.User.Username), http.StatusFound)
		return
	}

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

	h.Templates.ExecuteTemplate(w, "user_update.html", m)
}
Example #3
0
func (h *Handler) handleUserPasswordPost(w http.ResponseWriter, r *http.Request) {
	segments := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
	username := segments[2]

	s, err := session.Parse(r)
	if err != nil {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	if s.User.Username != username {
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), username), http.StatusFound)
		return
	}

	password := strings.TrimSpace(r.FormValue("password"))

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

	if len(v.Errors) == 0 {
		err := s.User.UpdatePassword(password)
		if err != nil {
			logger.Error(w, err)
			h.serveServerError(w, r)
			return
		}
		flashdata.Set(w, "Password updated")
		http.Redirect(w, r, fmt.Sprintf("%s/u/%s",
			config.Get("baseurl"), s.User.Username), http.StatusFound)
		return
	}

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

	h.Templates.ExecuteTemplate(w, "user_update_password.html", m)
}
Example #4
0
func (h *Handler) handleForgotPost(w http.ResponseWriter, r *http.Request) {
	username := strings.TrimSpace(r.FormValue("username"))

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

	if len(v.Errors) == 0 {
		u, err := user.Find(username)
		if err != nil {
			logger.Error(w, err)
			h.serveServerError(w, r)
			return
		}
		err = user.SendVerify(u.Id, u.Email, false)
		if err != nil {
			logger.Error(w, err)
			h.serveServerError(w, r)
			return
		}

		msg := `
            Password reset link sent
        `

		flashdata.Set(w, msg)

		url := fmt.Sprintf("%s/message", config.Get("baseurl"))
		http.Redirect(w, r, url, http.StatusFound)

		return
	}

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

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