Пример #1
0
// ValidateUsername checks to see if the username is in use
func (u Controller) ValidateUsername(e echo.Context) error {
	name := e.Param("name")

	count, err := models.Users(u.DB, qm.Where("username=?", name)).Count()
	if err != nil {
		log.Error("failed to get user from db", "err", err)
		return e.NoContent(http.StatusInternalServerError)
	}

	if count > 0 {
		return e.NoContent(http.StatusNotAcceptable)
	}

	return e.NoContent(http.StatusOK)
}
Пример #2
0
// Create logs a user in
func (s Controller) Create(e echo.Context) error {
	session := &userSession{}

	if err := e.Bind(session); err != nil {
		return err
	}

	user, err := models.Users(s.DB, qm.Select("hash"), qm.Where("username=$1", session.Username)).One()
	if err != nil {
		s.Error("failed to fetch user", "err", err)
		return e.NoContent(http.StatusInternalServerError)
	}

	if err := bcrypt.CompareHashAndPassword([]byte(user.Hash), []byte(session.Password)); err != nil {
		return e.JSONBlob(http.StatusUnauthorized, errInvalidCredentials)
	}

	return e.NoContent(http.StatusOK)
}