Exemplo n.º 1
0
// PostUser -
func PostUser(w http.ResponseWriter, r *http.Request) {
	var user models.User

	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&user)

	if err != nil {
		BadRequest(err, w, r)
		return
	}

	if err := user.Valid(); err != nil {
		UnprocessableEntity(err, w, r)
		return
	}

	if err := user.Save(); err != nil {
		InternalServerError(err, w, r)
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	w.WriteHeader(http.StatusCreated)

	if err := json.NewEncoder(w).Encode(user); err != nil {
		InternalServerError(err, w, r)
		return
	}
}
Exemplo n.º 2
0
// Authenticate - autenticate a user and return true or return false and send an unauthorized status
func Authenticate(currentUser *models.User, w http.ResponseWriter, r *http.Request) bool {
	token := core.ExtractToken(r.Header.Get("Authorization"))

	user, err := models.GetUserByToken(token)

	if err != nil {
		Unauthorized(w, r)
		return false
	}

	currentUser.ID = user.ID
	currentUser.Email = user.Email
	currentUser.Password = user.Password

	if currentUser.ID == 0 {
		Unauthorized(w, r)
		return false
	}

	return true
}