Example #1
0
func signinWithUserName(reqBody userReqPostBody) (*model.UserToken, *ae.Error) {
	user := model.User{}
	user.Email = reqBody.Email
	err := user.GetUserByEmail()
	if err == sql.ErrNoRows {
		return nil, ae.InvalidUserNamePassword("")
	}
	if err != nil {
		return nil, ae.DB("", err)
	}
	salt, err := user.GetPasswordSalt()
	if err != nil {
		return nil, ae.DB("", err)
	}
	user.Password = reqBody.Password
	user.HashPassword(salt)
	exists, err := user.IsValidUser()
	if err != nil || !exists {
		return nil, ae.InvalidUserNamePassword("")
	}
	userToken := model.UserToken{}
	userToken.UserID = user.UserID
	err = userToken.Add()
	if err != nil {
		return nil, ae.DB("", err)
	}
	return &userToken, nil
}
Example #2
0
func SaveUser(w http.ResponseWriter, r *http.Request) {
	reqBody := userReqPostBody{}
	appErr := decode(r, &reqBody)
	if appErr != nil {
		reply.Err(w, appErr)
		return
	}
	user := model.User{}
	user.Email = reqBody.Email
	user.Password = reqBody.Password
	exists, err := user.EmailExists()
	if exists {
		reply.Err(w, ae.UserNameExists("", "email"))
		return
	} else if err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}
	user.HashPassword("")
	err = user.Save()
	if err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}

	reply.OK(w, user)
}