got new auth to start working

This commit is contained in:
David Baldwynn 2015-06-30 13:57:20 -07:00
parent dc3a58ff85
commit d6b64187ab
13 changed files with 150 additions and 145 deletions

3
config/env/all.js vendored
View file

@ -65,7 +65,8 @@ module.exports = {
'public/lib/angular-ui-router/release/angular-ui-router.js', 'public/lib/angular-ui-router/release/angular-ui-router.js',
'public/lib/angular-ui-utils/ui-utils.js', 'public/lib/angular-ui-utils/ui-utils.js',
'public/lib/angular-bootstrap/ui-bootstrap-tpls.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: [ css: [

View file

@ -9,8 +9,8 @@ angular.module(ApplicationConfiguration.applicationModuleName).config(['$locatio
$locationProvider.hashPrefix('!'); $locationProvider.hashPrefix('!');
} }
]); ]);
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Principal', angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope',
function($rootScope, Principal) { function($rootScope) {
$rootScope.$on('$stateChangeStart', function(event, toState, toStateParams) { $rootScope.$on('$stateChangeStart', function(event, toState, toStateParams) {
// track the state the user wants to go to; authorization service needs this // track the state the user wants to go to; authorization service needs this
$rootScope.toState = toState; $rootScope.toState = toState;

View file

@ -14,7 +14,7 @@ angular.module('core').config(['$stateProvider', '$urlRouterProvider',
}); });
$urlRouterProvider.otherwise( function($injector) { $urlRouterProvider.otherwise( function($injector) {
var $state = $injector.get("$state"); var $state = $injector.get('$state');
$state.go('home'); $state.go('home');
}); });

View file

@ -1,9 +1,9 @@
'use strict'; 'use strict';
angular.module('core').controller('HeaderController', ['$rootScope','$scope', 'Principal', 'Menus', '$state', angular.module('core').controller('HeaderController', ['$rootScope','$scope','Menus', '$state',
function($rootScope, $scope, Auth, Menus, $state) { function($rootScope, $scope, Menus, $state) {
$rootScope.authentication = Auth; // $rootScope.authentication = Auth;
$rootScope.user = {}, // $rootScope.user = {},
$scope.isCollapsed = false; $scope.isCollapsed = false;
$scope.hideNav = false; $scope.hideNav = false;
$scope.menu = Menus.getMenu('topbar'); $scope.menu = Menus.getMenu('topbar');
@ -16,45 +16,45 @@ angular.module('core').controller('HeaderController', ['$rootScope','$scope', 'P
// function(error){ // function(error){
// console.log(error); // console.log(error);
// }).then(function(){ // }).then(function(){
$scope.signout = function() { // $scope.signout = function() {
// $http.get('/auth/signout').success(function(response) { // $http.get('/auth/signout').success(function(response) {
// $state.go('home'); // $state.go('home');
// }).error(function(error) { // }).error(function(error) {
// $scope.error = (error.message || error); // $scope.error = (error.message || error);
// }); // });
Principal.signout().then( // Principal.signout().then(
function(result){ // function(result){
$state.go('home'); // $state.go('home');
}, // },
function(error){ // function(error){
$scope.error = (error.message || error); // $scope.error = (error.message || error);
} // }
); // );
// if( angular.isDefined(response_obj.error) ){ // if( angular.isDefined(response_obj.error) ){
// $scope.error = response_obj.error; // $scope.error = response_obj.error;
// } else{ // } else{
// $state.go('home'); // $state.go('home');
// } // }
}; // };
$scope.toggleCollapsibleMenu = function() { $scope.toggleCollapsibleMenu = function() {
$scope.isCollapsed = !$scope.isCollapsed; $scope.isCollapsed = !$scope.isCollapsed;
}; };
// Collapsing the menu after navigation // Collapsing the menu after navigation
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) { $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
$scope.isCollapsed = false; $scope.isCollapsed = false;
$scope.hideNav = false; $scope.hideNav = false;
if ( angular.isDefined( toState.data ) ) { if ( angular.isDefined( toState.data ) ) {
if ( angular.isDefined( toState.data.hideNav ) ) { if ( angular.isDefined( toState.data.hideNav ) ) {
$scope.hideNav = toState.data.hideNav; $scope.hideNav = toState.data.hideNav;
} }
} }
});
}); });
// });
} }
]); ]);

View file

