Skip to content

Refactor/split auth sync async #2023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
};
}
Expand Down
98 changes: 61 additions & 37 deletions templates/app/client/components/auth(auth)/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -112,52 +111,68 @@ 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);
return is;
});
},

/**
* 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)
Expand All @@ -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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ 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) { _%>
$state.go(is ? 'main' : 'login');<% } %>
});
});
} else {
Auth.isLoggedIn(_.noop).then(is => {
Auth.isLoggedIn().then(is => {
if(is) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions templates/app/client/components/navbar/navbar.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) { _%>
Expand All @@ -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) { _%>
Expand Down