TwoToc code
[YouAndWeb_TwoToc] / client / app / account / signup / signup.controller.js
diff --git a/client/app/account/signup/signup.controller.js b/client/app/account/signup/signup.controller.js
new file mode 100755 (executable)
index 0000000..6bf8864
--- /dev/null
@@ -0,0 +1,68 @@
+'use strict';
+
+angular.module('dashboardApp')
+  .controller('SignupCtrl', function($scope, Auth, $state, $window, datepickerPopupConfig) {
+    $scope.user = {};
+    $scope.errors = {};
+
+    $scope.$watch('user', function() {
+      $scope.user.name = $scope.user.firstName + ' ' + $scope.user.lastName;
+    }, true);
+
+    $scope.datepickers = {
+      birthDate: false
+    };
+
+    $scope.open = function($event, which, whichnot) {
+      $event.preventDefault();
+      $event.stopPropagation();
+
+      $scope.datepickers[which]= true;
+      $scope.datepickers[whichnot]= false;
+    };
+
+    $scope.dateOptions = {
+      'year-format': 'yy',
+      'starting-day': 1,
+      'show-weeks': false
+    };
+
+    datepickerPopupConfig.showButtonBar = false;
+    datepickerPopupConfig.appendToBody = false;
+
+    $scope.minDate = new Date();
+    $scope.minDateFine = new Date();
+
+    $scope.register = function(form) {
+      $scope.submitted = true;
+
+      if (form.$valid) {
+        Auth.createUser({
+          name: $scope.user.name,
+          firstNname: $scope.user.firstName,
+          lastName: $scope.user.lastName,
+          birthDate: $scope.user.birthDate,
+          email: $scope.user.email,
+          password: $scope.user.password
+        })
+        .then(function() {
+          // Account created, redirect to home
+          $state.go('main');
+        })
+        .catch(function(err) {
+          err = err.data;
+          $scope.errors = {};
+
+          // Update validity of form fields that match the mongoose errors
+          angular.forEach(err.errors, function(error, field) {
+            form[field].$setValidity('mongoose', false);
+            $scope.errors[field] = error.message;
+          });
+        });
+      }
+    };
+
+    $scope.loginOauth = function(provider) {
+      $window.location.href = '/auth/' + provider;
+    };
+  });