Esempio n. 1
0
File: users.go Progetto: mewben/onix
// Password Change password of logged user
func (*UsersController) Password(c echo.Context) error {
	type pl struct {
		Oldp string `json:"old_password"`
		Newp string `json:"new_password"`
		Conp string `json:"confirm_password"`
	}

	var payload pl
	var model models.User

	if err := c.Bind(&payload); err != nil {
		return c.JSON(400, utils.ErrMarshal(err.Error()))
	}

	if payload.Newp != payload.Conp {
		return c.JSON(400, utils.ErrMarshal("Password did not match."))
	}

	userID := int(c.Get("userID").(float64))
	ret, err := model.ChangePassword(userID, payload.Oldp, payload.Newp)
	if err != nil {
		return c.JSON(400, utils.ErrMarshal(err.Error()))
	}

	return c.JSON(200, ret)
}
Esempio n. 2
0
File: users.go Progetto: mewben/onix
// Login POST /auth/login
func (*UsersController) Login(c echo.Context) error {
	var payload models.LoginPayload
	var user models.User

	an, _, err := net.SplitHostPort(c.Request().RemoteAddress())
	if err != nil {
		return c.JSON(400, utils.ErrMarshal(err.Error()))
	}

	if err = c.Bind(&payload); err != nil {
		return c.JSON(400, utils.ErrMarshal(err.Error()))
	}

	ret, err := user.Login(payload)
	if err != nil {
		msg := err.Error()
		// login failed
		err = utils.ST.Infraction(an, "Login")
		if err != nil {
			msg = msg + " " + err.Error()
			return c.JSON(400, utils.ErrMarshal(msg))
		}

		// auth fails, increment infraction
		_, err = utils.ST.Strikes(an, "Login")

		return c.JSON(400, utils.ErrMarshal(msg))
	}

	return c.JSON(200, ret)
}