- Hi there {{authentication.user.displayName}}
+ Hi there {{user.displayName}}
diff --git a/public/modules/forms/controllers/view-form-submission.client.controller.js b/public/modules/forms/controllers/view-form-submission.client.controller.js
index a7f55de3..aa82a69f 100644
--- a/public/modules/forms/controllers/view-form-submission.client.controller.js
+++ b/public/modules/forms/controllers/view-form-submission.client.controller.js
@@ -12,14 +12,14 @@ angular.module('forms').controller('ViewSubmissionController', ['$scope', '$stat
// Return all form's submissions
$scope.findAll = function() {
- $scope.submissions = submissions.query({
+ $scope.submissions = Submissions.query({
formId: $stateParams.formId
});
};
// Find a specific submission
$scope.findOne = function() {
- $scope.submission = submissions.get({
+ $scope.submission = Submissions.get({
submissionId: $scope.submissionId,
formId: $stateParams.formId
});
diff --git a/public/modules/index.js b/public/modules/index.js
new file mode 100644
index 00000000..a2f4c7ee
--- /dev/null
+++ b/public/modules/index.js
@@ -0,0 +1,71 @@
+'use strict';
+
+/**
+ * @ngdoc function
+ * @name AvianServer.controller:IndexCtrl
+ * @description
+ * # IndexCtrl
+ * Controller of the AvianServer
+ */
+angular.module('AvianServer')
+ .controller('IndexCtrl', function ($scope, $location, User, Auth, $state) {
+ Auth.ensureHasCurrentUser(User);
+ $scope.user = Auth.getUserState();
+ $scope.authorization = Auth;
+
+
+ $scope.login = function() {
+ Auth.currentUser = User.login($scope.credentials,
+ function() {
+
+ // console.log(response);
+ // Auth.currentUser = $rootScope.loginResult.user;
+ Auth.login();
+
+ // console.log( $rootScope.loginResult.user);
+
+ $location.path('home');
+ },
+ function(res) {
+
+ $scope.loginError = res.data.error;
+ console.log(res);
+
+ 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.user = User.save($scope.registration,
+ function() {
+ },
+ function(res) {
+ if(res && res.data) {
+ $scope.registerError = res.data.error;
+ }else {
+ console.log('No response received');
+ }
+ }
+ );
+ };
+
+ $scope.logout = function() {
+ User.logout(function() {
+ Auth.logout();
+ $state.go('index');
+ $scope.$apply();
+ });
+ };
+
+
+ });
diff --git a/public/modules/users/config/users.client.config.js b/public/modules/users/config/users.client.config.js
index 79380e96..693e54a1 100755
--- a/public/modules/users/config/users.client.config.js
+++ b/public/modules/users/config/users.client.config.js
@@ -3,28 +3,56 @@
// Config HTTP Error Handling
angular.module('users').config(['$httpProvider',
function($httpProvider) {
- // Set the httpProvider "not authorized" interceptor
- $httpProvider.interceptors.push(['$q', '$state', 'Principal',
- function($q, $state, Principal) {
- return {
- responseError: function(rejection) {
- switch (rejection.status) {
- case 401:
- // Deauthenticate the global user
- Principal.authenticate(null);
+ $httpProvider.interceptors.push(function($q, $location) {
+ return {
+ responseError: function(response) {
+ console.log('intercepted rejection of ', response.config.url, response.status);
+ if (response.status === 401 || response.status === 403) {
+ // save the current location so that login can redirect back
+ $location.nextAfterLogin = $location.path();
+ $location.path('/login');
+ }
+ return $q.reject(response);
+ }
+ };
+ });
+}]);
- // Redirect to signin page
- $state.go('signin');
- break;
- case 403:
- // Add unauthorized behaviour
- break;
- }
- return $q.reject(rejection);
- }
- };
- }
- ]);
- }
-]);
\ No newline at end of file
+// Config HTTP Error Handling
+// angular.module('users').config(['$httpProvider',
+// function($httpProvider) {
+// // Set the httpProvider "not authorized" interceptor
+// $httpProvider.interceptors.push(['$q', '$location', 'Principal',
+// function($q, $state, Principal) {
+// return {
+// responseSuccess: function(response) {
+// Principal.identity().then(function(user){
+// console.log(user);
+// // $rootScope.user = user;
+// }, function(error){
+// console.log("Coudn't get current user. \n ERROR: "+error);
+// });
+
+// },
+// responseError: function(rejection) {
+// switch (rejection.status) {
+// case 401:
+// // Deauthenticate the global user
+// Principal.authenticate(null);
+
+// // Redirect to signin page
+// $location.path('/signin');
+// break;
+// case 403:
+// // Add unauthorized behaviour
+// break;
+// }
+
+// return $q.reject(rejection);
+// }
+// };
+// }
+// ]);
+// }
+// ]);
\ No newline at end of file
diff --git a/public/modules/users/config/users.client.routes.js b/public/modules/users/config/users.client.routes.js
index 40711f41..1c3732ac 100755
--- a/public/modules/users/config/users.client.routes.js
+++ b/public/modules/users/config/users.client.routes.js
@@ -3,13 +3,41 @@
// Setting up route
angular.module('users').config(['$stateProvider',
function($stateProvider) {
- // Users state routing
- $stateProvider.
+
+
+ var checkLoggedin = function($q, $timeout, $location, User, Auth) {
+ var deferred = $q.defer();
+
+ console.log(Auth.getUserState);
+
+ if (Auth.currentUser && Auth.currentUser.email) {
+ $timeout(deferred.resolve);
+ }
+ else {
+ Auth.currentUser = User.getCurrent(function() {
+ Auth.login();
+ $timeout(deferred.resolve);
+ },
+ function() {
+ Auth.logout();
+ $timeout(deferred.reject);
+ $location.path('/login');
+ });
+ }
+
+ return deferred.promise;
+ };
+
+ // Users state routing
+ $stateProvider.
state('profile', {
// parent: 'restricted',
// data: {
// roles: ['user', 'admin'],
// },
+ resolve: {
+ loggedin: checkLoggedin
+ },
url: '/settings/profile',
templateUrl: 'modules/users/views/settings/edit-profile.client.view.html'
}).
@@ -18,6 +46,9 @@ angular.module('users').config(['$stateProvider',
// data: {
// roles: ['user', 'admin'],
// },
+ resolve: {
+ loggedin: checkLoggedin
+ },
url: '/settings/password',
templateUrl: 'modules/users/views/settings/change-password.client.view.html'
}).
@@ -26,6 +57,9 @@ angular.module('users').config(['$stateProvider',
// data: {
// roles: ['user', 'admin'],
// },
+ resolve: {
+ loggedin: checkLoggedin
+ },
url: '/settings/accounts',
templateUrl: 'modules/users/views/settings/social-accounts.client.view.html'
}).
@@ -34,6 +68,10 @@ angular.module('users').config(['$stateProvider',
url: '/signup',
templateUrl: 'modules/users/views/authentication/signup.client.view.html'
}).
+ state('signup-success', {
+ url: '/signup-success',
+ templateUrl: 'modules/users/views/authentication/signup.client.view.html'
+ }).
state('signin', {
url: '/signin',
templateUrl: 'modules/users/views/authentication/signin.client.view.html'
diff --git a/public/modules/users/controllers/authentication.client.controller.js b/public/modules/users/controllers/authentication.client.controller.js
index 377437f3..1c532a55 100755
--- a/public/modules/users/controllers/authentication.client.controller.js
+++ b/public/modules/users/controllers/authentication.client.controller.js
@@ -4,7 +4,6 @@ angular.module('users').controller('AuthenticationController', ['$scope', '$loca
function($scope, $location, Principal, $state) {
$scope.authentication = Principal;
- // $scope.authentication.user = Principal.getUser();
// If user is signed in then redirect back home
if ($scope.authentication.isAuthenticated()) $state.go('home');
diff --git a/public/modules/users/css/users.css b/public/modules/users/css/users.css
index de67bf94..49a358c8 100755
--- a/public/modules/users/css/users.css
+++ b/public/modules/users/css/users.css
@@ -11,4 +11,22 @@
top: 10px;
right: 10px;
position: absolute;
-}
\ No newline at end of file
+}
+
+section.auth {
+ margin-top: 5em;
+}
+ section.auth > h3{
+ font-size: 3em;
+ font-weight: 500;
+ color: #777;
+ }
+
+ section.auth.signup-view > h3 {
+ font-size: 4.4em;
+ padding-bottom: 0.5em;
+ }
+
+ section.auth.signup-view.success > h3 {
+ padding-bottom: 1.2em;
+ }
\ No newline at end of file
diff --git a/public/modules/users/services/auth.js b/public/modules/users/services/auth.js
new file mode 100644
index 00000000..224aab22
--- /dev/null
+++ b/public/modules/users/services/auth.js
@@ -0,0 +1,52 @@
+'use strict';
+
+angular.module('AvianServer')
+ .factory('Auth', function($cookies) {
+ var userState =
+ {
+ // isLoggedIn: $cookies.get('isLoggedIn')
+ isLoggedIn: false
+ };
+
+ return {
+ currentUser: null,
+
+ // Note: we can't make the User a dependency of Auth
+ // because that would create a circular dependency
+ // Auth <- $http <- $resource <- LoopBackResource <- User <- Auth
+ ensureHasCurrentUser: function(User) {
+ if (this.currentUser) {
+ console.log('Using cached current user.');
+ console.log(this.currentUser);
+ } else{
+ console.log('Fetching current user from the server.');
+ this.currentUser = User.getCurrent(function() {
+ // success
+ userState.isLoggedIn = true;
+ $cookies.put('isLoggedIn', 'true');
+ },
+ function(response) {
+ userState.isLoggedIn = false;
+ $cookies.put('isLoggedIn', 'false');
+ console.log('User.getCurrent() err', response);
+ });
+ }
+ },
+
+ getUserState: function() {
+ return userState;
+ },
+
+ login: function() {
+ userState.isLoggedIn = true;
+ $cookies.put('isLoggedIn', 'true');
+ this.ensureHasCurrentUser(null);
+ },
+
+ logout: function() {
+ this.currentUser = null;
+ userState.isLoggedIn = false;
+ $cookies.put('isLoggedIn', 'false');
+ },
+ };
+ });
diff --git a/public/modules/users/services/authorization.client.service.js b/public/modules/users/services/authorization.client.service.js
deleted file mode 100644
index b75ca84c..00000000
--- a/public/modules/users/services/authorization.client.service.js
+++ /dev/null
@@ -1,28 +0,0 @@
-// 'use strict';
-
-// angular.module('users').service('Authorization', ['$rootScope', '$location', 'Principal',
-// function($rootScope, $location, Principal) {
-
-// this.authorize = function() {
-// return Principal.identity().then(function(){
-// var isAuthenticated = Principal.isAuthenticated();
-// if( angular.isDefined($rootScope.toState.data) ){
-// // 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
-// // console.log('isAuthenticated: '+isAuthenticated);
-
-// // else {
-// // 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
-// $rootScope.returnToState = $rootScope.toState;
-// $rootScope.returnToStateParams = $rootScope.toStateParams;
-
-// // now, send them to the signin state so they can log in
-// $location.path('/signin');
-// }
-// // }
-// }
-// });
-// };
-// }
-// ]);
\ No newline at end of file
diff --git a/public/modules/users/services/principal.client.service.js b/public/modules/users/services/principal.client.service.js
deleted file mode 100755
index 125f44a5..00000000
--- a/public/modules/users/services/principal.client.service.js
+++ /dev/null
@@ -1,163 +0,0 @@
-'use strict';
-
-angular.module('users').factory('Principal', ['$window', '$q', '$timeout', '$http', '$state',
- function($window, $q, $timeout, $http, $state) {
-
- var service = {
- _currentUser: null,
-
- isIdentityResolved: function() {
- if(service._currentUser === null) return false;
- return true;
- },
- isAuthenticated: function() {
- return !!service._currentUser;
- },
- isInRole: function(role) {
- if (!service.isAuthenticated() || !service._currentUser.roles) return false;
-
- return service._currentUser.roles.indexOf(role) !== -1;
- },
- isInAnyRole: function(roles) {
- if (!service.isAuthenticated() || !service._currentUser.roles) return false;
-
- for (var i = 0; i < roles.length; i++) {
- if (this.isInRole(roles[i])) return true;
- }
-
- return false;
- },
- authenticate: function(user) {
- service._currentUser = user;
-
- // store the user in $window
- if (user) $window.user = user;
- else $window.user = null;
- },
- signin: function(credentials) {
-
- var deferred = $q.defer();
- $http.post('/auth/signin', credentials).success(function(response) {
- console.log(response);
- // If successful we assign the response to the global user model
- service.authenticate(response);
- deferred.resolve(response);
- }).error(function(error) {
-
- deferred.reject(error.message || error);
- });
- return deferred.promise;
- },
- signup: function(credentials) {
-
- var deferred = $q.defer();
-
- $http.post('/auth/signup', credentials).success(function(response) {
- // If successful we assign the response to the global user model
- deferred.resolve(response);
- }).error(function(error) {
-
- deferred.reject(error.message || error);
- });
-
- return deferred.promise;
- },
- signout: function() {
- var deferred = $q.defer();
- $http.get('/auth/signout').success(function(response) {
- // If successful we assign the response to the global user model
- deferred.resolve(null);
- service.authenticate(null);
- }).error(function(error) {
- deferred.reject(error.message || error);
- });
-
- return deferred.promise;
- },
-
- resetPassword: function(passwordDetails, token) {
- 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);
- });
-
- return deferred.promise;
- },
-
- // Submit forgotten password account id
- askForPasswordReset: function(credentials) {
- var deferred = $q.defer();
- $http.post('/auth/forgot', credentials).success(function(response) {
- // Show user success message and clear form
-
- deferred.resolve(response);
-
- }).error(function(error) {
- // Show user error message
- deferred.reject(error.message || error);
- });
-
- return deferred.promise;
- },
- identity: function() {
-
- // if (service.isAuthenticated()) {
- // return service._currentUser;
- // } else if($window.user){
- // service.authenticate($window.user);
- // return service._currentUser;
- // }else {
- // return $http.get('/user/me')
- // .success(function(response) {
- // service.authenticate(response.data.user);
- // return response.data.user;
- // })
- // .error(function() {
- // service.authenticate(null);
- // // $state.go('signin');
- // return null;
- // });
- // }
-
- var deferred = $q.defer();
-
- 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;
-
- }
-]);
diff --git a/public/modules/users/services/user.js b/public/modules/users/services/user.js
new file mode 100644
index 00000000..0d630ed2
--- /dev/null
+++ b/public/modules/users/services/user.js
@@ -0,0 +1,92 @@
+'use strict';
+
+angular.module('users').factory('User', ['$window', '$q', '$timeout', '$http', '$state',
+ function($window, $q, $timeout, $http, $state) {
+
+
+ var userService = {
+ getCurrent: function() {
+ deferred = $q.defer();
+
+ $http.get('/users/me')
+ .success(function(response) {
+ deferred.resolve(response);
+ })
+ .error(function() {
+ deferred.reject("User's session has expired");
+ });
+ },
+ login: function(credentials) {
+
+ var deferred = $q.defer();
+ $http.post('/auth/signin', credentials).success(function(response) {
+ console.log(response);
+ deferred.resolve(response);
+ }).error(function(error) {
+
+ deferred.reject(error.message || error);
+ });
+ return deferred.promise;
+ },
+ logout: function() {
+ var deferred = $q.defer();
+ $http.get('/auth/signout').success(function(response) {
+ deferred.resolve(null);
+ }).error(function(error) {
+ deferred.reject(error.message || error);
+ });
+
+ return deferred.promise;
+ },
+ signup: function(credentials) {
+
+ var deferred = $q.defer();
+
+ $http.post('/auth/signup', credentials).success(function(response) {
+ // If successful we assign the response to the global user model
+ deferred.resolve(response);
+ }).error(function(error) {
+
+ deferred.reject(error.message || error);
+ });
+
+ return deferred.promise;
+ },
+
+ resetPassword: function(passwordDetails, token) {
+ 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);
+ });
+
+ return deferred.promise;
+ },
+
+ // Submit forgotten password account id
+ askForPasswordReset: function(credentials) {
+ var deferred = $q.defer();
+ $http.post('/auth/forgot', credentials).success(function(response) {
+ // Show user success message and clear form
+
+ deferred.resolve(response);
+
+ }).error(function(error) {
+ // Show user error message
+ deferred.reject(error.message || error);
+ });
+
+ return deferred.promise;
+ },
+
+ };
+
+ return userService;
+
+ }
+]);
diff --git a/public/modules/users/views/authentication/access-denied.client.view.html b/public/modules/users/views/authentication/access-denied.client.view.html
index 09317657..35b782aa 100644
--- a/public/modules/users/views/authentication/access-denied.client.view.html
+++ b/public/modules/users/views/authentication/access-denied.client.view.html
@@ -1,4 +1,4 @@
-
+
You need to be logged in to access this page
Login
\ No newline at end of file
diff --git a/public/modules/users/views/authentication/signin.client.view.html b/public/modules/users/views/authentication/signin.client.view.html
index e56e87de..8c69df69 100755
--- a/public/modules/users/views/authentication/signin.client.view.html
+++ b/public/modules/users/views/authentication/signin.client.view.html
@@ -1,4 +1,4 @@
-
+