@@ -3,7 +3,7 @@ import { ActivatedRoute, Router, CanActivate, ActivatedRouteSnapshot, RouterStat
3
3
import { HttpClient , HttpResponse , HttpErrorResponse } from '@angular/common/http' ;
4
4
import { isPlatformServer } from '@angular/common' ;
5
5
6
- import { Observable , fromEvent , interval } from 'rxjs' ;
6
+ import { Observable , fromEvent , interval , BehaviorSubject } from 'rxjs' ;
7
7
import { pluck , filter , share , finalize } from 'rxjs/operators' ;
8
8
9
9
import { ANGULAR_TOKEN_OPTIONS } from './angular-token.token' ;
@@ -28,19 +28,19 @@ import {
28
28
export class AngularTokenService implements CanActivate {
29
29
30
30
get currentUserType ( ) : string {
31
- if ( this . userType != null ) {
32
- return this . userType . name ;
31
+ if ( this . userType . value != null ) {
32
+ return this . userType . value . name ;
33
33
} else {
34
34
return undefined ;
35
35
}
36
36
}
37
37
38
38
get currentUserData ( ) : UserData {
39
- return this . userData ;
39
+ return this . userData . value ;
40
40
}
41
41
42
42
get currentAuthData ( ) : AuthData {
43
- return this . authData ;
43
+ return this . authData . value ;
44
44
}
45
45
46
46
get apiBase ( ) : string {
@@ -58,9 +58,9 @@ export class AngularTokenService implements CanActivate {
58
58
}
59
59
60
60
private options : AngularTokenOptions ;
61
- private userType : UserType ;
62
- private authData : AuthData ;
63
- private userData : UserData ;
61
+ public userType : BehaviorSubject < UserType > = new BehaviorSubject < UserType > ( null ) ;
62
+ public authData : BehaviorSubject < AuthData > = new BehaviorSubject < AuthData > ( null ) ;
63
+ public userData : BehaviorSubject < UserData > = new BehaviorSubject < UserData > ( null ) ;
64
64
private global : Window | any ;
65
65
66
66
private localStorage : Storage | any = { } ;
@@ -138,7 +138,11 @@ export class AngularTokenService implements CanActivate {
138
138
}
139
139
140
140
userSignedIn ( ) : boolean {
141
- return ! ! this . authData ;
141
+ if ( this . authData . value == null ) {
142
+ return false ;
143
+ } else {
144
+ return true ;
145
+ }
142
146
}
143
147
144
148
canActivate ( route : ActivatedRouteSnapshot , state : RouterStateSnapshot ) : boolean {
@@ -175,9 +179,9 @@ export class AngularTokenService implements CanActivate {
175
179
registerData = Object . assign ( { } , registerData ) ;
176
180
177
181
if ( registerData . userType == null ) {
178
- this . userType = null ;
182
+ this . userType . next ( null ) ;
179
183
} else {
180
- this . userType = this . getUserTypeByName ( registerData . userType ) ;
184
+ this . userType . next ( this . getUserTypeByName ( registerData . userType ) ) ;
181
185
delete registerData . userType ;
182
186
}
183
187
@@ -211,7 +215,7 @@ export class AngularTokenService implements CanActivate {
211
215
212
216
// Sign in request and set storage
213
217
signIn ( signInData : SignInData , additionalData ?: any ) : Observable < ApiResponse > {
214
- this . userType = ( signInData . userType == null ) ? null : this . getUserTypeByName ( signInData . userType ) ;
218
+ this . userType . next ( ( signInData . userType == null ) ? null : this . getUserTypeByName ( signInData . userType ) ) ;
215
219
216
220
const body = {
217
221
[ this . options . loginField ] : signInData . login ,
@@ -226,7 +230,7 @@ export class AngularTokenService implements CanActivate {
226
230
this . getServerPath ( ) + this . options . signInPath , body
227
231
) . pipe ( share ( ) ) ;
228
232
229
- observ . subscribe ( res => this . userData = res . data ) ;
233
+ observ . subscribe ( res => this . userData . next ( res . data ) ) ;
230
234
231
235
return observ ;
232
236
}
@@ -280,9 +284,9 @@ export class AngularTokenService implements CanActivate {
280
284
this . localStorage . removeItem ( 'tokenType' ) ;
281
285
this . localStorage . removeItem ( 'uid' ) ;
282
286
283
- this . authData = null ;
284
- this . userType = null ;
285
- this . userData = null ;
287
+ this . authData . next ( null ) ;
288
+ this . userType . next ( null ) ;
289
+ this . userData . next ( null ) ;
286
290
}
287
291
)
288
292
) ;
@@ -295,7 +299,7 @@ export class AngularTokenService implements CanActivate {
295
299
) . pipe ( share ( ) ) ;
296
300
297
301
observ . subscribe (
298
- ( res ) => this . userData = res . data ,
302
+ ( res ) => this . userData . next ( res . data ) ,
299
303
( error ) => {
300
304
if ( error . status === 401 && this . options . signOutFailedValidate ) {
301
305
this . signOut ( ) ;
@@ -309,7 +313,7 @@ export class AngularTokenService implements CanActivate {
309
313
updatePassword ( updatePasswordData : UpdatePasswordData ) : Observable < ApiResponse > {
310
314
311
315
if ( updatePasswordData . userType != null ) {
312
- this . userType = this . getUserTypeByName ( updatePasswordData . userType ) ;
316
+ this . userType . next ( this . getUserTypeByName ( updatePasswordData . userType ) ) ;
313
317
}
314
318
315
319
let args : any ;
@@ -338,7 +342,9 @@ export class AngularTokenService implements CanActivate {
338
342
// Reset password request
339
343
resetPassword ( resetPasswordData : ResetPasswordData ) : Observable < ApiResponse > {
340
344
341
- this . userType = ( resetPasswordData . userType == null ) ? null : this . getUserTypeByName ( resetPasswordData . userType ) ;
345
+ this . userType . next (
346
+ ( resetPasswordData . userType == null ) ? null : this . getUserTypeByName ( resetPasswordData . userType )
347
+ ) ;
342
348
343
349
const body = {
344
350
[ this . options . loginField ] : resetPasswordData . login ,
@@ -356,7 +362,7 @@ export class AngularTokenService implements CanActivate {
356
362
*/
357
363
358
364
private getUserPath ( ) : string {
359
- return ( this . userType == null ) ? '' : this . userType . path + '/' ;
365
+ return ( this . userType . value == null ) ? '' : this . userType . value . path + '/' ;
360
366
}
361
367
362
368
private getApiPath ( ) : string {
@@ -396,8 +402,8 @@ export class AngularTokenService implements CanActivate {
396
402
url += `?omniauth_window_type=${ windowType } ` ;
397
403
url += `&auth_origin_url=${ encodeURIComponent ( callbackUrl ) } ` ;
398
404
399
- if ( this . userType != null ) {
400
- url += `&resource_class=${ this . userType . name } ` ;
405
+ if ( this . userType . value != null ) {
406
+ url += `&resource_class=${ this . userType . value . name } ` ;
401
407
}
402
408
403
409
return url ;
@@ -416,7 +422,7 @@ export class AngularTokenService implements CanActivate {
416
422
const userType = this . getUserTypeByName ( this . localStorage . getItem ( 'userType' ) ) ;
417
423
418
424
if ( userType ) {
419
- this . userType = userType ;
425
+ this . userType . next ( userType ) ;
420
426
}
421
427
422
428
this . getAuthDataFromStorage ( ) ;
@@ -470,7 +476,7 @@ export class AngularTokenService implements CanActivate {
470
476
} ;
471
477
472
478
if ( this . checkAuthData ( authData ) ) {
473
- this . authData = authData ;
479
+ this . authData . next ( authData ) ;
474
480
}
475
481
}
476
482
@@ -486,7 +492,7 @@ export class AngularTokenService implements CanActivate {
486
492
} ;
487
493
488
494
if ( this . checkAuthData ( authData ) ) {
489
- this . authData = authData ;
495
+ this . authData . next ( authData ) ;
490
496
}
491
497
} ) ;
492
498
}
@@ -501,16 +507,16 @@ export class AngularTokenService implements CanActivate {
501
507
private setAuthData ( authData : AuthData ) : void {
502
508
if ( this . checkAuthData ( authData ) ) {
503
509
504
- this . authData = authData ;
510
+ this . authData . next ( authData ) ;
505
511
506
512
this . localStorage . setItem ( 'accessToken' , authData . accessToken ) ;
507
513
this . localStorage . setItem ( 'client' , authData . client ) ;
508
514
this . localStorage . setItem ( 'expiry' , authData . expiry ) ;
509
515
this . localStorage . setItem ( 'tokenType' , authData . tokenType ) ;
510
516
this . localStorage . setItem ( 'uid' , authData . uid ) ;
511
517
512
- if ( this . userType != null ) {
513
- this . localStorage . setItem ( 'userType' , this . userType . name ) ;
518
+ if ( this . userType . value != null ) {
519
+ this . localStorage . setItem ( 'userType' , this . userType . value . name ) ;
514
520
}
515
521
516
522
}
@@ -533,14 +539,12 @@ export class AngularTokenService implements CanActivate {
533
539
authData . tokenType != null &&
534
540
authData . uid != null
535
541
) {
536
- if ( this . authData != null ) {
537
- return authData . expiry >= this . authData . expiry ;
538
- } else {
539
- return true ;
542
+ if ( this . authData . value != null ) {
543
+ return authData . expiry >= this . authData . value . expiry ;
540
544
}
541
- } else {
542
- return false ;
545
+ return true ;
543
546
}
547
+ return false ;
544
548
}
545
549
546
550
0 commit comments