TwoToc code
[YouAndWeb_TwoToc] / client / app / account / signup / signup.controller.js
1 'use strict';
2
3 angular.module('dashboardApp')
4   .controller('SignupCtrl', function($scope, Auth, $state, $window, datepickerPopupConfig) {
5     $scope.user = {};
6     $scope.errors = {};
7
8     $scope.$watch('user', function() {
9       $scope.user.name = $scope.user.firstName + ' ' + $scope.user.lastName;
10     }, true);
11
12     $scope.datepickers = {
13       birthDate: false
14     };
15
16     $scope.open = function($event, which, whichnot) {
17       $event.preventDefault();
18       $event.stopPropagation();
19
20       $scope.datepickers[which]= true;
21       $scope.datepickers[whichnot]= false;
22     };
23
24     $scope.dateOptions = {
25       'year-format': 'yy',
26       'starting-day': 1,
27       'show-weeks': false
28     };
29
30     datepickerPopupConfig.showButtonBar = false;
31     datepickerPopupConfig.appendToBody = false;
32
33     $scope.minDate = new Date();
34     $scope.minDateFine = new Date();
35
36     $scope.register = function(form) {
37       $scope.submitted = true;
38
39       if (form.$valid) {
40         Auth.createUser({
41           name: $scope.user.name,
42           firstNname: $scope.user.firstName,
43           lastName: $scope.user.lastName,
44           birthDate: $scope.user.birthDate,
45           email: $scope.user.email,
46           password: $scope.user.password
47         })
48         .then(function() {
49           // Account created, redirect to home
50           $state.go('main');
51         })
52         .catch(function(err) {
53           err = err.data;
54           $scope.errors = {};
55
56           // Update validity of form fields that match the mongoose errors
57           angular.forEach(err.errors, function(error, field) {
58             form[field].$setValidity('mongoose', false);
59             $scope.errors[field] = error.message;
60           });
61         });
62       }
63     };
64
65     $scope.loginOauth = function(provider) {
66       $window.location.href = '/auth/' + provider;
67     };
68   });