Skip to content

Commit 65d03fc

Browse files
kingcodyDaftMonk
authored andcommitted
feat(app-auth): Improve client-side Auth service
Changes: - getCurrentUser, isLoggedIn, and isAdmin are now sync if no arg and async with an arg - Use Error first callback signature where applicable - Remove unused arguments from Auth service - Remove isLoggedInAsync - Switch use of isLoggedInAsync to isLoggedIn - Add/Improve comments - Fix client/app/account(auth)/settings/settings.controller(js).js Breaking Changes: - Callbacks that return Errors, use 'Error first' signature Closes #456
1 parent 502be54 commit 65d03fc

File tree

4 files changed

+136
-105
lines changed

4 files changed

+136
-105
lines changed

Diff for: app/templates/client/app/app(coffee).coffee

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ angular.module '<%= scriptAppName %>', [<%= angularModules %>]
3434
.run ($rootScope, $location, Auth) ->
3535
# Redirect to login if route requires auth and you're not logged in
3636
$rootScope.$on <% if(filters.ngroute) { %>'$routeChangeStart'<% } %><% if(filters.uirouter) { %>'$stateChangeStart'<% } %>, (event, next) ->
37-
Auth.isLoggedInAsync (loggedIn) ->
37+
Auth.isLoggedIn (loggedIn) ->
3838
$location.path "/login" if next.authenticate and not loggedIn
39-
<% } %>
39+
<% } %>

Diff for: app/templates/client/app/app(js).js

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ angular.module('<%= scriptAppName %>', [<%= angularModules %>])
4646
.run(function ($rootScope, $location, Auth) {
4747
// Redirect to login if route requires auth and you're not logged in
4848
$rootScope.$on(<% if(filters.ngroute) { %>'$routeChangeStart'<% } %><% if(filters.uirouter) { %>'$stateChangeStart'<% } %>, function (event, next) {
49-
Auth.isLoggedInAsync(function(loggedIn) {
49+
Auth.isLoggedIn(function(loggedIn) {
5050
if (next.authenticate && !loggedIn) {
5151
$location.path('/login');
5252
}
5353
});
5454
});
55-
})<% } %>;
55+
})<% } %>;

Diff for: app/templates/client/components/auth(auth)/auth.service(coffee).coffee

+52-41
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
11
'use strict'
22

33
angular.module '<%= scriptAppName %>'
4-
.factory 'Auth', ($location, $rootScope, $http, User, $cookieStore, $q) ->
4+
.factory 'Auth', ($http, User, $cookieStore, $q) ->
55
currentUser = if $cookieStore.get 'token' then User.get() else {}
66

77
###
88
Authenticate user and save token
99
1010
@param {Object} user - login info
11-
@param {Function} callback - optional
11+
@param {Function} callback - optional, function(error)
1212
@return {Promise}
1313
###
1414
login: (user, callback) ->
15-
deferred = $q.defer()
1615
$http.post '/auth/local',
1716
email: user.email
1817
password: user.password
1918

20-
.success (data) ->
21-
$cookieStore.put 'token', data.token
19+
.then (res) ->
20+
$cookieStore.put 'token', res.data.token
2221
currentUser = User.get()
23-
deferred.resolve data
2422
callback?()
23+
res.data
2524

26-
.error (err) =>
25+
, (err) =>
2726
@logout()
28-
deferred.reject err
29-
callback? err
30-
31-
deferred.promise
27+
callback? err.data
28+
$q.reject err.data
3229

3330

3431
###
3532
Delete access token and user info
36-
37-
@param {Function}
3833
###
3934
logout: ->
4035
$cookieStore.remove 'token'
@@ -46,15 +41,15 @@ angular.module '<%= scriptAppName %>'
4641
Create a new user
4742
4843
@param {Object} user - user info
49-
@param {Function} callback - optional
44+
@param {Function} callback - optional, function(error, user)
5045
@return {Promise}
5146
###
5247
createUser: (user, callback) ->
5348
User.save user,
5449
(data) ->
5550
$cookieStore.put 'token', data.token
5651
currentUser = User.get()
57-
callback? user
52+
callback? null, user
5853

5954
, (err) =>
6055
@logout()
@@ -68,7 +63,7 @@ angular.module '<%= scriptAppName %>'
6863
6964
@param {String} oldPassword
7065
@param {String} newPassword
71-
@param {Function} callback - optional
66+
@param {Function} callback - optional, function(error, user)
7267
@return {Promise}
7368
###
7469
changePassword: (oldPassword, newPassword, callback) ->
@@ -79,7 +74,7 @@ angular.module '<%= scriptAppName %>'
7974
newPassword: newPassword
8075

8176
, (user) ->
82-
callback? user
77+
callback? null, user
8378

8479
, (err) ->
8580
callback? err
@@ -88,45 +83,61 @@ angular.module '<%= scriptAppName %>'
8883

8984

9085
###
91-
Gets all available info on authenticated user
86+
Gets all available info on a user
87+
(synchronous|asynchronous)
9288
93-
@return {Object} user
89+
@param {Function|*} callback - optional, funciton(user)
90+
@return {Object|Promise}
9491
###
95-
getCurrentUser: ->
96-
currentUser
92+
getCurrentUser: (callback) ->
93+
return currentUser if arguments.length is 0
9794

95+
value = if (currentUser.hasOwnProperty("$promise")) then currentUser.$promise else currentUser
96+
$q.when value
9897

99-
###
100-
Check if a user is logged in synchronously
98+
.then (user) ->
99+
callback? user
100+
user
101101

102-
@return {Boolean}
103-
###
104-
isLoggedIn: ->
105-
currentUser.hasOwnProperty 'role'
102+
, ->
103+
callback? {}
104+
{}
106105

107106

108107
###
109-
Waits for currentUser to resolve before checking if user is logged in
108+
Check if a user is logged in
109+
(synchronous|asynchronous)
110+
111+
@param {Function|*} callback - optional, function(is)
112+
@return {Bool|Promise}
110113
###
111-
isLoggedInAsync: (callback) ->
112-
if currentUser.hasOwnProperty '$promise'
113-
currentUser.$promise.then ->
114-
callback? true
115-
return
116-
.catch ->
117-
callback? false
118-
return
114+
isLoggedIn: (callback) ->
115+
return currentUser.hasOwnProperty("role") if arguments.length is 0
116+
117+
@getCurrentUser null
118+
119+
.then (user) ->
120+
is_ = user.hasOwnProperty("role")
121+
callback? is_
122+
is_
119123

120-
else
121-
callback? currentUser.hasOwnProperty 'role'
122124

123125
###
124126
Check if a user is an admin
127+
(synchronous|asynchronous)
125128
126-
@return {Boolean}
129+
@param {Function|*} callback - optional, function(is)
130+
@return {Bool|Promise}
127131
###
128-
isAdmin: ->
129-
currentUser.role is 'admin'
132+
isAdmin: (callback) ->
133+
return currentUser.role is "admin" if arguments_.length is 0
134+
135+
@getCurrentUser null
136+
137+
.then (user) ->
138+
is_ = user.role is "admin"
139+
callback? is_
140+
is_
130141

131142

132143
###

0 commit comments

Comments
 (0)