got authentication working

This commit is contained in:
David Baldwynn 2015-06-30 00:28:29 -07:00
parent ebca4591fa
commit 0045f3d9cb
31 changed files with 161 additions and 125 deletions

View file

@ -134,7 +134,7 @@ FormSchema.pre('save', function (next) {
//Convert types from FDF to 'FormField' types //Convert types from FDF to 'FormField' types
if(_typeConvMap[ field.fieldType+'' ]){ if(_typeConvMap[ field.fieldType+'' ]){
field.fieldType = _pdfConvMap[ field.fieldType+'' ]; field.fieldType = _typeConvMap[ field.fieldType+'' ];
} }
field.created = Date.now(); field.created = Date.now();

View file

@ -10,12 +10,12 @@ module.exports = function(app) {
var users = require('../../app/controllers/users.server.controller'); var users = require('../../app/controllers/users.server.controller');
// Setting up the users profile api // Setting up the users profile api
app.route('/users/me').get(users.me); app.route('/users/me').get(users.requiresLogin, users.me);
app.route('/users').put(users.update); app.route('/users').put(users.requiresLogin, users.update);
app.route('/users/accounts').delete(users.removeOAuthProvider); app.route('/users/accounts').delete(users.requiresLogin, users.removeOAuthProvider);
// Setting up the users password api // Setting up the users password api
app.route('/users/password').post(users.changePassword); app.route('/users/password').post(users.requiresLogin, users.changePassword);
app.route('/auth/forgot').post(users.forgot); app.route('/auth/forgot').post(users.forgot);
app.route('/auth/reset/:token').get(users.validateResetToken); app.route('/auth/reset/:token').get(users.validateResetToken);
app.route('/auth/reset/:token').post(users.reset); app.route('/auth/reset/:token').post(users.reset);

16
data1435634800654.fdf Normal file
View file

@ -0,0 +1,16 @@
%FDF-1.2
%âãÏÓ
1 0 obj
<<
/FDF
<<
/Fields []
>>
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF

View file

@ -9,15 +9,15 @@ angular.module(ApplicationConfiguration.applicationModuleName).config(['$locatio
$locationProvider.hashPrefix('!'); $locationProvider.hashPrefix('!');
} }
]); ]);
angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Authorization', 'Principal', angular.module(ApplicationConfiguration.applicationModuleName).run(['$rootScope', 'Principal',
function($rootScope, Authorization, Principal) { function($rootScope, Principal) {
$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;
$rootScope.toStateParams = toStateParams; $rootScope.toStateParams = toStateParams;
// if the principal is resolved, do an authorization check immediately. otherwise, // if the principal is resolved, do an authorization check immediately. otherwise,
// it'll be done when the state it resolved. // it'll be done when the state it resolved.
if (Principal.isIdentityResolved()) Authorization.authorize(); // if (Principal.isIdentityResolved()) Authorization.authorize();
}); });
} }
]); ]);

View file

@ -7,9 +7,10 @@ angular.module('core').controller('HeaderController', ['$scope', 'Principal', 'M
$scope.hideNav = false; $scope.hideNav = false;
$scope.menu = Menus.getMenu('topbar'); $scope.menu = Menus.getMenu('topbar');
Principal.identity().then(function(user){ // Principal.identity().then(function(user){
$scope.authentication.user = user; // $scope.authentication.user = user;
}).then(function(){ // }).then(function(){
$scope.authentication.user = Principal.identity();
$scope.signout = function() { $scope.signout = function() {
var response_obj = Principal.signout(); var response_obj = Principal.signout();
if( angular.isDefined(response_obj.error) ){ if( angular.isDefined(response_obj.error) ){
@ -34,7 +35,7 @@ angular.module('core').controller('HeaderController', ['$scope', 'Principal', 'M
} }
} }
}); });
}); // });
} }
]); ]);

View file

