// BeforeCreate is here to check inputs and generating an encrypted password
BeforeCreate is here to check inputs and generating an encrypted password
// BeforeCreate is here to check inputs and generating an encrypted password
func (user *User) BeforeCreate() error {
	user.ID = bson.NewObjectId().Hex()
	user.Key = helpers.RandomString(40)
	user.Email = strings.ToLower(user.Email)
	user.LastModification = time.Now().Unix()
	user.Status = "created"
	hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost)
	if err != nil {
		return helpers.NewError(http.StatusInternalServerError, "encryption_failed", "Failed to generate the crypted password", err)
	}
	user.Password = string(hashedPassword)
	_, err = govalidator.ValidateStruct(user)
	if err != nil {
		return helpers.NewError(http.StatusBadRequest, "input_not_valid", err.Error(), err)
	}
	return nil
}
func ( user * User ) BeforeCreate ( ) error { user . ID = bson . NewObjectId ( ) . Hex ( ) user . Key = helpers . RandomString ( 40 ) user . Email = strings . ToLower ( user . Email ) user . LastModification = time . Now ( ) . Unix ( ) user . Status = " " hashedPassword , err := bcrypt . GenerateFromPassword ( [ ] byte ( user . Password ) , bcrypt . DefaultCost ) if err != nil { return helpers . NewError ( http . StatusInternalServerError , " " , " " , err ) } user . Password = string ( hashedPassword ) _ , err = govalidator . ValidateStruct ( user ) if err != nil { return helpers . NewError ( http . StatusBadRequest , " " , err . Error ( ) , err ) } return nil }
BeforeCreate is here to check inputs and generating an encrypted password
BeforeCreate is here to check inputs and generating an encrypted password