Example #1
0
func UpdateUser(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	userId, _ := strconv.Atoi(vars["userId"])
	user := repository.GetUserById(userId)

	// oldPassword := r.FormValue("oldPassword")
	// newPassword := r.FormValue("newPassword")

	// if oldPassword != "" && newPassword != "" {
	// 	result := updatePassword(&user, oldPassword, newPassword)

	// 	if result {
	// 		w.Write([]byte(`{"result":true}`))
	// 	} else {
	// 		w.Write([]byte(`{"result":false}`))
	// 	}

	// 	return
	// }

	// User is exist
	displayName := r.FormValue("displayName")
	email := r.FormValue("email")
	if displayName != "" {
		user.DisplayName = displayName
	}
	user.Email.Scan(email)

	repository.UpdateUser(&user)
	json.NewEncoder(w).Encode(user)
}
Example #2
0
// Input: email string, password string
// Output:
// - Success: session string
// - Failed: {}
func Login(w http.ResponseWriter, r *http.Request) {
	var email sql.NullString
	email.Scan(r.FormValue("email"))
	password := r.FormValue("password")

	user := models.User{Email: email, Password: password}

	if !repository.Login(&user) {
		w.WriteHeader(http.StatusForbidden)
		w.Write([]byte(`{"error":"Mat khau hoac email khong dung"}`))
		return
	}

	createUserToken(&user)
	repository.UpdateUser(&user)

	json.NewEncoder(w).Encode(user)
}