@ -5,10 +5,15 @@ angular.module('core').controller('HomeController', ['$scope', 'Principal',
function($scope, Principal) { function($scope, Principal) {
// This provides Principal context. // This provides Principal context.
$scope.authentication = Principal; $scope.authentication = Principal;
$scope.authentication.user = undefined; $scope.authentication.user = Principal.identity();
Principal.identity().then(function(user){
$scope.authentication.user = user; console.log($scope.authentication.user);
}); // Principal.identity().then(function(user){
// console.log(user);
// $scope.authentication.user = user;
// }, function(){
// console.log('error');
// });
// console.log("user.displayName: "+Principal.user()._id); // console.log("user.displayName: "+Principal.user()._id);
} }

View file

@ -10,13 +10,14 @@ angular.module('users').controller('AuthenticationController', ['$scope', '$http
if ($scope.authentication.isAuthenticated()) $state.go('home'); if ($scope.authentication.isAuthenticated()) $state.go('home');
$scope.signup = function() { $scope.signup = function() {
var response_obj = Principal.signup($scope.credentials); Principal.signup($scope.credentials).then(
function(result){
if( angular.isDefined(response_obj.error) ){ $state.go('home');
$scope.error = response_obj.error; },
} else{ function(rejection_reason){
$state.go('home'); $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;
@ -30,13 +31,22 @@ angular.module('users').controller('AuthenticationController', ['$scope', '$http
$scope.signin = function() { $scope.signin = function() {
console.log('signin'); console.log('signin');
var response_obj = Principal.signin($scope.credentials);
if( angular.isDefined(response_obj.error) ){ Principal.signin($scope.credentials).then(
$scope.error = response_obj.error; function(result){
$location.path('/signin'); $state.go('home');
} else{ },
$location.path('/'); 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) { // $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;

View file

@ -3,9 +3,10 @@
angular.module('users').controller('SettingsController', ['$scope', '$http', '$state', 'Users', 'Principal', angular.module('users').controller('SettingsController', ['$scope', '$http', '$state', 'Users', 'Principal',
function($scope, $http, $state, Users, Principal) { function($scope, $http, $state, Users, Principal) {
Principal.identity().then(function(user){ // Principal.identity().then(function(user){
$scope.user = user; // $scope.user = user;
}).then(function(){ // }).then(function(){
$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');
@ -71,6 +72,6 @@ angular.module('users').controller('SettingsController', ['$scope', '$http', '$s
}); });
}; };
}); // });
} }
]); ]);

View file

@ -1,28 +1,28 @@
'use strict'; // 'use strict';
angular.module('users').service('Authorization', ['$rootScope', '$location', 'Principal', // angular.module('users').service('Authorization', ['$rootScope', '$location', 'Principal',
function($rootScope, $location, Principal) { // function($rootScope, $location, Principal) {
this.authorize = function() { // this.authorize = function() {
return Principal.identity().then(function(){ // return Principal.identity().then(function(){
var isAuthenticated = Principal.isAuthenticated(); // var isAuthenticated = Principal.isAuthenticated();
if( angular.isDefined($rootScope.toState.data) ){ // if( angular.isDefined($rootScope.toState.data) ){
// if ($rootScope.toState.data.roles && $rootScope.toState.data.roles.length > 0 && !principal.isInAnyRole($rootScope.toState.data.roles)) { // // if ($rootScope.toState.data.roles && $rootScope.toState.data.roles.length > 0 && !principal.isInAnyRole($rootScope.toState.data.roles)) {
if (!isAuthenticated){ //$location.path('/access_denied'); // user is signed in but not authorized for desired state // if (!isAuthenticated){ //$location.path('/access_denied'); // user is signed in but not authorized for desired state
// console.log('isAuthenticated: '+isAuthenticated); // // console.log('isAuthenticated: '+isAuthenticated);
// else { // // else {
// user is not authenticated. so the state they wanted before you // // user is not authenticated. so the state they wanted before you
// send them to the signin state, so you can return them when you're done // // send them to the signin state, so you can return them when you're done
$rootScope.returnToState = $rootScope.toState; // $rootScope.returnToState = $rootScope.toState;
$rootScope.returnToStateParams = $rootScope.toStateParams; // $rootScope.returnToStateParams = $rootScope.toStateParams;
// now, send them to the signin state so they can log in // // now, send them to the signin state so they can log in
$location.path('/signin'); // $location.path('/signin');
} // }
// } // // }
} // }
}); // });
}; // };
} // }
]); // ]);

View file

@ -2,23 +2,24 @@
angular.module('users').factory('Principal', ['$window', '$http', '$q', '$timeout', '$state', angular.module('users').factory('Principal', ['$window', '$http', '$q', '$timeout', '$state',
function($window, $http, $q, $timeout, $state) { function($window, $http, $q, $timeout, $state) {
var _identity,
_authenticated = false;
return { var service = {
_currentUser: null,
isIdentityResolved: function() { isIdentityResolved: function() {
return angular.isDefined(_identity); if(service._currentUser === null) return false
return true;
}, },
isAuthenticated: function() { isAuthenticated: function() {
return _authenticated; return !!service._currentUser;
}, },
isInRole: function(role) { isInRole: function(role) {
if (!_authenticated || !_identity.roles) return false; if (!service.isAuthenticated() || !service._currentUser.roles) return false;
return _identity.roles.indexOf(role) !== -1; return service._currentUser.roles.indexOf(role) !== -1;
}, },
isInAnyRole: function(roles) { isInAnyRole: function(roles) {
if (!_authenticated || !_identity.roles) return false; if (!service.isAuthenticated() || !service._currentUser.roles) return false;
for (var i = 0; i < roles.length; i++) { for (var i = 0; i < roles.length; i++) {
if (this.isInRole(roles[i])) return true; if (this.isInRole(roles[i])) return true;
@ -27,24 +28,23 @@ angular.module('users').factory('Principal', ['$window', '$http', '$q', '$timeou
return false; return false;
}, },
authenticate: function(user) { authenticate: function(user) {
_identity = user; service._currentUser = user;
_authenticated = (user !== null);
// store the user in $window
// for this demo, we'll store the identity in localStorage. For you, it could be a cookie, sessionStorage, whatever
if (user) $window.user = user; if (user) $window.user = user;
else $window.user = null; else $window.user = null;
}, },
signin: function(credentials) { signin: function(credentials) {
var deferred = $q.defer(); var deferred = $q.defer();
var self = this;
$http.post('/auth/signin', credentials).success(function(response) { $http.post('/auth/signin', credentials).success(function(response) {
console.log(response);
// If successful we assign the response to the global user model // If successful we assign the response to the global user model
self.authenticate(response); service.authenticate(response);
deferred.resolve(response); deferred.resolve(response);
}).error(function(response) { }).error(function(error) {
_authenticated = false;
deferred.resolve({ error: response.message }); deferred.reject(error.message || error);
}); });
return deferred.promise; return deferred.promise;
}, },
@ -55,9 +55,9 @@ angular.module('users').factory('Principal', ['$window', '$http', '$q', '$timeou
$http.post('/auth/signup', credentials).success(function(response) { $http.post('/auth/signup', 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
deferred.resolve(response); deferred.resolve(response);
}).error(function(response) { }).error(function(error) {
deferred.resolve({ error: response.message }); deferred.reject(error.message || error);
}); });
return deferred.promise; return deferred.promise;
@ -66,63 +66,66 @@ angular.module('users').factory('Principal', ['$window', '$http', '$q', '$timeou
var deferred = $q.defer(); var deferred = $q.defer();
$http.get('/auth/signout').success(function(response) { $http.get('/auth/signout').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
deferred.resolve({}); deferred.resolve(null);
}).error(function(response) { service.authenticate(null);
deferred.resolve({ error: response.message }); }).error(function(error) {
deferred.reject(error.message || error);
}); });
_authenticated = false;
_identity = undefined;
return deferred.promise; return deferred.promise;
}, },
identity: function(force) { identity: function() {
var self = this;
var deferred = $q.defer(); if (service.isAuthenticated()) {
return service._currentUser;
if (force === true) _identity = undefined; } else if($window.user){
service.authenticate($window.user);
// check and see if we have retrieved the user data from the server. if we have, reuse it by immediately resolving return service._currentUser;
if (angular.isDefined(_identity)) {
deferred.resolve(_identity);
return deferred.promise;
}else if($window.user){
// console.log($window.user);
// self.authenticate($window.user);
// var user = $window.user;
_identity = $window.user;
self.authenticate(_identity);
deferred.resolve(_identity);
return deferred.promise;
}else { }else {
return $http.get('/user/me')
// otherwise, retrieve the user data from the server, update the user object, and then resolve. .success(function(response) {
$http.get('/users/me', { ignoreErrors: true }) service.authenticate(response.data.user);
.success(function(response) { return response.data.user;
self.authenticate(response); })
$window.user = response; .error(function() {
deferred.resolve(_identity); service.authenticate(null);
}) // $state.go('signin');
.error(function() { return null;
_identity = null; });
_authenticated = false;
$window.user = null;
$state.path('signin');
deferred.resolve(_identity);
});
return deferred.promise;
} }
},
getUser: function(){ // var deferred = $q.defer();
this.identity(false).then( function(user){
return user; // console.log($window.user);
}); // console.log(service.isAuthenticated());
// // check and see if we have retrieved the user data from the server. if we have, reuse it by immediately resolving
// if (service.isAuthenticated() === true ) {
// deferred.resolve(service.currentUser);
// }else if($window.user){
// service.authenticate($window.user);
// deferred.resolve(service._currentUser);
// }else {
// // otherwise, retrieve the user data from the server, update the user object, and then resolve.
// $http.get('/users/me')
// .success(function(response) {
// service.authenticate(response);
// deferred.resolve(response);
// })
// .error(function() {
// service.authenticate(null);
// deferred.reject("User's session has expired");
// });
// }
// return deferred.promise;
} }
}; };
return service;
} }
]); ]);

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.