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']; } }; } diff --git a/templates/app/client/components/auth(auth)/auth.service.js b/templates/app/client/components/auth(auth)/auth.service.js index 08a91603a..2e3bb33a9 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(); } @@ -21,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(); @@ -78,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); @@ -90,18 +92,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); @@ -112,19 +111,23 @@ 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) * - * @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) + return Auth.getCurrentUser() .then(user => { var is = user.hasOwnProperty('role'); safeCb(callback)(is); @@ -132,32 +135,44 @@ 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) * * @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) + return Auth.getCurrentUser() .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; }); }, + /** + * Check if a user has a specified role or higher + * + * @param {String} role - the role to check against + * @return {Bool} + */ + hasRoleSync(role) { + return hasRole(currentUser.role, role); + }, + /** * Check if a user is an admin * (synchronous|asynchronous) @@ -170,6 +185,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 * 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; } 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) { _%>