// Login user via email func (s *Service) Login(email, pass string) (map[string]interface{}, error) { payload := make(map[string]interface{}) user, err := s.FindOne(Option{ Filter: Object{ Email: email, Password: x.Hash(pass), }, }) if err != nil { return payload, errors.New("invalid email or password") } // payload population payload["id"] = user.ID payload["type"] = user.Type token, err := x.CreateToken(x.AuthUser{ ID: user.ID, Type: payload["type"].(string), }) payload["token"] = token return payload, err }
// Create user func (s *Service) Create(p Object) (Object, error) { // check email existence user, _ := s.FindOne(Option{ Filter: Object{ Email: p.Email, }, }) if user.ID != "" { return Object{}, errors.New("email already exists") } // pasword hashing p.Password = module.Hash(p.Password) return resource.Create(p) }