コード例 #1
0
ファイル: auth.go プロジェクト: nancyandrews/MobileMainStreet
func ChangePasswordHandler() httperr.Handler {
	return func(w http.ResponseWriter, r *http.Request) error {

		type changePasswordReq struct {
			OldPassword string
			NewPassword string
		}

		req := &changePasswordReq{}
		if err := json.NewDecoder(r.Body).Decode(req); err != nil {
			return httperr.New(http.StatusBadRequest, err.Error(), err)
		}

		dbmap, err := getDB()
		defer dbmap.Db.Close()
		if err != nil {
			return err
		}

		email := r.URL.Query().Get(authEmailKey)
		member, err := model.FindMember(dbmap, email)
		if err != nil {
			return err
		}

		if !member.HasPassword(req.OldPassword) {
			err := errors.New("password invalid")
			return httperr.New(http.StatusBadRequest, err.Error(), err)
		}

		pword, err := model.NewPassword(req.NewPassword)
		if err != nil {
			return httperr.New(http.StatusBadRequest, "password must be between 7 and 32 characters", err)
		}
		member.SetPassword(pword)
		if _, err := dbmap.Update(member); err != nil {
			return err
		}
		return nil
	}
}
コード例 #2
0
ファイル: auth.go プロジェクト: nancyandrews/MobileMainStreet
func SignupHandler() httperr.Handler {
	return func(w http.ResponseWriter, r *http.Request) error {
		dbmap, err := getDB()
		defer dbmap.Db.Close()
		if err != nil {
			return err
		}

		// check if the community already exits, if so prevent signup
		coms := []*model.Community{}
		dbmap.Select(&coms, "select * from communities")
		if len(coms) > 0 {
			err := errors.New("community already created")
			return httperr.New(http.StatusBadRequest, err.Error(), err)
		}

		type signupReq struct {
			Email              string
			Password           string
			Name               string
			CommunityName      string
			RegistrationPolicy string
			Location           []float64
			Description        string
		}

		req := &signupReq{}
		if err := json.NewDecoder(r.Body).Decode(req); err != nil {
			return httperr.New(http.StatusBadRequest, err.Error(), err)
		}

		trans, err := dbmap.Begin()
		if err != nil {
			return err
		}

		member := &model.Member{
			Email:     req.Email,
			Organizer: true,
			Name:      req.Name,
		}
		pword, err := model.NewPassword(req.Password)
		if err != nil {
			return httperr.New(http.StatusBadRequest, "password must be between 7 and 32 characters", err)
		}
		member.SetPassword(pword)

		if err := trans.Insert(member); err != nil {
			return err
		}

		com := &model.Community{
			Name:               req.CommunityName,
			Location:           req.Location,
			Description:        req.Description,
			RegistrationPolicy: req.RegistrationPolicy,
		}
		if err := trans.Insert(com); err != nil {
			return err
		}

		categories := []*model.Category{
			{Name: "Play Local"},
			{Name: "Be Local"},
			{Name: "Eat Local"},
			{Name: "Shop Local"},
		}
		for _, cat := range categories {
			if err := trans.Insert(cat); err != nil {
				return err
			}
		}
		if err := trans.Commit(); err != nil {
			return err
		}
		return json.NewEncoder(w).Encode(member)
	}
}