From 71b97dfbfdffab098501e87eb3768521214cd68c Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Tue, 28 Jun 2016 10:36:51 -0400 Subject: [PATCH 1/6] feat(client:auth.service): create sync methods --- .../components/auth(auth)/auth.service.js | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index 08a91603a..203aae574 100644 --- a/templates/app/client/components/auth(auth)/auth.service.js +++ b/templates/app/client/components/auth(auth)/auth.service.js @@ -112,6 +112,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use }); }, + /** + * Gets all available info on a user + * + * @return {Object} + */ + getCurrentUserSync() { + return currentUser; + }, + /** * Check if a user is logged in * (synchronous|asynchronous) @@ -132,6 +141,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use }); }, + /** + * Check if a user is logged in + * + * @return {Bool} + */ + isLoggedInSync() { + return currentUser.hasOwnProperty('role'); + }, + /** * Check if a user has a specified role or higher * (synchronous|asynchronous) @@ -158,6 +176,20 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use }); }, + /** + * Check if a user has a specified role or higher + * + * @param {String} role - the role to check against + * @return {Bool} + */ + hasRoleSync(role) { + var hasRole = function(r, h) { + return userRoles.indexOf(r) >= userRoles.indexOf(h); + }; + + return hasRole(currentUser.role, role); + }, + /** * Check if a user is an admin * (synchronous|asynchronous) @@ -170,6 +202,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use .apply(Auth, [].concat.apply(['admin'], arguments)); }, + /** + * Check if a user is an admin + * + * @return {Bool} + */ + isAdminSync() { + return Auth.hasRoleSync('admin'); + }, + /** * Get auth token * From f5deecb9f7df7f98441f7c6eb9b62a23bbc2d9d6 Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Tue, 28 Jun 2016 10:57:46 -0400 Subject: [PATCH 2/6] refactor(client:navbar): refactor to use new sync auth methods --- .../app/client/components/navbar/navbar.component.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/templates/app/client/components/navbar/navbar.component.js b/templates/app/client/components/navbar/navbar.component.js index d46af76b3..1e9c3ad9d 100644 --- a/templates/app/client/components/navbar/navbar.component.js +++ b/templates/app/client/components/navbar/navbar.component.js @@ -6,7 +6,7 @@ export class NavbarComponent { 'title': 'Home', <% if (filters.uirouter) { %>'state': 'main'<% } else { %>'link': '/'<% } %> }]; - + isCollapsed = true; //end-non-standard <%_ if(filters.ngroute || filters.auth) { _%> @@ -17,9 +17,9 @@ export class NavbarComponent { this.$location = $location; <%_ } _%> <%_ if (filters.auth) { _%> - this.isLoggedIn = Auth.isLoggedIn; - this.isAdmin = Auth.isAdmin; - this.getCurrentUser = Auth.getCurrentUser; + this.isLoggedIn = Auth.isLoggedInSync; + this.isAdmin = Auth.isAdminSync; + this.getCurrentUser = Auth.getCurrentUserSync; <%_ } _%> }<% } %> <%_ if(!filters.uirouter) { _%> From 427cbaff143260c02076ca82fd70bcfc857a655d Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Tue, 28 Jun 2016 11:18:58 -0400 Subject: [PATCH 3/6] refactor(client:auth.service): clean up async methods --- .../components/auth(auth)/auth.service.js | 51 ++++++++----------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index 203aae574..794b10738 100644 --- a/templates/app/client/components/auth(auth)/auth.service.js +++ b/templates/app/client/components/auth(auth)/auth.service.js @@ -6,8 +6,16 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use var safeCb = Util.safeCb; var currentUser = {}; var userRoles = appConfig.userRoles || []; + /** + * Check if userRole is >= role + * @param {String} userRole - role of current user + * @param {String} role - role to check against + */ + var hasRole = function(userRole, role) { + return userRoles.indexOf(userRole) >= userRoles.indexOf(role); + }; - if ($cookies.get('token') && $location.path() !== '/logout') { + if($cookies.get('token') && $location.path() !== '/logout') { currentUser = User.get(); } @@ -90,18 +98,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use /** * Gets all available info on a user - * (synchronous|asynchronous) * - * @param {Function|*} callback - optional, funciton(user) - * @return {Object|Promise} + * @param {Function} [callback] - funciton(user) + * @return {Promise} */ getCurrentUser(callback) { - if (arguments.length === 0) { - return currentUser; - } + var value = currentUser.hasOwnProperty('$promise') + ? currentUser.$promise + : currentUser; - var value = (currentUser.hasOwnProperty('$promise')) ? - currentUser.$promise : currentUser; return $q.when(value) .then(user => { safeCb(callback)(user); @@ -123,16 +128,11 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use /** * Check if a user is logged in - * (synchronous|asynchronous) * - * @param {Function|*} callback - optional, function(is) + * @param {Function} [callback] - function(is) * @return {Bool|Promise} */ isLoggedIn(callback) { - if (arguments.length === 0) { - return currentUser.hasOwnProperty('role'); - } - return Auth.getCurrentUser(null) .then(user => { var is = user.hasOwnProperty('role'); @@ -152,25 +152,18 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use /** * Check if a user has a specified role or higher - * (synchronous|asynchronous) * * @param {String} role - the role to check against - * @param {Function|*} callback - optional, function(has) + * @param {Function} [callback] - function(has) * @return {Bool|Promise} */ hasRole(role, callback) { - var hasRole = function(r, h) { - return userRoles.indexOf(r) >= userRoles.indexOf(h); - }; - - if (arguments.length < 2) { - return hasRole(currentUser.role, role); - } - return Auth.getCurrentUser(null) .then(user => { - var has = (user.hasOwnProperty('role')) ? - hasRole(user.role, role) : false; + var has = user.hasOwnProperty('role') + ? hasRole(user.role, role) + : false; + safeCb(callback)(has); return has; }); @@ -183,10 +176,6 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * @return {Bool} */ hasRoleSync(role) { - var hasRole = function(r, h) { - return userRoles.indexOf(r) >= userRoles.indexOf(h); - }; - return hasRole(currentUser.role, role); }, From 4ac72354b8b87569a00e51df02cc8f08625ec2cb Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Mon, 27 Jun 2016 16:21:31 -0400 Subject: [PATCH 4/6] fix(gen): add port var assignment bad merge? --- src/generators/app/index.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/generators/app/index.js b/src/generators/app/index.js index e24584103..bba173d6b 100644 --- a/src/generators/app/index.js +++ b/src/generators/app/index.js @@ -132,6 +132,11 @@ export class Generator extends Base { this.config.forceSave(); } }); + }, + assignPorts() { + this.devPort = this.options['dev-port']; + this.debugPort = this.options['debug-port']; + this.prodPort = this.options['prod-port']; } }; } From 95f25afecc8a0cc44d1ad94df7a18bc06df72213 Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Tue, 28 Jun 2016 12:48:01 -0400 Subject: [PATCH 5/6] style(client:auth.service): use object shorthand --- .../app/client/components/auth(auth)/auth.service.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index 794b10738..04607ac50 100644 --- a/templates/app/client/components/auth(auth)/auth.service.js +++ b/templates/app/client/components/auth(auth)/auth.service.js @@ -29,10 +29,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * @return {Promise} */ login({email, password}, callback: Function) { - return $http.post('/auth/local', { - email: email, - password: password - }) + return $http.post('/auth/local', { email, password }) .then(res => { $cookies.put('token', res.data.token); currentUser = User.get(); @@ -86,10 +83,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * @return {Promise} */ changePassword(oldPassword, newPassword, callback) { - return User.changePassword({ id: currentUser._id }, { - oldPassword: oldPassword, - newPassword: newPassword - }, function() { + return User.changePassword({ id: currentUser._id }, { oldPassword, newPassword }, function() { return safeCb(callback)(null); }, function(err) { return safeCb(callback)(err); From a5d489c734e03eeb6f4493d6ea4666fcdbf7080d Mon Sep 17 00:00:00 2001 From: Andrew Koroluk Date: Tue, 28 Jun 2016 12:52:28 -0400 Subject: [PATCH 6/6] refactor(client): remove no-longer-needed extra async auth function params --- templates/app/client/components/auth(auth)/auth.service.js | 4 ++-- .../app/client/components/auth(auth)/router.decorator.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index 04607ac50..2e3bb33a9 100644 --- a/templates/app/client/components/auth(auth)/auth.service.js +++ b/templates/app/client/components/auth(auth)/auth.service.js @@ -127,7 +127,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * @return {Bool|Promise} */ isLoggedIn(callback) { - return Auth.getCurrentUser(null) + return Auth.getCurrentUser() .then(user => { var is = user.hasOwnProperty('role'); safeCb(callback)(is); @@ -152,7 +152,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use * @return {Bool|Promise} */ hasRole(role, callback) { - return Auth.getCurrentUser(null) + return Auth.getCurrentUser() .then(user => { var has = user.hasOwnProperty('role') ? hasRole(user.role, role) diff --git a/templates/app/client/components/auth(auth)/router.decorator.js b/templates/app/client/components/auth(auth)/router.decorator.js index 5c135293b..ea205ec7d 100644 --- a/templates/app/client/components/auth(auth)/router.decorator.js +++ b/templates/app/client/components/auth(auth)/router.decorator.js @@ -10,13 +10,13 @@ export function routerDecorator($rootScope<% if(filters.ngroute) { %>, $location } if(typeof next.authenticate === 'string') { - Auth.hasRole(next.authenticate, _.noop).then(has => { + Auth.hasRole(next.authenticate).then(has => { if(has) { return; } event.preventDefault(); - return Auth.isLoggedIn(_.noop).then(is => { + return Auth.isLoggedIn().then(is => { <%_ if(filters.ngroute) { _%> $location.path(is ? '/' : '/login');<% } %> <%_ if(filters.uirouter) { _%> @@ -24,7 +24,7 @@ export function routerDecorator($rootScope<% if(filters.ngroute) { %>, $location }); }); } else { - Auth.isLoggedIn(_.noop).then(is => { + Auth.isLoggedIn().then(is => { if(is) { return; }