Exemplo n.º 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)
}
Exemplo n.º 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)
}
Exemplo n.º 3
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)
}
Exemplo n.º 4
0
func (h *Handler) handlePurge(w http.ResponseWriter, r *http.Request) {
	err := h.loadTemplates()
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	w.Header().Add("Content-Type", "text/plain")
	w.Write([]byte("Templates Reloaded"))
}
Exemplo n.º 5
0
func (h *Handler) handleUser(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 {
		logger.Error(w, err)
	} else {
		err = s.Save(w, true)
		if err != nil {
			logger.Error(w, err)
		}
	}

	var owner bool
	var u *user.User
	if s != nil && s.User.Username == username {
		owner = true
		u = s.User
	} else {
		owner = false
		u, err = user.LoadByUsername(username)
		if err != nil {
			logger.Error(w, err)
			h.serveNotFound(w, r)
			return
		}
	}

	msg, _ := flashdata.Get(w, r)

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
		"session": s,
		"message": msg,
		"user":    u,
		"owner":   owner,
	}

	h.Templates.ExecuteTemplate(w, "user.html", m)
}
Exemplo n.º 6
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)
}
Exemplo n.º 7
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)
}
Exemplo n.º 8
0
func (h *Handler) handleHome(w http.ResponseWriter, r *http.Request) {
	s, err := session.Parse(r)
	if err != nil {
		logger.Error(w, err)
	} else {
		err = s.Save(w, true)
		if err != nil {
			logger.Error(w, err)
		}
	}

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

	m := map[string]interface{}{
		"baseurl": config.Get("baseurl"),
	}

	h.Templates.ExecuteTemplate(w, "home.html", m)
}
Exemplo n.º 9
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)
}