11'use strict' ;
22// @flow
3+ class User {
4+ _id : string = '' ;
5+ name : string = '' ;
6+ email : string = '' ;
7+ role : string = '' ;
8+ $promise = undefined ;
9+ }
310
411export function AuthService ( $location , $http , $cookies , $q , appConfig , Util , User ) {
512 'ngInject' ;
613 var safeCb = Util . safeCb ;
7- var currentUser = { } ;
14+ var currentUser : User = new User ( ) ;
815 var userRoles = appConfig . userRoles || [ ] ;
916 /**
1017 * Check if userRole is >= role
@@ -25,10 +32,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
2532 * Authenticate user and save token
2633 *
2734 * @param {Object } user - login info
28- * @param {Function } callback - optional, function(error, user)
35+ * @param {Function } callback - function(error, user)
2936 * @return {Promise }
3037 */
31- login ( { email, password} , callback : Function ) {
38+ login ( { email, password} , callback ? : Function ) {
3239 return $http . post ( '/auth/local' , { email, password } )
3340 . then ( res => {
3441 $cookies . put ( 'token' , res . data . token ) ;
@@ -51,17 +58,17 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
5158 */
5259 logout ( ) {
5360 $cookies . remove ( 'token' ) ;
54- currentUser = { } ;
61+ currentUser = new User ( ) ;
5562 } ,
5663
5764 /**
5865 * Create a new user
5966 *
6067 * @param {Object } user - user info
61- * @param {Function } callback - optional, function(error, user)
68+ * @param {Function } callback - function(error, user)
6269 * @return {Promise }
6370 */
64- createUser ( user , callback ) {
71+ createUser ( user , callback ?: Function ) {
6572 return User . save ( user ,
6673 function ( data ) {
6774 $cookies . put ( 'token' , data . token ) ;
@@ -79,10 +86,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
7986 *
8087 * @param {String } oldPassword
8188 * @param {String } newPassword
82- * @param {Function } callback - optional, function(error, user)
89+ * @param {Function } callback - function(error, user)
8390 * @return {Promise }
8491 */
85- changePassword ( oldPassword , newPassword , callback ) {
92+ changePassword ( oldPassword , newPassword , callback ?: Function ) {
8693 return User . changePassword ( { id : currentUser . _id } , { oldPassword, newPassword } , function ( ) {
8794 return safeCb ( callback ) ( null ) ;
8895 } , function ( err ) {
@@ -93,10 +100,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
93100 /**
94101 * Gets all available info on a user
95102 *
96- * @param {Function } [callback] - funciton (user)
103+ * @param {Function } [callback] - function (user)
97104 * @return {Promise }
98105 */
99- getCurrentUser ( callback ) {
106+ getCurrentUser ( callback ?: Function ) {
100107 var value = currentUser . hasOwnProperty ( '$promise' )
101108 ? currentUser . $promise
102109 : currentUser ;
@@ -124,10 +131,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
124131 * Check if a user is logged in
125132 *
126133 * @param {Function } [callback] - function(is)
127- * @return {Bool| Promise }
134+ * @return {Promise }
128135 */
129- isLoggedIn ( callback ) {
130- return Auth . getCurrentUser ( )
136+ isLoggedIn ( callback ?: Function ) {
137+ return Auth . getCurrentUser ( undefined )
131138 . then ( user => {
132139 var is = user . hasOwnProperty ( 'role' ) ;
133140 safeCb ( callback ) ( is ) ;
@@ -149,10 +156,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
149156 *
150157 * @param {String } role - the role to check against
151158 * @param {Function } [callback] - function(has)
152- * @return {Bool| Promise }
159+ * @return {Promise }
153160 */
154- hasRole ( role , callback ) {
155- return Auth . getCurrentUser ( )
161+ hasRole ( role , callback ?: Function ) {
162+ return Auth . getCurrentUser ( undefined )
156163 . then ( user => {
157164 var has = user . hasOwnProperty ( 'role' )
158165 ? hasRole ( user . role , role )
0 commit comments