Exemplo n.º 1
0
func (r *Begin) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form struct {
		Email string `json:"email"`
	}
	err := json.NewDecoder(req.Body).Decode(&form)
	if err != nil {
		return ctx.BadRequest(rw, c.T("reset.begin.could_not_query"))
	}

	// validate email address
	if ok := regexp.MustCompile(cfg.Email.Regex).MatchString(form.Email); !ok {
		return ctx.BadRequest(rw, c.T("reset.begin.invalid_email_address"))
	}

	// get user from database
	var u *user.Model
	u, err = user.GetByEmail(db, form.Email)
	if err != nil {
		return ctx.BadRequest(rw, c.T("reset.begin.user_not_found"))
	}

	go sendEmail(c, u)

	return ctx.OK(rw, c.T("reset.begin.email_sent"))
}
Exemplo n.º 2
0
func (r *SignUp) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form struct {
		FirstName     string `json:"firstName"`
		LastName      string `json:"lastName"`
		Email         string `json:"email"`
		Password      string `json:"password"`
		PasswordAgain string `json:"passwordAgain"`
	}
	err := json.NewDecoder(req.Body).Decode(&form)
	if err != nil {
		log.Errorf("Could not parse request data: %s", err)
		return ctx.BadRequest(rw, c.T("user.signup.could_not_parse_request_data"))
	}

	// check whether the email address is already taken
	_, err = user.GetByEmail(db, form.Email)
	if err == nil {
		return ctx.BadRequest(rw, c.T("user.signup.email_taken"))
	} else if err != pg.ERecordNotFound {
		log.Errorf("Could not query user: %s", err)
		return ctx.InternalServerError(rw, c.T("user.signup.could_not_query_user"))
	}

	// password validation
	if form.Password != form.PasswordAgain {
		return ctx.BadRequest(rw, c.T("user.signup.passwords_mismatch"))
	}

	// create new user
	u, err := user.Create(
		db,
		form.Email,
		form.Password,
		&user.UserJsonData{
			FirstName: form.FirstName,
			LastName:  form.LastName,
		},
	)
	if err != nil {
		return ctx.InternalServerError(rw, c.T("user.signup.could_not_create_user"))
	}

	// return created user data
	return ctx.Created(rw, u)
}
Exemplo n.º 3
0
func (r *SignIn) POST(c *ctx.Context, rw http.ResponseWriter, req *http.Request) error {
	db := c.Vars["db"].(*pg.Session)

	// decode request data
	var form struct {
		Email    string `json:"email"`
		Password string `json:"password"`
	}
	err := json.NewDecoder(req.Body).Decode(&form)
	if err != nil {
		return ctx.BadRequest(rw, c.T("user.signin.could_not_query"))
	}

	// validate email address
	if ok := regexp.MustCompile(cfg.Email.Regex).MatchString(form.Email); !ok {
		return ctx.BadRequest(rw, c.T("user.signin.invalid_email_address"))
	}

	// validate password length
	if len(form.Password) == 0 {
		return ctx.BadRequest(rw, c.T("user.signin.password_cannot_be_empty"))
	}

	// check user in database
	var u *user.Model
	u, err = user.GetByEmail(db, form.Email)
	if err != nil {
		return ctx.BadRequest(rw, c.T("user.signin.invalid_email_or_password"))
	}

	// check user password
	if !u.Password.Valid(form.Password) {
		return ctx.BadRequest(rw, c.T("user.signin.invalid_email_or_password"))
	}

	// generate new token
	return token.Response(c, rw, token.New(u))
}