func LoginUser(w http.ResponseWriter, r *http.Request) {
	user, err := GetUser(r)
	if err != nil {
		log.Println("route LoginUser, user context:", err)
		render.R.JSON(w, 500, map[string]interface{}{"error": "Internal server error"})
		return
	}
	switch Root(r) {
	case "api":
		user, err := user.Login()
		if err != nil {
			log.Println("route LoginUser, user.Login:", err)
			if err.Error() == "wrong username or password" {
				render.R.JSON(w, 401, map[string]interface{}{"error": "Wrong username or password."})
				return
			}
			if err.Error() == "not found" {
				render.R.JSON(w, 404, map[string]interface{}{"error": "User with that email does not exist."})
				return
			}
			render.R.JSON(w, 500, map[string]interface{}{"error": "Internal server error"})
			return
		}
		SessionSetValue(w, r, "id", user.ID)
		user.Password = ""
		render.R.JSON(w, 200, user)
	case "user":
		user, err := user.Login()
		if err != nil {
			log.Println("route LoginUser, user.Login:", err)
			if err.Error() == "wrong username or password" {
				render.R.HTML(w, 401, "user/login", "Wrong username or password.")
				return
			}
			if err.Error() == "not found" {
				render.R.HTML(w, 404, "user/login", "User with that email does not exist.")
				return
			}
			render.R.HTML(w, 500, "user/login", "Internal server error. Please try again.")
			return
		}
		SessionSetValue(w, r, "id", user.ID)
		http.Redirect(w, r, "/user", 302)
	}
}
LoginUser is a route which compares plaintext password sent with POST request with
hash stored in database. On successful request returns session cookie named "user", which contains
user's ID encrypted, which is the primary key used in database table.
When called by API it responds with user struct.
On frontend call it redirects the client to "/user" page.
package routes
import (
	"errors"
	"log"
	"net/http"
	"strconv"
	. "github.com/toldjuuso/vertigo/databases/sqlx"
	"github.com/toldjuuso/vertigo/render"
	. "github.com/toldjuuso/vertigo/session"
	"github.com/gorilla/context"
	"github.com/husobee/vestigo"
	"github.com/pborman/uuid"
)
func GetUser(r *http.Request) (User, error) {
	rv, ok := context.GetOk(r, "user")
	if !ok {
		return User{}, errors.New("context not set")
	}
	return rv.(User), nil
}
func CreateUser(w http.ResponseWriter, r *http.Request) {
	user, err := GetUser(r)
	if err != nil {
		log.Println("route CreateUser, user context:", err)
		render.R.JSON(w, 500, map[string]interface{}{"error": "Internal server error"})
		return
	}
	if Settings.AllowRegistrations == false {
		log.Println("Denied a new registration.")
		switch Root(r) {
		case "api":
			render.R.JSON(w, 403, map[string]interface{}{"error": "New registrations are not allowed at this time."})
			return
		case "user":
			render.R.HTML(w, 403, "user/login", "New registrations are not allowed at this time.")
			return
		}
	}
	user, err = user.Insert()
	if err != nil {
		log.Println("route CreateUser, user.Insert:", err)
		if err.Error() == "user email exists" {
			render.R.JSON(w, 422, map[string]interface{}{"error": "Email already in use"})
			return
		}
		if err.Error() == "user location invalid" {
			render.R.JSON(w, 422, map[string]interface{}{"error": "Location invalid. Please use IANA timezone database compatible locations."})
			return
		}
		render.R.JSON(w, 500, map[string]interface{}{"error": "Internal server error"})
		return
	}
	user, err = user.Login()
	if err != nil {
		log.Println("route CreateUser, user.Login:", err)
		render.R.JSON(w, 500, map[string]interface{}{"error": "Internal server error"})
		return
	}
	SessionSetValue(w, r, "id", user.ID)
	switch Root(r) {
	case "api":
		render.R.JSON(w, 200, user)
	case "user":
		http.Redirect(w, r, "/user", 302)
	}
}
func ReadUser(w http.ResponseWriter, r *http.Request) {
	var user User
	switch Root(r) {
	case "api":
		id, err := strconv.ParseInt(vestigo.Param(r, "id"), 10, 64)
		if err != nil {
			log.Println("route ReadUser, strconv.Atoi:", err)
			render.R.JSON(w, 400, map[string]interface{}{"error": "The user ID could not be parsed from the request URL."})
			return
		}
		user.ID = id
		user, err := user.Get()
		if err != nil {
			log.Println("route ReadUser, user.Get:", err)
			if err.Error() == "not found" {
				render.R.JSON(w, 404, map[string]interface{}{"error": "Not found"})
				return
			}
			render.R.JSON(w, 500, map[string]interface{}{"error": "Internal server error"})
			return
		}
		for i, post := range user.Posts {
			if !post.Published {
				user.Posts = append(user.Posts[:i], user.Posts[i+1:]...)
			}
		}
		if len(user.Posts) == 0 {
			p := make([]Post, 0)
			user.Posts = p
		}
		render.R.JSON(w, 200, user)
	case "user":
		id, ok := SessionGetValue(r, "id")
		if !ok {
			log.Println("route ReadUser, SessionGetValue:", ok)
			SessionDelete(w, r, "id")
			render.R.HTML(w, 500, "error", "Session could not be fetched. Please log in again.")
			return
		}
		user.ID = id
		user, err := user.Get()
		if err != nil {
			log.Println("route ReadUser, user.Get:", err)
			SessionDelete(w, r, "id")
			render.R.HTML(w, 500, "error", err)
			return
		}
		render.R.HTML(w, 200, "user/index", user)
	}
}
func ReadUsers(w http.ResponseWriter, r *http.Request) {
	var user User
	users, err := user.GetAll()
	if err != nil {
		log.Println("route ReadUsers, user.GetAll:", err)
		render.R.JSON(w, 500, err)
		return
	}
	if len(users) == 0 {
		users = make([]User, 0)
		render.R.JSON(w, 200, users)
		return
	}
	for i, user := range users {
		published := make([]Post, 0)
		for _, post := range user.Posts {
			if post.Published {
				published = append(published, post)
			}
		}
		users[i].Posts = published
	}
	render.R.JSON(w, 200, users)
}