Esempio n. 1
0
// LoginHandler calls upon the users repo to validate the login process
func (controller *UserController) LoginHandler(w http.ResponseWriter, r *http.Request) {
	body := context.Get(r, "body").(models.LoginForm)
	repo := repos.UserRepo{Coll: controller.DB.C(userContextName)}
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(200)

	result := models.BaseResponse{}
	var user models.User
	user, err := repo.Login(body)

	if err != nil {
		result.Success = false
		result.Message = err.Error()
	} else {
		if len(user.Username) == 0 && len(user.Email) == 0 && len(user.Password) == 0 {
			result.Success = false
			result.Message = "Username or password incorrect."
		} else {
			result.Data = user
			result.Success = true
			result.Message = ""
		}
	}

	json.NewEncoder(w).Encode(result)
}
Esempio n. 2
0
// CreateUserHandler calls upon the users repo to process the new user request
func (controller UserController) CreateUserHandler(w http.ResponseWriter, r *http.Request) {
	body := context.Get(r, "body").(*models.User)
	repo := repos.UserRepo{Coll: controller.DB.C(userContextName)}
	result := models.BaseResponse{}
	err := repo.Create(body)
	if err != nil {
		result.Success = false
		result.Message = err.Error()
	} else {
		result.Success = true
		result.Message = ""
		result.Data = body
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(201)
	json.NewEncoder(w).Encode(result)
}