Skip to content

Commit 1ddf83d

Browse files
authored
Merge pull request #2023 from angular-fullstack/refactor/split-auth-sync-async
* feat(client:auth.service): create sync methods * refactor(client:navbar): refactor to use new sync auth methods * refactor(client:auth.service): clean up async methods * fix(gen): add port var assignment bad merge? * style(client:auth.service): use object shorthand * refactor(client): remove no-longer-needed extra async auth function params [skip ci]
2 parents bf78a58 + a5d489c commit 1ddf83d

File tree

4 files changed

+73
-44
lines changed

4 files changed

+73
-44
lines changed

src/generators/app/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ export class Generator extends Base {
132132
this.config.forceSave();
133133
}
134134
});
135+
},
136+
assignPorts() {
137+
this.devPort = this.options['dev-port'];
138+
this.debugPort = this.options['debug-port'];
139+
this.prodPort = this.options['prod-port'];
135140
}
136141
};
137142
}

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

Lines changed: 61 additions & 37 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

@@ -21,10 +29,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
2129
* @return {Promise}
2230
*/
2331
login({email, password}, callback: Function) {
24-
return $http.post('/auth/local', {
25-
email: email,
26-
password: password
27-
})
32+
return $http.post('/auth/local', { email, password })
2833
.then(res => {
2934
$cookies.put('token', res.data.token);
3035
currentUser = User.get();
@@ -78,10 +83,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
7883
* @return {Promise}
7984
*/
8085
changePassword(oldPassword, newPassword, callback) {
81-
return User.changePassword({ id: currentUser._id }, {
82-
oldPassword: oldPassword,
83-
newPassword: newPassword
84-
}, function() {
86+
return User.changePassword({ id: currentUser._id }, { oldPassword, newPassword }, function() {
8587
return safeCb(callback)(null);
8688
}, function(err) {
8789
return safeCb(callback)(err);
@@ -90,18 +92,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
9092

9193
/**
9294
* Gets all available info on a user
93-
* (synchronous|asynchronous)
9495
*
95-
* @param {Function|*} callback - optional, funciton(user)
96-
* @return {Object|Promise}
96+
* @param {Function} [callback] - funciton(user)
97+
* @return {Promise}
9798
*/
9899
getCurrentUser(callback) {
99-
if (arguments.length === 0) {
100-
return currentUser;
101-
}
100+
var value = currentUser.hasOwnProperty('$promise')
101+
? currentUser.$promise
102+
: currentUser;
102103

103-
var value = (currentUser.hasOwnProperty('$promise')) ?
104-
currentUser.$promise : currentUser;
105104
return $q.when(value)
106105
.then(user => {
107106
safeCb(callback)(user);
@@ -112,52 +111,68 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
112111
});
113112
},
114113

114+
/**
115+
* Gets all available info on a user
116+
*
117+
* @return {Object}
118+
*/
119+
getCurrentUserSync() {
120+
return currentUser;
121+
},
122+
115123
/**
116124
* Check if a user is logged in
117-
* (synchronous|asynchronous)
118125
*
119-
* @param {Function|*} callback - optional, function(is)
126+
* @param {Function} [callback] - function(is)
120127
* @return {Bool|Promise}
121128
*/
122129
isLoggedIn(callback) {
123-
if (arguments.length === 0) {
124-
return currentUser.hasOwnProperty('role');
125-
}
126-
127-
return Auth.getCurrentUser(null)
130+
return Auth.getCurrentUser()
128131
.then(user => {
129132
var is = user.hasOwnProperty('role');
130133
safeCb(callback)(is);
131134
return is;
132135
});
133136
},
134137

138+
/**
139+
* Check if a user is logged in
140+
*
141+
* @return {Bool}
142+
*/
143+
isLoggedInSync() {
144+
return currentUser.hasOwnProperty('role');
145+
},
146+
135147
/**
136148
* Check if a user has a specified role or higher
137-
* (synchronous|asynchronous)
138149
*
139150
* @param {String} role - the role to check against
140-
* @param {Function|*} callback - optional, function(has)
151+
* @param {Function} [callback] - function(has)
141152
* @return {Bool|Promise}
142153
*/
143154
hasRole(role, callback) {
144-
var hasRole = function(r, h) {
145-
return userRoles.indexOf(r) >= userRoles.indexOf(h);
146-
};
147-
148-
if (arguments.length < 2) {
149-
return hasRole(currentUser.role, role);
150-
}
151-
152-
return Auth.getCurrentUser(null)
155+
return Auth.getCurrentUser()
153156
.then(user => {
154-
var has = (user.hasOwnProperty('role')) ?
155-
hasRole(user.role, role) : false;
157+
var has = user.hasOwnProperty('role')
158+
? hasRole(user.role, role)
159+
: false;
160+
156161
safeCb(callback)(has);
157162
return has;
158163
});
159164
},
160165

166+
/**
167+
* Check if a user has a specified role or higher
168+
*
169+
* @param {String} role - the role to check against
170+
* @return {Bool}
171+
*/
172+
hasRoleSync(role) {
173+
return hasRole(currentUser.role, role);
174+
},
175+
161176
/**
162177
* Check if a user is an admin
163178
* (synchronous|asynchronous)
@@ -170,6 +185,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
170185
.apply(Auth, [].concat.apply(['admin'], arguments));
171186
},
172187

188+
/**
189+
* Check if a user is an admin
190+
*
191+
* @return {Bool}
192+
*/
193+
isAdminSync() {
194+
return Auth.hasRoleSync('admin');
195+
},
196+
173197
/**
174198
* Get auth token
175199
*

templates/app/client/components/auth(auth)/router.decorator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ export function routerDecorator($rootScope<% if(filters.ngroute) { %>, $location
1010
}
1111

1212
if(typeof next.authenticate === 'string') {
13-
Auth.hasRole(next.authenticate, _.noop).then(has => {
13+
Auth.hasRole(next.authenticate).then(has => {
1414
if(has) {
1515
return;
1616
}
1717

1818
event.preventDefault();
19-
return Auth.isLoggedIn(_.noop).then(is => {
19+
return Auth.isLoggedIn().then(is => {
2020
<%_ if(filters.ngroute) { _%>
2121
$location.path(is ? '/' : '/login');<% } %>
2222
<%_ if(filters.uirouter) { _%>
2323
$state.go(is ? 'main' : 'login');<% } %>
2424
});
2525
});
2626
} else {
27-
Auth.isLoggedIn(_.noop).then(is => {
27+
Auth.isLoggedIn().then(is => {
2828
if(is) {
2929
return;
3030
}

templates/app/client/components/navbar/navbar.component.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class NavbarComponent {
66
'title': 'Home',
77
<% if (filters.uirouter) { %>'state': 'main'<% } else { %>'link': '/'<% } %>
88
}];
9-
9+
1010
isCollapsed = true;
1111
//end-non-standard
1212
<%_ if(filters.ngroute || filters.auth) { _%>
@@ -17,9 +17,9 @@ export class NavbarComponent {
1717
this.$location = $location;
1818
<%_ } _%>
1919
<%_ if (filters.auth) { _%>
20-
this.isLoggedIn = Auth.isLoggedIn;
21-
this.isAdmin = Auth.isAdmin;
22-
this.getCurrentUser = Auth.getCurrentUser;
20+
this.isLoggedIn = Auth.isLoggedInSync;
21+
this.isAdmin = Auth.isAdminSync;
22+
this.getCurrentUser = Auth.getCurrentUserSync;
2323
<%_ } _%>
2424
}<% } %>
2525
<%_ if(!filters.uirouter) { _%>

0 commit comments

Comments
 (0)