1
1
'use strict' ;
2
2
// @flow
3
+ class User {
4
+ _id : string = '' ;
5
+ name : string = '' ;
6
+ email : string = '' ;
7
+ role : string = '' ;
8
+ $promise = undefined ;
9
+ }
3
10
4
11
export function AuthService ( $location , $http , $cookies , $q , appConfig , Util , User ) {
5
12
'ngInject' ;
6
13
var safeCb = Util . safeCb ;
7
- var currentUser = { } ;
14
+ var currentUser : User = new User ( ) ;
8
15
var userRoles = appConfig . userRoles || [ ] ;
9
16
/**
10
17
* Check if userRole is >= role
@@ -25,10 +32,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
25
32
* Authenticate user and save token
26
33
*
27
34
* @param {Object } user - login info
28
- * @param {Function } callback - optional, function(error, user)
35
+ * @param {Function } callback - function(error, user)
29
36
* @return {Promise }
30
37
*/
31
- login ( { email, password} , callback : Function ) {
38
+ login ( { email, password} , callback ? : Function ) {
32
39
return $http . post ( '/auth/local' , { email, password } )
33
40
. then ( res => {
34
41
$cookies . put ( 'token' , res . data . token ) ;
@@ -51,17 +58,17 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
51
58
*/
52
59
logout ( ) {
53
60
$cookies . remove ( 'token' ) ;
54
- currentUser = { } ;
61
+ currentUser = new User ( ) ;
55
62
} ,
56
63
57
64
/**
58
65
* Create a new user
59
66
*
60
67
* @param {Object } user - user info
61
- * @param {Function } callback - optional, function(error, user)
68
+ * @param {Function } callback - function(error, user)
62
69
* @return {Promise }
63
70
*/
64
- createUser ( user , callback ) {
71
+ createUser ( user , callback ?: Function ) {
65
72
return User . save ( user ,
66
73
function ( data ) {
67
74
$cookies . put ( 'token' , data . token ) ;
@@ -79,10 +86,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
79
86
*
80
87
* @param {String } oldPassword
81
88
* @param {String } newPassword
82
- * @param {Function } callback - optional, function(error, user)
89
+ * @param {Function } callback - function(error, user)
83
90
* @return {Promise }
84
91
*/
85
- changePassword ( oldPassword , newPassword , callback ) {
92
+ changePassword ( oldPassword , newPassword , callback ?: Function ) {
86
93
return User . changePassword ( { id : currentUser . _id } , { oldPassword, newPassword } , function ( ) {
87
94
return safeCb ( callback ) ( null ) ;
88
95
} , function ( err ) {
@@ -93,10 +100,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
93
100
/**
94
101
* Gets all available info on a user
95
102
*
96
- * @param {Function } [callback] - funciton (user)
103
+ * @param {Function } [callback] - function (user)
97
104
* @return {Promise }
98
105
*/
99
- getCurrentUser ( callback ) {
106
+ getCurrentUser ( callback ?: Function ) {
100
107
var value = currentUser . hasOwnProperty ( '$promise' )
101
108
? currentUser . $promise
102
109
: currentUser ;
@@ -124,10 +131,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
124
131
* Check if a user is logged in
125
132
*
126
133
* @param {Function } [callback] - function(is)
127
- * @return {Bool| Promise }
134
+ * @return {Promise }
128
135
*/
129
- isLoggedIn ( callback ) {
130
- return Auth . getCurrentUser ( )
136
+ isLoggedIn ( callback ?: Function ) {
137
+ return Auth . getCurrentUser ( undefined )
131
138
. then ( user => {
132
139
var is = user . hasOwnProperty ( 'role' ) ;
133
140
safeCb ( callback ) ( is ) ;
@@ -149,10 +156,10 @@ export function AuthService($location, $http, $cookies, $q, appConfig, Util, Use
149
156
*
150
157
* @param {String } role - the role to check against
151
158
* @param {Function } [callback] - function(has)
152
- * @return {Bool| Promise }
159
+ * @return {Promise }
153
160
*/
154
- hasRole ( role , callback ) {
155
- return Auth . getCurrentUser ( )
161
+ hasRole ( role , callback ?: Function ) {
162
+ return Auth . getCurrentUser ( undefined )
156
163
. then ( user => {
157
164
var has = user . hasOwnProperty ( 'role' )
158
165
? hasRole ( user . role , role )
0 commit comments