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)
}
Example #3
0
func EmailExists(w http.ResponseWriter, r *http.Request) {
	urlParams := context.Get(r, "params").(httprouter.Params)
	email := urlParams.ByName("email")
	if len(email) == 0 {
		reply.Err(w, ae.Required("", "email"))
		return
	}
	user := model.User{}
	user.Email = email
	exists, err := user.EmailExists()
	if err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}

	result := make(map[string]interface{})
	result["exists"] = exists
	reply.OK(w, result)
}