Skip to content

Commit 427cbaf

Browse files
committed
refactor(client:auth.service): clean up async methods
1 parent f5deecb commit 427cbaf

File tree

1 file changed

+20
-31
lines changed

1 file changed

+20
-31
lines changed

templates/app/client/components/auth(auth)/auth.service.js

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
66
var safeCb = Util.safeCb;
77
var currentUser = {};
88
var userRoles = appConfig.userRoles || [];
9+
/**
10+
* Check if userRole is >= role
11+
* @param {String} userRole - role of current user
12+
* @param {String} role - role to check against
13+
*/
14+
var hasRole = function(userRole, role) {
15+
return userRoles.indexOf(userRole) >= userRoles.indexOf(role);
16+
};
917

10-
if ($cookies.get('token') && $location.path() !== '/logout') {
18+
if($cookies.get('token') && $location.path() !== '/logout') {
1119
currentUser = User.get();
1220
}
1321

@@ -90,18 +98,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
9098

9199
/**
92100
* Gets all available info on a user
93-
* (synchronous|asynchronous)
94101
*
95-
* @param {Function|*} callback - optional, funciton(user)
96-
* @return {Object|Promise}
102+
* @param {Function} [callback] - funciton(user)
103+
* @return {Promise}
97104
*/
98105
getCurrentUser(callback) {
99-
if (arguments.length === 0) {
100-
return currentUser;
101-
}
106+
var value = currentUser.hasOwnProperty('$promise')
107+
? currentUser.$promise
108+
: currentUser;
102109

103-
var value = (currentUser.hasOwnProperty('$promise')) ?
104-
currentUser.$promise : currentUser;
105110
return $q.when(value)
106111
.then(user => {
107112
safeCb(callback)(user);
@@ -123,16 +128,11 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
123128

124129
/**
125130
* Check if a user is logged in
126-
* (synchronous|asynchronous)
127131
*
128-
* @param {Function|*} callback - optional, function(is)
132+
* @param {Function} [callback] - function(is)
129133
* @return {Bool|Promise}
130134
*/
131135
isLoggedIn(callback) {
132-
if (arguments.length === 0) {
133-
return currentUser.hasOwnProperty('role');
134-
}
135-
136136
return Auth.getCurrentUser(null)
137137
.then(user => {
138138
var is = user.hasOwnProperty('role');
@@ -152,25 +152,18 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
152152

153153
/**
154154
* Check if a user has a specified role or higher
155-
* (synchronous|asynchronous)
156155
*
157156
* @param {String} role - the role to check against
158-
* @param {Function|*} callback - optional, function(has)
157+
* @param {Function} [callback] - function(has)
159158
* @return {Bool|Promise}
160159
*/
161160
hasRole(role, callback) {
162-
var hasRole = function(r, h) {
163-
return userRoles.indexOf(r) >= userRoles.indexOf(h);
164-
};
165-
166-
if (arguments.length < 2) {
167-
return hasRole(currentUser.role, role);
168-
}
169-
170161
return Auth.getCurrentUser(null)
171162
.then(user => {
172-
var has = (user.hasOwnProperty('role')) ?
173-
hasRole(user.role, role) : false;
163+
var has = user.hasOwnProperty('role')
164+
? hasRole(user.role, role)
165+
: false;
166+
174167
safeCb(callback)(has);
175168
return has;
176169
});
@@ -183,10 +176,6 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
183176
* @return {Bool}
184177
*/
185178
hasRoleSync(role) {
186-
var hasRole = function(r, h) {
187-
return userRoles.indexOf(r) >= userRoles.indexOf(h);
188-
};
189-
190179
return hasRole(currentUser.role, role);
191180
},
192181

0 commit comments

Comments
 (0)