예제 #1
0
func LoginUser(res http.ResponseWriter, req *http.Request, dbmap *gorp.DbMap) {
	// get email and password
	item := new(models.User)
	// vars := mux.Vars(req)
	// email := vars["email"]
	// password := vars["password"]

	var userWrapper UserWrapper
	var err error

	err = json.NewDecoder(req.Body).Decode(&userWrapper)
	if err != nil {
		Respond(nil, err, res)
		return
	}

	// get the user
	err = dbmap.SelectOne(&item, "select * from users where email=$1", userWrapper.Email)
	if err != nil {
		Respond(nil, err, res)
		return
	}

	// check the password
	err = item.ComparePassword(userWrapper.Password)
	if err != nil {
		Respond(nil, err, res)
		return
	}

	Respond(item, nil, res)
}
예제 #2
0
// For another model without hooks (i.e. password->hash),
// would likely want to use parseFormValues.
func PostUser(res http.ResponseWriter, req *http.Request, dbmap *gorp.DbMap) {
	item := new(models.User)

	var userWrapper UserWrapper
	var err error

	err = json.NewDecoder(req.Body).Decode(&userWrapper)
	if err != nil {
		Respond(nil, err, res)
		return
	}

	email := userWrapper.Email
	password := userWrapper.Password

	// set new object for insert
	item.Email = email
	item.SetPassword(password)

	// save user
	err = dbmap.Insert(item)
	Respond(item, err, res)
}