@@ -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 *
0 commit comments