Exemple #1
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)
}
Exemple #2
0
func signinWithGoogle(reqBody userReqPostBody) (*model.UserToken, *ae.Error) {
	person, token, err := model.GetGooglUserDetails(reqBody.GoogleKey)
	if err != nil {
		return nil, ae.DB("", err)
	}
	user := model.User{Email: person.Emails[0].Value}
	if err = user.GetUserByEmail(); err != nil {
		if err == sql.ErrNoRows {
			user.Verified = true
			user.ProfilePicURL = person.Image.Url
			if err1 := user.Save(); err1 != nil {
				return nil, ae.DB("", err1)
			}
		} else {
			return nil, ae.DB("", err)
		}
	}
	gToken := model.GoogleToken{UserID: user.UserID}
	gToken.Token = *token
	if err := gToken.Replace(); err != nil {
		return nil, ae.DB("", err)
	}
	userToken := model.UserToken{}
	userToken.UserID = user.UserID
	err = userToken.Add()
	if err != nil {
		return nil, ae.DB("", err)
	}
	return &userToken, nil
}
Exemple #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)
}
Exemple #4
0
func GetUser(w http.ResponseWriter, r *http.Request) {
	userID := context.Get(r, "user_id").(uint64)
	var err error

	urlParams := context.Get(r, "params").(httprouter.Params)
	id := urlParams.ByName("id")
	if len(id) != 0 {
		userID, err = strconv.ParseUint(id, 10, 64)
		if err != nil {
			reply.Err(w, ae.InvalidInput("id is not a number", "id"))
		}
	}

	user := model.User{}
	user.UserID = userID
	err = user.Get()
	if err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}

	reply.OK(w, user)
}
Exemple #5
0
func DeleteUser(w http.ResponseWriter, r *http.Request) {
	user := model.User{}
	user.UserID = context.Get(r, "user_id").(uint64)
	err := user.Get()
	if err != nil {
		reply.Err(w, ae.DB("", err))
	}
	err = user.Delete()
	if err != nil {
		reply.Err(w, ae.DB("", err))
	}
	reply.OK(w, user)
}
Exemple #6
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
}
Exemple #7
0
func UpdateUser(w http.ResponseWriter, r *http.Request) {
	userID := context.Get(r, "user_id").(uint64)

	reqBody := userReqPutBody{}
	appErr := decode(r, &reqBody)
	if appErr != nil {
		reply.Err(w, appErr)
		return
	}

	user := model.User{UserID: userID}
	if err := user.Get(); err != nil {
		if err == sql.ErrNoRows {
			reply.Err(w, ae.ResourceNotFound(""))
			return
		}
		reply.Err(w, ae.DB("", err))
		return
	}

	user.FirstName = reqBody.FirstName
	user.LastName = reqBody.LastName
	user.ProfilePicURL = reqBody.ProfilePicURL
	user.MetricSystem = reqBody.MetricSystem

	if err := user.Save(); err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}

	if err := user.Get(); err != nil {
		reply.Err(w, ae.DB("", err))
		return
	}

	reply.OK(w, user)
	return
}