Example #1
0
func Create(db *pg.Session, email, pw string, userJsonData *UserJsonData) (*Model, error) {
	// encode password
	var saltedPassword password.Salted
	err := saltedPassword.Encode(pw)
	if err != nil {
		return nil, errors.New("Could not encode password")
	}

	// create new user structure
	user := &Model{
		Id:       null.NewInt(0, false),
		State:    UserStateActive,
		Email:    email,
		Password: saltedPassword,
	}

	// fill user structure with additional data
	err = user.JsonData.Encode(userJsonData)
	if err != nil {
		return nil, fmt.Errorf("Failed to marshal json data: %s", err)
	}

	// create new user in database
	err = db.Create(user)
	if err != nil {
		return nil, fmt.Errorf("Could not persist user: %s", err)
	}

	return user, nil
}
Example #2
0
func createToken(db *pg.Session, userId int64) (*Model, error) {
	// generate key
	key, err := random.New(32)
	if err != nil {
		return nil, err
	}

	// create new reset token structure
	resetToken := &Model{
		Id:         null.NewInt(0, false),
		State:      ResetTokenActive,
		Key:        key,
		Expiration: time.Now().Add(time.Minute * 10),
		UserId:     userId,
	}

	// create new user in database
	err = db.Create(resetToken)
	if err != nil {
		return nil, fmt.Errorf("Could not persist reset token: %s", err)
	}

	return resetToken, nil
}