Exemplo n.º 1
0
func (u *User) CreateAdmin(r *http.Request) (bool, error) {

	//populate user data
	userModel := model.User{}
	userModel.Email = "*****@*****.**"
	plainPassword := "******"

	//encrypt password
	password, err := bcrypt.GenerateFromPassword([]byte(plainPassword), 10)

	if err != nil {
		panic(err)
	}

	//populate model
	userModel.Password = string(password)
	userModel.Role = 2

	//create new user
	err = userModel.CreateUser(r)

	if err != nil {
		return false, err
	}

	return true, nil
}
Exemplo n.º 2
0
func (u *User) CreateUser(w http.ResponseWriter, r *http.Request) (bool, error) {
	type Message struct {
		Email    string `json:"email"`
		Password string `json:"password"`
	}

	var m Message
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&m)

	if err != nil {
		//handle err
	}

	//populate user data
	userModel := model.User{}
	userModel.Email = m.Email

	//encrypt password
	password, err := bcrypt.GenerateFromPassword([]byte(m.Password), 10)

	if err != nil {
		panic(err)
	}

	//populate model
	userModel.Password = string(password)
	userModel.Role = 1

	//make sure email doesn't exist
	emailStatus, err := u.CheckEmail(w, r)

	if emailStatus == true || err != nil {
		return false, nil
	}

	//create new user
	err = userModel.CreateUser(r)

	if err != nil {
		return false, err
	}

	return true, nil
}