Ejemplo n.º 1
0
func userPost(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	user := &UserDao.UserDTO{}

	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(user)
	if err != nil {
		ctx.Errorf(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if err := UserDao.CreateUser(ctx, user); err != nil {
		if err == UserDao.EmailAlreadyExistsError {
			http.Error(w, err.Error(), http.StatusConflict)
			ctx.Infof(err.Error())
			return
		}
		ctx.Errorf(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if _, err := AppEngineHelper.WriteJSON(w, user); err != nil {
		ctx.Errorf(err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Ejemplo n.º 2
0
func getUserTransactionsHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	user, err := getUserFromSession(ctx, r)
	if err != nil {
		// User is not authorised sending back an empty response
		return
	}

	transactions, err := TransactionDao.GetTransactionsByUser(ctx, user.GetDataStoreKey(ctx))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	txnClientDtoList := make([]*TransactionMsgClientDTO, len(transactions))

	for i, txn := range transactions {
		txnClientDtoList[i] = newTransactionMsgClientDTO(txn)
	}

	if _, err := AppEngineHelper.WriteJSON(w, txnClientDtoList); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Ejemplo n.º 3
0
func getUserFromSessionHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	user, err := getUserFromSession(ctx, r)
	if err != nil {
		http.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	if _, err := AppEngineHelper.WriteJSON(w, user); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
Ejemplo n.º 4
0
func doLogin(w http.ResponseWriter, r *http.Request) {

	var (
		user *UserDao.UserDTO
		uuid string
		err  error
	)

	ctx := appengine.NewContext(r)

	loginRequestUser := new(UserLogin)

	decoder := json.NewDecoder(r.Body)
	if err = decoder.Decode(loginRequestUser); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if !loginRequestUser.hasValues() {
		http.Error(w, invalidLoginError.Error(), http.StatusBadRequest)
		return
	}

	user, err = UserDao.GetUserByEmail(ctx, loginRequestUser.Email)
	if user == nil || err == UserDao.UserNotFoundError {
		ctx.Errorf("Failed to login, %s does not exist in DB", loginRequestUser.Email)
		http.Error(w, "Invalid credentials", http.StatusUnauthorized)
		return
	}
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	if err := user.VerifyPassword(loginRequestUser.Password); err != nil {
		ctx.Errorf("Failed to login, %s Invalid password", loginRequestUser.Email)
		ctx.Errorf(err.Error())
		http.Error(w, "Invalid credentials", http.StatusUnauthorized)
		return
	}

	uuid, err = generateUUID()
	if err != nil {
		http.Error(w, "Error Generating UUID", http.StatusInternalServerError)
		return
	}

	err = setSessionCookie(w, uuid)
	if err != nil {
		http.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	err = UserDao.SetSessionUUID(ctx, user, uuid)
	if err != nil {
		http.Error(w, err.Error(), http.StatusUnauthorized)
		return
	}

	if _, err := AppEngineHelper.WriteJSON(w, user); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

}