From d6b64187abc1a37753b30d832a39e390670efe4c Mon Sep 17 00:00:00 2001 From: David Baldwynn Date: Tue, 30 Jun 2015 13:57:20 -0700 Subject: [PATCH] got new auth to start working --- config/env/all.js | 3 +- public/application.js | 4 +- .../modules/core/config/core.client.routes.js | 2 +- .../controllers/header.client.controller.js | 66 +++++------ .../controllers/home.client.controller.js | 8 +- .../controllers/index.client.controller.js} | 38 +++--- .../create-form.client.controller.js | 4 +- .../submit-form.client.controller.js | 4 +- .../authentication.client.controller.js | 110 +++++++++--------- .../controllers/password.client.controller.js | 16 +-- .../controllers/settings.client.controller.js | 6 +- public/modules/users/services/auth.js | 27 +++-- public/modules/users/services/user.js | 7 +- 13 files changed, 150 insertions(+), 145 deletions(-) rename public/modules/{index.js => core/controllers/index.client.controller.js} (56%) diff --git a/config/env/all.js b/config/env/all.js index bd3871f3..c4a5d4cf 100755 --- a/config/env/all.js +++ b/config/env/all.js @@ -65,7 +65,8 @@ module.exports = { 'public/lib/angular-ui-router/release/angular-ui-router.js', 'public/lib/angular-ui-utils/ui-utils.js', 'public/lib/angular-bootstrap/ui-bootstrap-tpls.js', - 'public/lib/ng-file-upload/ng-file-upload-all.js' + 'public/lib/ng-file-upload/ng-file-upload-all.js', + 'public/lib/angular-cookies/angular-cookies.js', ] }, css: [ diff --git a/public/application.js b/public/application.js index be5a2704..e260ccf3 100755 --- a/public/application.js +++ b/public/application.js @@ -9,8 +9,8 @@ angular.module(ApplicationConfiguration.applicationModuleName).config(['$locatio $locationProvider.hashPrefix('!'); } ]); -angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Principal', - function($rootScope, Principal) { +angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', + function($rootScope) { $rootScope.$on('$stateChangeStart', function(event, toState, toStateParams) { // track the state the user wants to go to; authorization service needs this $rootScope.toState = toState; diff --git a/public/modules/core/config/core.client.routes.js b/public/modules/core/config/core.client.routes.js index 730618f9..b0fa9147 100755 --- a/public/modules/core/config/core.client.routes.js +++ b/public/modules/core/config/core.client.routes.js @@ -14,7 +14,7 @@ angular.module('core').config(['$stateProvider', '$urlRouterProvider', }); $urlRouterProvider.otherwise( function($injector) { - var $state = $injector.get("$state"); + var $state = $injector.get('$state'); $state.go('home'); }); diff --git a/public/modules/core/controllers/header.client.controller.js b/public/modules/core/controllers/header.client.controller.js index 8f4cb911..e87b835c 100755 --- a/public/modules/core/controllers/header.client.controller.js +++ b/public/modules/core/controllers/header.client.controller.js @@ -1,9 +1,9 @@ 'use strict'; -angular.module('core').controller('HeaderController', ['$rootScope','$scope', 'Principal', 'Menus', '$state', - function($rootScope, $scope, Auth, Menus, $state) { - $rootScope.authentication = Auth; - $rootScope.user = {}, +angular.module('core').controller('HeaderController', ['$rootScope','$scope','Menus', '$state', + function($rootScope, $scope, Menus, $state) { + // $rootScope.authentication = Auth; + // $rootScope.user = {}, $scope.isCollapsed = false; $scope.hideNav = false; $scope.menu = Menus.getMenu('topbar'); @@ -16,45 +16,45 @@ angular.module('core').controller('HeaderController', ['$rootScope','$scope', 'P // function(error){ // console.log(error); // }).then(function(){ - $scope.signout = function() { - // $http.get('/auth/signout').success(function(response) { - // $state.go('home'); - // }).error(function(error) { - // $scope.error = (error.message || error); - // }); + // $scope.signout = function() { + // $http.get('/auth/signout').success(function(response) { + // $state.go('home'); + // }).error(function(error) { + // $scope.error = (error.message || error); + // }); - Principal.signout().then( - function(result){ - $state.go('home'); - }, - function(error){ - $scope.error = (error.message || error); - } - ); - // if( angular.isDefined(response_obj.error) ){ + // Principal.signout().then( + // function(result){ + // $state.go('home'); + // }, + // function(error){ + // $scope.error = (error.message || error); + // } + // ); + // if( angular.isDefined(response_obj.error) ){ // $scope.error = response_obj.error; // } else{ // $state.go('home'); // } - }; + // }; - $scope.toggleCollapsibleMenu = function() { - $scope.isCollapsed = !$scope.isCollapsed; - }; + $scope.toggleCollapsibleMenu = function() { + $scope.isCollapsed = !$scope.isCollapsed; + }; - // Collapsing the menu after navigation - $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { - $scope.isCollapsed = false; - $scope.hideNav = false; - if ( angular.isDefined( toState.data ) ) { + // Collapsing the menu after navigation + $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { + $scope.isCollapsed = false; + $scope.hideNav = false; + if ( angular.isDefined( toState.data ) ) { - if ( angular.isDefined( toState.data.hideNav ) ) { - $scope.hideNav = toState.data.hideNav; - } - } - }); + if ( angular.isDefined( toState.data.hideNav ) ) { + $scope.hideNav = toState.data.hideNav; + } + } }); + // }); } ]); \ No newline at end of file diff --git a/public/modules/core/controllers/home.client.controller.js b/public/modules/core/controllers/home.client.controller.js index d27115b8..269c4504 100755 --- a/public/modules/core/controllers/home.client.controller.js +++ b/public/modules/core/controllers/home.client.controller.js @@ -1,13 +1,13 @@ 'use strict'; -angular.module('core').controller('HomeController', ['$rootScope', '$scope', 'Principal', - function($rootScope, $scope, Principal) { +angular.module('core').controller('HomeController', ['$rootScope', '$scope', + function($rootScope, $scope) { // This provides Principal context. - $scope.authentication = Principal; + // $scope.authentication = Principal; // $scope.user = {}; - $rootScope.user = $window.user; + // $rootScope.user = $window.user; console.log($rootScope.user); // Principal.identity().then(function(user){ diff --git a/public/modules/index.js b/public/modules/core/controllers/index.client.controller.js similarity index 56% rename from public/modules/index.js rename to public/modules/core/controllers/index.client.controller.js index a2f4c7ee..52a851e5 100644 --- a/public/modules/index.js +++ b/public/modules/core/controllers/index.client.controller.js @@ -2,50 +2,51 @@ /** * @ngdoc function - * @name AvianServer.controller:IndexCtrl + * @name medform.controller:IndexCtrl * @description * # IndexCtrl - * Controller of the AvianServer + * Controller of core */ -angular.module('AvianServer') - .controller('IndexCtrl', function ($scope, $location, User, Auth, $state) { - Auth.ensureHasCurrentUser(User); - $scope.user = Auth.getUserState(); - $scope.authorization = Auth; +angular.module('medform').controller('IndexCtrl', function ($scope, $rootScope, $location, User, Auth, $state) { + $rootScope.user = Auth.ensureHasCurrentUser(User); + // $rootScope.user = Auth.getUserState(User).user; + $rootScope.authorization = Auth; - $scope.login = function() { + $scope.signin = function() { Auth.currentUser = User.login($scope.credentials, - function() { + function(response) { // console.log(response); // Auth.currentUser = $rootScope.loginResult.user; Auth.login(); + $rootScope.user = Auth.ensureHasCurrentUser(User); // console.log( $rootScope.loginResult.user); - $location.path('home'); + $location.path('listForms'); }, function(res) { $scope.loginError = res.data.error; - console.log(res); + console.log('loginError: '+res.data.error); + $rootScope.user = Auth.ensureHasCurrentUser(User); - if(!$scope.loginError){ + // if(!$scope.loginError){ // Auth.currentUser = rootScope.loginResult.user; // console.log(Auth.currentUser ); - } + // } // Auth.currentUser = $rootScope.loginResult.user; } - );- + ); console.log(Auth.currentUser); // Auth.currentUser = $rootScope.loginResult; }; - $scope.register = function() { + $scope.signup = function() { $scope.user = User.save($scope.registration, function() { }, @@ -59,11 +60,12 @@ angular.module('AvianServer') ); }; - $scope.logout = function() { + $scope.signout = function() { User.logout(function() { Auth.logout(); - $state.go('index'); - $scope.$apply(); + $rootScope.user = null; + $state.go('home'); + // $scope.$apply(); }); }; diff --git a/public/modules/forms/controllers/create-form.client.controller.js b/public/modules/forms/controllers/create-form.client.controller.js index f7b5761b..577ea3a0 100644 --- a/public/modules/forms/controllers/create-form.client.controller.js +++ b/public/modules/forms/controllers/create-form.client.controller.js @@ -1,7 +1,7 @@ 'use strict'; -angular.module('forms').controller('EditFormController', ['$scope', '$state', 'Upload', '$stateParams', 'Principal', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location', - function ($scope, $state, Upload, $stateParams, Principal, FormFields, Forms, CurrentForm, $modal, $location) { +angular.module('forms').controller('EditFormController', ['$scope', '$state', 'Upload', '$stateParams', 'FormFields', 'Forms', 'CurrentForm', '$modal', '$location', + function ($scope, $state, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location) { // Principal.identity().then(function(user){ // $scope.authentication.user = user; // }).then(function(){ diff --git a/public/modules/forms/controllers/submit-form.client.controller.js b/public/modules/forms/controllers/submit-form.client.controller.js index bdd0d0ef..6e6405b3 100644 --- a/public/modules/forms/controllers/submit-form.client.controller.js +++ b/public/modules/forms/controllers/submit-form.client.controller.js @@ -1,8 +1,8 @@ 'use strict'; // Forms controller -angular.module('forms').controller('SubmitFormController', ['$scope', '$stateParams', '$state', 'Principal', 'Forms', 'CurrentForm', - function($scope, $stateParams, $state, Principal, Forms, CurrentForm) { +angular.module('forms').controller('SubmitFormController', ['$scope', '$stateParams', '$state', 'Forms', 'CurrentForm', + function($scope, $stateParams, $state, Forms, CurrentForm) { // Principal.identity().then(function(user){ // $scope.authentication.user = user; diff --git a/public/modules/users/controllers/authentication.client.controller.js b/public/modules/users/controllers/authentication.client.controller.js index 1c532a55..a9f9af9d 100755 --- a/public/modules/users/controllers/authentication.client.controller.js +++ b/public/modules/users/controllers/authentication.client.controller.js @@ -1,63 +1,63 @@ -'use strict'; +// 'use strict'; -angular.module('users').controller('AuthenticationController', ['$scope', '$location', 'Principal', '$state', - function($scope, $location, Principal, $state) { +// angular.module('users').controller('AuthenticationController', ['$scope', '$location', '$state', +// function($scope, $location, $state) { - $scope.authentication = Principal; +// // $scope.authentication = Principal; - // If user is signed in then redirect back home - if ($scope.authentication.isAuthenticated()) $state.go('home'); +// // If user is signed in then redirect back home +// if ($scope.authentication.isAuthenticated()) $state.go('home'); - $scope.signup = function() { - Principal.signup($scope.credentials).then( - function(result){ - $state.go('home'); - }, - function(rejection_reason){ - $scope.error = rejection_reason; - } - ); - // $http.post('/auth/signup', $scope.credentials).success(function(response) { - // // If successful we assign the response to the global user model - // $scope.authentication.user = response; - // Principal.authenticate(response); +// $scope.signup = function() { +// Principal.signup($scope.credentials).then( +// function(result){ +// $state.go('home'); +// }, +// function(rejection_reason){ +// $scope.error = rejection_reason; +// } +// ); +// // $http.post('/auth/signup', $scope.credentials).success(function(response) { +// // // If successful we assign the response to the global user model +// // $scope.authentication.user = response; +// // Principal.authenticate(response); - // // And redirect to the index page - // $location.path('/'); - // }).error(function(response) { - // $scope.error = response.message; - // }); - }; +// // // And redirect to the index page +// // $location.path('/'); +// // }).error(function(response) { +// // $scope.error = response.message; +// // }); +// }; - $scope.signin = function() { - console.log('signin'); +// $scope.signin = function() { +// console.log('signin'); - Principal.signin($scope.credentials).then( - function(result){ - $state.go('home'); - }, - function(rejection_reason){ - $scope.error = rejection_reason; - } - ); - // var response_obj = Principal.signin($scope.credentials); - // if( angular.isDefined(response_obj.error) ){ - // $scope.error = response_obj.error; - // $location.path('/signin'); - // } else{ - // $location.path('/'); - // } - // $http.post('/auth/signin', $scope.credentials).success(function(response) { - // // If successful we assign the response to the global user model - // $scope.authentication.user = response; - // Principal.authenticate(response); +// Principal.signin($scope.credentials).then( +// function(result){ +// $state.go('home'); +// }, +// function(rejection_reason){ +// $scope.error = rejection_reason; +// } +// ); +// // var response_obj = Principal.signin($scope.credentials); +// // if( angular.isDefined(response_obj.error) ){ +// // $scope.error = response_obj.error; +// // $location.path('/signin'); +// // } else{ +// // $location.path('/'); +// // } +// // $http.post('/auth/signin', $scope.credentials).success(function(response) { +// // // If successful we assign the response to the global user model +// // $scope.authentication.user = response; +// // Principal.authenticate(response); - // // And redirect to the index page - // $location.path('/'); - // }).error(function(response) { - // Principal.authenticate(null); - // $scope.error = response.message; - // }); - }; - } -]); \ No newline at end of file +// // // And redirect to the index page +// // $location.path('/'); +// // }).error(function(response) { +// // Principal.authenticate(null); +// // $scope.error = response.message; +// // }); +// }; +// } +// ]); \ No newline at end of file diff --git a/public/modules/users/controllers/password.client.controller.js b/public/modules/users/controllers/password.client.controller.js index 269aa14a..99f5deac 100755 --- a/public/modules/users/controllers/password.client.controller.js +++ b/public/modules/users/controllers/password.client.controller.js @@ -1,18 +1,18 @@ 'use strict'; -angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$state', 'Principal', - function($scope, $stateParams, $state, Principal) { - $scope.authentication = Principal; +angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$state', 'User', + function($scope, $stateParams, $state, User) { + // $scope.authentication = Principal; //If user is signed in then redirect back home if ($scope.authentication.isAuthenticated()) $state.go('home'); - Principal.identity().then(function(response){ - $scope.authentication.user = response; + // Principal.identity().then(function(response){ + // $scope.authentication.user = response; // Submit forgotten password account id $scope.askForPasswordReset = function() { - Principal.askForPasswordReset($scope.credentials).then( + User.askForPasswordReset($scope.credentials).then( function(response){ $scope.success = response.message; $scope.credentials = null; @@ -27,7 +27,7 @@ angular.module('users').controller('PasswordController', ['$scope', '$stateParam // Change user password $scope.resetUserPassword = function() { $scope.success = $scope.error = null; - Principal.resetPassword($scope.passwordDetails, $stateParams.token).then( + User.resetPassword($scope.passwordDetails, $stateParams.token).then( function(response){ // If successful show success message and clear form $scope.success = response.message; @@ -56,6 +56,6 @@ angular.module('users').controller('PasswordController', ['$scope', '$stateParam // $scope.error = response.message; // }); }; - }); + // }); } ]); \ No newline at end of file diff --git a/public/modules/users/controllers/settings.client.controller.js b/public/modules/users/controllers/settings.client.controller.js index 4b9c274c..6683fcee 100755 --- a/public/modules/users/controllers/settings.client.controller.js +++ b/public/modules/users/controllers/settings.client.controller.js @@ -1,12 +1,12 @@ 'use strict'; -angular.module('users').controller('SettingsController', ['$scope', '$http', '$state', 'Users', 'Principal', - function($scope, $http, $state, Users, Principal) { +angular.module('users').controller('SettingsController', ['$scope', '$http', '$state', 'Users', + function($scope, $http, $state, Users) { // Principal.identity().then(function(user){ // $scope.user = user; // }).then(function(){ - $scope.user = Principal.identity(); + // $scope.user = Principal.identity(); // If user is not signed in then redirect back home if (!$scope.user) $state.go('home'); diff --git a/public/modules/users/services/auth.js b/public/modules/users/services/auth.js index 224aab22..60d972aa 100644 --- a/public/modules/users/services/auth.js +++ b/public/modules/users/services/auth.js @@ -1,11 +1,12 @@ 'use strict'; -angular.module('AvianServer') - .factory('Auth', function($cookies) { +angular.module('users') + .factory('Auth', function() { var userState = { // isLoggedIn: $cookies.get('isLoggedIn') isLoggedIn: false + // user: null }; return { @@ -18,35 +19,37 @@ angular.module('AvianServer') if (this.currentUser) { console.log('Using cached current user.'); console.log(this.currentUser); + return this.currentUser; } else{ console.log('Fetching current user from the server.'); this.currentUser = User.getCurrent(function() { // success userState.isLoggedIn = true; - $cookies.put('isLoggedIn', 'true'); + // $cookies.put('isLoggedIn', 'true'); + return this.currentUser; }, function(response) { userState.isLoggedIn = false; - $cookies.put('isLoggedIn', 'false'); + // $cookies.put('isLoggedIn', 'false'); console.log('User.getCurrent() err', response); + return null; }); } }, - getUserState: function() { + getUserState: function(user) { + // userState.user = ensureHasCurrentUser(user); return userState; }, - login: function() { - userState.isLoggedIn = true; - $cookies.put('isLoggedIn', 'true'); - this.ensureHasCurrentUser(null); + login: function(user) { + // userState.isLoggedIn = true; + // $cookies.put('isLoggedIn', 'true'); + this.ensureHasCurrentUser(user); }, logout: function() { - this.currentUser = null; - userState.isLoggedIn = false; - $cookies.put('isLoggedIn', 'false'); + this.ensureHasCurrentUser(null); }, }; }); diff --git a/public/modules/users/services/user.js b/public/modules/users/services/user.js index 0d630ed2..c3a8f8c7 100644 --- a/public/modules/users/services/user.js +++ b/public/modules/users/services/user.js @@ -6,7 +6,7 @@ angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', ' var userService = { getCurrent: function() { - deferred = $q.defer(); + var deferred = $q.defer(); $http.get('/users/me') .success(function(response) { @@ -15,6 +15,8 @@ angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', ' .error(function() { deferred.reject("User's session has expired"); }); + + return deferred.promise; }, login: function(credentials) { @@ -57,9 +59,6 @@ angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', ' var deferred = $q.defer(); $http.get('/auth/password/'+token, passwordDetails).success(function(response) { - // Attach user profile - service.authenticate(response); - deferred.resolve(); }).error(function(error) { deferred.reject(error.message || error);