示例#1
0
func (accounts *accountsPq) Create(credentials *models.Credentials) (*models.Account, error) {
	account, err := credentials.Account()
	if err != nil {
		return nil, err
	}
	if err = accounts.Storage.DbMap.Insert(account); err != nil {
		return nil, err
	}
	return account, nil
}
示例#2
0
func (c Auth) Signup() revel.Result {
	var credentials models.Credentials

	json_data, err := ioutil.ReadAll(c.Request.Body)
	if err != nil {
		c.Response.Status = http.StatusUnauthorized
		return c.RenderJson(AuthError{
			Error:        1,
			Flash:        "Error while reading request body.",
			ErrorDetails: err.Error(),
		})
	}

	if err := json.Unmarshal([]byte(json_data), &credentials); err != nil {
		c.Response.Status = http.StatusUnauthorized
		return c.RenderJson(AuthError{
			Error:        1,
			Flash:        "Invalid JSON format in the request body.",
			ErrorDetails: err.Error(),
		})
	}

	credentials.Validate(c.Validation)
	if c.Validation.HasErrors() {
		c.Response.Status = http.StatusUnauthorized
		return c.RenderJson(AuthError{
			Error:        1,
			Flash:        "Invalid credentials.",
			ErrorDetails: err.Error(),
		})
	}

	revel.INFO.Println(credentials)
	_, err = Accounts.Create(&credentials)

	if err != nil {
		c.Response.Status = http.StatusUnauthorized
		return c.RenderJson(AuthError{
			Error:        1,
			Flash:        "API error while creating new account. " + err.Error(),
			ErrorDetails: err.Error(),
		})
	}

	// TODO:([email protected]): send activation email
	return c.RenderJson(SignUpResponse{
		Error: 0,
		Flash: fmt.Sprintf("New account was successfully created. Activation email sent to %s.", credentials.Email),
	})
}