@ -1,13 +1,13 @@
'use strict'; 'use strict';
angular.module('core').controller('HomeController', ['$rootScope', '$scope', 'Principal', angular.module('core').controller('HomeController', ['$rootScope', '$scope',
function($rootScope, $scope, Principal) { function($rootScope, $scope) {
// This provides Principal context. // This provides Principal context.
$scope.authentication = Principal; // $scope.authentication = Principal;
// $scope.user = {}; // $scope.user = {};
$rootScope.user = $window.user; // $rootScope.user = $window.user;
console.log($rootScope.user); console.log($rootScope.user);
// Principal.identity().then(function(user){ // Principal.identity().then(function(user){

View file

@ -2,50 +2,51 @@
/** /**
* @ngdoc function * @ngdoc function
* @name AvianServer.controller:IndexCtrl * @name medform.controller:IndexCtrl
* @description * @description
* # IndexCtrl * # IndexCtrl
* Controller of the AvianServer * Controller of core
*/ */
angular.module('AvianServer') angular.module('medform').controller('IndexCtrl', function ($scope, $rootScope, $location, User, Auth, $state) {
.controller('IndexCtrl', function ($scope, $location, User, Auth, $state) { $rootScope.user = Auth.ensureHasCurrentUser(User);
Auth.ensureHasCurrentUser(User); // $rootScope.user = Auth.getUserState(User).user;
$scope.user = Auth.getUserState(); $rootScope.authorization = Auth;
$scope.authorization = Auth;
$scope.login = function() { $scope.signin = function() {
Auth.currentUser = User.login($scope.credentials, Auth.currentUser = User.login($scope.credentials,
function() { function(response) {
// console.log(response); // console.log(response);
// Auth.currentUser = $rootScope.loginResult.user; // Auth.currentUser = $rootScope.loginResult.user;
Auth.login(); Auth.login();
$rootScope.user = Auth.ensureHasCurrentUser(User);
// console.log( $rootScope.loginResult.user); // console.log( $rootScope.loginResult.user);
$location.path('home'); $location.path('listForms');
}, },
function(res) { function(res) {
$scope.loginError = res.data.error; $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; // Auth.currentUser = rootScope.loginResult.user;
// console.log(Auth.currentUser ); // console.log(Auth.currentUser );
} // }
// Auth.currentUser = $rootScope.loginResult.user; // Auth.currentUser = $rootScope.loginResult.user;
} }
);- );
console.log(Auth.currentUser); console.log(Auth.currentUser);
// Auth.currentUser = $rootScope.loginResult; // Auth.currentUser = $rootScope.loginResult;
}; };
$scope.register = function() { $scope.signup = function() {
$scope.user = User.save($scope.registration, $scope.user = User.save($scope.registration,
function() { function() {
}, },
@ -59,11 +60,12 @@ angular.module('AvianServer')
); );
}; };
$scope.logout = function() { $scope.signout = function() {
User.logout(function() { User.logout(function() {
Auth.logout(); Auth.logout();
$state.go('index'); $rootScope.user = null;
$scope.$apply(); $state.go('home');
// $scope.$apply();
}); });
}; };

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('forms').controller('EditFormController', ['$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, Principal, FormFields, Forms, CurrentForm, $modal, $location) { function ($scope, $state, Upload, $stateParams, FormFields, Forms, CurrentForm, $modal, $location) {
// Principal.identity().then(function(user){ // Principal.identity().then(function(user){
// $scope.authentication.user = user; // $scope.authentication.user = user;
// }).then(function(){ // }).then(function(){

View file

@ -1,8 +1,8 @@
'use strict'; 'use strict';
// Forms controller // Forms controller
angular.module('forms').controller('SubmitFormController', ['$scope', '$stateParams', '$state', 'Principal', 'Forms', 'CurrentForm', angular.module('forms').controller('SubmitFormController', ['$scope', '$stateParams', '$state', 'Forms', 'CurrentForm',
function($scope, $stateParams, $state, Principal, Forms, CurrentForm) { function($scope, $stateParams, $state, Forms, CurrentForm) {
// Principal.identity().then(function(user){ // Principal.identity().then(function(user){
// $scope.authentication.user = user; // $scope.authentication.user = user;

View file

@ -1,63 +1,63 @@
'use strict'; // 'use strict';
angular.module('users').controller('AuthenticationController', ['$scope', '$location', 'Principal', '$state', // angular.module('users').controller('AuthenticationController', ['$scope', '$location', '$state',
function($scope, $location, Principal, $state) { // function($scope, $location, $state) {
$scope.authentication = Principal; // // $scope.authentication = Principal;
// If user is signed in then redirect back home // // If user is signed in then redirect back home
if ($scope.authentication.isAuthenticated()) $state.go('home'); // if ($scope.authentication.isAuthenticated()) $state.go('home');
$scope.signup = function() { // $scope.signup = function() {
Principal.signup($scope.credentials).then( // Principal.signup($scope.credentials).then(
function(result){ // function(result){
$state.go('home'); // $state.go('home');
}, // },
function(rejection_reason){ // function(rejection_reason){
$scope.error = rejection_reason; // $scope.error = rejection_reason;
} // }
); // );
// $http.post('/auth/signup', $scope.credentials).success(function(response) { // // $http.post('/auth/signup', $scope.credentials).success(function(response) {
// // If successful we assign the response to the global user model // // // If successful we assign the response to the global user model
// $scope.authentication.user = response; // // $scope.authentication.user = response;
// Principal.authenticate(response); // // Principal.authenticate(response);
// // And redirect to the index page // // // And redirect to the index page
// $location.path('/'); // // $location.path('/');
// }).error(function(response) { // // }).error(function(response) {
// $scope.error = response.message; // // $scope.error = response.message;
// }); // // });
}; // };
$scope.signin = function() { // $scope.signin = function() {
console.log('signin'); // console.log('signin');
Principal.signin($scope.credentials).then( // Principal.signin($scope.credentials).then(
function(result){ // function(result){
$state.go('home'); // $state.go('home');
}, // },
function(rejection_reason){ // function(rejection_reason){
$scope.error = rejection_reason; // $scope.error = rejection_reason;
} // }
); // );
// var response_obj = Principal.signin($scope.credentials); // // var response_obj = Principal.signin($scope.credentials);
// if( angular.isDefined(response_obj.error) ){ // // if( angular.isDefined(response_obj.error) ){
// $scope.error = response_obj.error; // // $scope.error = response_obj.error;
// $location.path('/signin'); // // $location.path('/signin');
// } else{ // // } else{
// $location.path('/'); // // $location.path('/');
// } // // }
// $http.post('/auth/signin', $scope.credentials).success(function(response) { // // $http.post('/auth/signin', $scope.credentials).success(function(response) {
// // If successful we assign the response to the global user model // // // If successful we assign the response to the global user model
// $scope.authentication.user = response; // // $scope.authentication.user = response;
// Principal.authenticate(response); // // Principal.authenticate(response);
// // And redirect to the index page // // // And redirect to the index page
// $location.path('/'); // // $location.path('/');
// }).error(function(response) { // // }).error(function(response) {
// Principal.authenticate(null); // // Principal.authenticate(null);
// $scope.error = response.message; // // $scope.error = response.message;
// }); // // });
}; // };
} // }
]); // ]);

View file

@ -1,18 +1,18 @@
'use strict'; 'use strict';
angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$state', 'Principal', angular.module('users').controller('PasswordController', ['$scope', '$stateParams', '$state', 'User',
function($scope, $stateParams, $state, Principal) { function($scope, $stateParams, $state, User) {
$scope.authentication = Principal; // $scope.authentication = Principal;
//If user is signed in then redirect back home //If user is signed in then redirect back home
if ($scope.authentication.isAuthenticated()) $state.go('home'); if ($scope.authentication.isAuthenticated()) $state.go('home');
Principal.identity().then(function(response){ // Principal.identity().then(function(response){
$scope.authentication.user = response; // $scope.authentication.user = response;
// Submit forgotten password account id // Submit forgotten password account id
$scope.askForPasswordReset = function() { $scope.askForPasswordReset = function() {
Principal.askForPasswordReset($scope.credentials).then( User.askForPasswordReset($scope.credentials).then(
function(response){ function(response){
$scope.success = response.message; $scope.success = response.message;
$scope.credentials = null; $scope.credentials = null;
@ -27,7 +27,7 @@ angular.module('users').controller('PasswordController', ['$scope', '$stateParam
// Change user password // Change user password
$scope.resetUserPassword = function() { $scope.resetUserPassword = function() {
$scope.success = $scope.error = null; $scope.success = $scope.error = null;
Principal.resetPassword($scope.passwordDetails, $stateParams.token).then( User.resetPassword($scope.passwordDetails, $stateParams.token).then(
function(response){ function(response){
// If successful show success message and clear form // If successful show success message and clear form
$scope.success = response.message; $scope.success = response.message;
@ -56,6 +56,6 @@ angular.module('users').controller('PasswordController', ['$scope', '$stateParam
// $scope.error = response.message; // $scope.error = response.message;
// }); // });
}; };
}); // });
} }
]); ]);

View file

@ -1,12 +1,12 @@
'use strict'; 'use strict';
angular.module('users').controller('SettingsController', ['$scope', '$http', '$state', 'Users', 'Principal', angular.module('users').controller('SettingsController', ['$scope', '$http', '$state', 'Users',
function($scope, $http, $state, Users, Principal) { function($scope, $http, $state, Users) {
// Principal.identity().then(function(user){ // Principal.identity().then(function(user){
// $scope.user = user; // $scope.user = user;
// }).then(function(){ // }).then(function(){
$scope.user = Principal.identity(); // $scope.user = Principal.identity();
// If user is not signed in then redirect back home // If user is not signed in then redirect back home
if (!$scope.user) $state.go('home'); if (!$scope.user) $state.go('home');

View file

@ -1,11 +1,12 @@
'use strict'; 'use strict';
angular.module('AvianServer') angular.module('users')
.factory('Auth', function($cookies) { .factory('Auth', function() {
var userState = var userState =
{ {
// isLoggedIn: $cookies.get('isLoggedIn') // isLoggedIn: $cookies.get('isLoggedIn')
isLoggedIn: false isLoggedIn: false
// user: null
}; };
return { return {
@ -18,35 +19,37 @@ angular.module('AvianServer')
if (this.currentUser) { if (this.currentUser) {
console.log('Using cached current user.'); console.log('Using cached current user.');
console.log(this.currentUser); console.log(this.currentUser);
return this.currentUser;
} else{ } else{
console.log('Fetching current user from the server.'); console.log('Fetching current user from the server.');
this.currentUser = User.getCurrent(function() { this.currentUser = User.getCurrent(function() {
// success // success
userState.isLoggedIn = true; userState.isLoggedIn = true;
$cookies.put('isLoggedIn', 'true'); // $cookies.put('isLoggedIn', 'true');
return this.currentUser;
}, },
function(response) { function(response) {
userState.isLoggedIn = false; userState.isLoggedIn = false;
$cookies.put('isLoggedIn', 'false'); // $cookies.put('isLoggedIn', 'false');
console.log('User.getCurrent() err', response); console.log('User.getCurrent() err', response);
return null;
}); });
} }
}, },
getUserState: function() { getUserState: function(user) {
// userState.user = ensureHasCurrentUser(user);
return userState; return userState;
}, },
login: function() { login: function(user) {
userState.isLoggedIn = true; // userState.isLoggedIn = true;
$cookies.put('isLoggedIn', 'true'); // $cookies.put('isLoggedIn', 'true');
this.ensureHasCurrentUser(null); this.ensureHasCurrentUser(user);
}, },
logout: function() { logout: function() {
this.currentUser = null; this.ensureHasCurrentUser(null);
userState.isLoggedIn = false;
$cookies.put('isLoggedIn', 'false');
}, },
}; };
}); });

View file

@ -6,7 +6,7 @@ angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', '
var userService = { var userService = {
getCurrent: function() { getCurrent: function() {
deferred = $q.defer(); var deferred = $q.defer();
$http.get('/users/me') $http.get('/users/me')
.success(function(response) { .success(function(response) {
@ -15,6 +15,8 @@ angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', '
.error(function() { .error(function() {
deferred.reject("User's session has expired"); deferred.reject("User's session has expired");
}); });
return deferred.promise;
}, },
login: function(credentials) { login: function(credentials) {
@ -57,9 +59,6 @@ angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', '
var deferred = $q.defer(); var deferred = $q.defer();
$http.get('/auth/password/'+token, passwordDetails).success(function(response) { $http.get('/auth/password/'+token, passwordDetails).success(function(response) {
// Attach user profile
service.authenticate(response);
deferred.resolve(); deferred.resolve();
}).error(function(error) { }).error(function(error) {
deferred.reject(error.message || error); deferred.reject(error.message || error);