{"size": 2262, "ext": "js", "max_stars_repo_path": "src/js/app/controllers/user/SignupCtrl.js", "max_stars_repo_name": "Connoropolous/communal-ui", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/js/app/controllers/user/SignupCtrl.js", "max_issues_repo_name": "Connoropolous/communal-ui", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/js/app/controllers/user/SignupCtrl.js", "max_forks_repo_name": "Connoropolous/communal-ui", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.9259259259, "max_line_length": 131, "alphanum_fraction": 0.6162687887}
var handleError = function (err, $scope) {
  var msg = err.data
  var email = $scope.user.email
  if (!msg) {
    $scope.signupError = "Couldn't sign up. Please try again."
    return
  }

  if (msg.match(/Key.*already exists/)) {
    var match = msg.match(/Key \((.*)\)=\((.*)\) already exists/)
    var key = match[1]
    var value = match[2]

    $scope.signupError = format('The %s "%s" is already in use. Try logging in instead?', key, value)
  } else {
    $scope.signupError = msg
  }
}

module.exports = function ($scope, User, Community, ThirdPartyAuth, Invitation, context, projectInvitation, $stateParams, $state) {
  'ngInject'
  $scope.user = {}

  if ($state.current.name === 'signupWithCode') {
    Community.get({id: $stateParams.slug}).$promise
    .then((community) => $scope.community = community)
    .catch(() => $scope.signupError = "We couldn't find a community with that name. Please check your link and try again.")
  }

  $scope.invitation = Invitation.storedData() || projectInvitation

  var finishSignup = provider => {
    if ($stateParams.next) {
      window.history.pushState(null, null, $stateParams.next)
    } else if (context === 'modal') {
      $scope.$close({action: 'finish'})
    } else {
      $scope.$state.go('appEntry')
    }
  }

  $scope.submit = function (form) {
    form.submitted = true
    $scope.signupError = null
    if (form.$invalid) return

    var params = _.merge({}, $scope.user, {login: true}, projectInvitation)

    User.signup(params).$promise.then(function (user) {
      finishSignup('password')
    }, function (err) {
      handleError(err, $scope)
    })
  }

  $scope.useThirdPartyAuth = function (service, form) {
    $scope.authStarted = true
    $scope.serviceUsed = service
    $scope.authDialog = ThirdPartyAuth.openPopup(service)
  }

  $scope.finishThirdPartyAuth = function (error) {
    $scope.$apply(function () {
      $scope.authDialog.close()
      if (error) {
        handleError({data: error}, $scope)
      } else {
        finishSignup($scope.serviceUsed)
      }
    })
  }

  $scope.go = function (state) {
    if (context === 'modal') {
      $scope.$close({action: 'go', state: state})
    } else {
      $scope.$state.go(state, {next: $stateParams.next})
    }
  }
}
