@@ -6,8 +6,16 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
6
6
var safeCb = Util . safeCb ;
7
7
var currentUser = { } ;
8
8
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
+ } ;
9
17
10
- if ( $cookies . get ( 'token' ) && $location . path ( ) !== '/logout' ) {
18
+ if ( $cookies . get ( 'token' ) && $location . path ( ) !== '/logout' ) {
11
19
currentUser = User . get ( ) ;
12
20
}
13
21
@@ -21,10 +29,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
21
29
* @return {Promise }
22
30
*/
23
31
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 } )
28
33
. then ( res => {
29
34
$cookies . put ( 'token' , res . data . token ) ;
30
35
currentUser = User . get ( ) ;
@@ -78,10 +83,7 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
78
83
* @return {Promise }
79
84
*/
80
85
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 ( ) {
85
87
return safeCb ( callback ) ( null ) ;
86
88
} , function ( err ) {
87
89
return safeCb ( callback ) ( err ) ;
@@ -90,18 +92,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
90
92
91
93
/**
92
94
* Gets all available info on a user
93
- * (synchronous|asynchronous)
94
95
*
95
- * @param {Function|* } callback - optional, funciton(user)
96
- * @return {Object| Promise }
96
+ * @param {Function } [ callback] - funciton(user)
97
+ * @return {Promise }
97
98
*/
98
99
getCurrentUser ( callback ) {
99
- if ( arguments . length === 0 ) {
100
- return currentUser ;
101
- }
100
+ var value = currentUser . hasOwnProperty ( '$promise' )
101
+ ? currentUser . $promise
102
+ : currentUser ;
102
103
103
- var value = ( currentUser . hasOwnProperty ( '$promise' ) ) ?
104
- currentUser . $promise : currentUser ;
105
104
return $q . when ( value )
106
105
. then ( user => {
107
106
safeCb ( callback ) ( user ) ;
@@ -112,52 +111,68 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
112
111
} ) ;
113
112
} ,
114
113
114
+ /**
115
+ * Gets all available info on a user
116
+ *
117
+ * @return {Object }
118
+ */
119
+ getCurrentUserSync ( ) {
120
+ return currentUser ;
121
+ } ,
122
+
115
123
/**
116
124
* Check if a user is logged in
117
- * (synchronous|asynchronous)
118
125
*
119
- * @param {Function|* } callback - optional, function(is)
126
+ * @param {Function } [ callback] - function(is)
120
127
* @return {Bool|Promise }
121
128
*/
122
129
isLoggedIn ( callback ) {
123
- if ( arguments . length === 0 ) {
124
- return currentUser . hasOwnProperty ( 'role' ) ;
125
- }
126
-
127
- return Auth . getCurrentUser ( null )
130
+ return Auth . getCurrentUser ( )
128
131
. then ( user => {
129
132
var is = user . hasOwnProperty ( 'role' ) ;
130
133
safeCb ( callback ) ( is ) ;
131
134
return is ;
132
135
} ) ;
133
136
} ,
134
137
138
+ /**
139
+ * Check if a user is logged in
140
+ *
141
+ * @return {Bool }
142
+ */
143
+ isLoggedInSync ( ) {
144
+ return currentUser . hasOwnProperty ( 'role' ) ;
145
+ } ,
146
+
135
147
/**
136
148
* Check if a user has a specified role or higher
137
- * (synchronous|asynchronous)
138
149
*
139
150
* @param {String } role - the role to check against
140
- * @param {Function|* } callback - optional, function(has)
151
+ * @param {Function } [ callback] - function(has)
141
152
* @return {Bool|Promise }
142
153
*/
143
154
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 ( )
153
156
. 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
+
156
161
safeCb ( callback ) ( has ) ;
157
162
return has ;
158
163
} ) ;
159
164
} ,
160
165
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
+
161
176
/**
162
177
* Check if a user is an admin
163
178
* (synchronous|asynchronous)
@@ -170,6 +185,15 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
170
185
. apply ( Auth , [ ] . concat . apply ( [ 'admin' ] , arguments ) ) ;
171
186
} ,
172
187
188
+ /**
189
+ * Check if a user is an admin
190
+ *
191
+ * @return {Bool }
192
+ */
193
+ isAdminSync ( ) {
194
+ return Auth . hasRoleSync ( 'admin' ) ;
195
+ } ,
196
+
173
197
/**
174
198
* Get auth token
175
199
*
0 commit comments