Skip to content

Commit 3f583f4

Browse files
authored
Behaviorsubject support for authData, userData and userType (#489)
1 parent 46986cc commit 3f583f4

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

projects/angular-token/src/lib/angular-token.interceptor.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ export class AngularTokenInterceptor implements HttpInterceptor {
1717
this.tokenService.getAuthDataFromStorage();
1818

1919
// Add the headers if the request is going to the configured server
20-
if (this.tokenService.currentAuthData &&
20+
const authData = this.tokenService.authData.value;
21+
22+
if (authData &&
2123
(this.tokenService.tokenOptions.apiBase === null || req.url.match(this.tokenService.tokenOptions.apiBase))) {
2224

2325
const headers = {
24-
'access-token': this.tokenService.currentAuthData.accessToken,
25-
'client': this.tokenService.currentAuthData.client,
26-
'expiry': this.tokenService.currentAuthData.expiry,
27-
'token-type': this.tokenService.currentAuthData.tokenType,
28-
'uid': this.tokenService.currentAuthData.uid
26+
'access-token': authData.accessToken,
27+
'client': authData.client,
28+
'expiry': authData.expiry,
29+
'token-type': authData.tokenType,
30+
'uid': authData.uid
2931
};
3032

3133
req = req.clone({

projects/angular-token/src/lib/angular-token.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,11 @@ describe('AngularTokenService', () => {
467467
});
468468

469469
it('currentAuthData should return undefined', () => {
470-
expect(service.currentAuthData).toEqual(undefined);
470+
expect(service.currentAuthData).toEqual(null);
471471
});
472472

473473
it('currentUserData should return undefined', () => {
474-
expect(service.currentUserData).toEqual(undefined);
474+
expect(service.currentUserData).toEqual(null);
475475
});
476476

477477
it('currentUserType should return undefined', () => {

projects/angular-token/src/lib/angular-token.service.ts

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ActivatedRoute, Router, CanActivate, ActivatedRouteSnapshot, RouterStat
33
import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http';
44
import { isPlatformServer } from '@angular/common';
55

6-
import { Observable, fromEvent, interval } from 'rxjs';
6+
import { Observable, fromEvent, interval, BehaviorSubject } from 'rxjs';
77
import { pluck, filter, share, finalize } from 'rxjs/operators';
88

99
import { ANGULAR_TOKEN_OPTIONS } from './angular-token.token';
@@ -28,19 +28,19 @@ import {
2828
export class AngularTokenService implements CanActivate {
2929

3030
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;
3333
} else {
3434
return undefined;
3535
}
3636
}
3737

3838
get currentUserData(): UserData {
39-
return this.userData;
39+
return this.userData.value;
4040
}
4141

4242
get currentAuthData(): AuthData {
43-
return this.authData;
43+
return this.authData.value;
4444
}
4545

4646
get apiBase(): string {
@@ -58,9 +58,9 @@ export class AngularTokenService implements CanActivate {
5858
}
5959

6060
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);
6464
private global: Window | any;
6565

6666
private localStorage: Storage | any = {};
@@ -138,7 +138,11 @@ export class AngularTokenService implements CanActivate {
138138
}
139139

140140
userSignedIn(): boolean {
141-
return !!this.authData;
141+
if (this.authData.value == null) {
142+
return false;
143+
} else {
144+
return true;
145+
}
142146
}
143147

144148
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
@@ -175,9 +179,9 @@ export class AngularTokenService implements CanActivate {
175179
registerData = Object.assign({}, registerData);
176180

177181
if (registerData.userType == null) {
178-
this.userType = null;
182+
this.userType.next(null);
179183
} else {
180-
this.userType = this.getUserTypeByName(registerData.userType);
184+
this.userType.next(this.getUserTypeByName(registerData.userType));
181185
delete registerData.userType;
182186
}
183187

@@ -211,7 +215,7 @@ export class AngularTokenService implements CanActivate {
211215

212216
// Sign in request and set storage
213217
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));
215219

216220
const body = {
217221
[this.options.loginField]: signInData.login,
@@ -226,7 +230,7 @@ export class AngularTokenService implements CanActivate {
226230
this.getServerPath() + this.options.signInPath, body
227231
).pipe(share());
228232

229-
observ.subscribe(res => this.userData = res.data);
233+
observ.subscribe(res => this.userData.next(res.data));
230234

231235
return observ;
232236
}
@@ -280,9 +284,9 @@ export class AngularTokenService implements CanActivate {
280284
this.localStorage.removeItem('tokenType');
281285
this.localStorage.removeItem('uid');
282286

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);
286290
}
287291
)
288292
);
@@ -295,7 +299,7 @@ export class AngularTokenService implements CanActivate {
295299
).pipe(share());
296300

297301
observ.subscribe(
298-
(res) => this.userData = res.data,
302+
(res) => this.userData.next(res.data),
299303
(error) => {
300304
if (error.status === 401 && this.options.signOutFailedValidate) {
301305
this.signOut();
@@ -309,7 +313,7 @@ export class AngularTokenService implements CanActivate {
309313
updatePassword(updatePasswordData: UpdatePasswordData): Observable<ApiResponse> {
310314

311315
if (updatePasswordData.userType != null) {
312-
this.userType = this.getUserTypeByName(updatePasswordData.userType);
316+
this.userType.next(this.getUserTypeByName(updatePasswordData.userType));
313317
}
314318

315319
let args: any;
@@ -338,7 +342,9 @@ export class AngularTokenService implements CanActivate {
338342
// Reset password request
339343
resetPassword(resetPasswordData: ResetPasswordData): Observable<ApiResponse> {
340344

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+
);
342348

343349
const body = {
344350
[this.options.loginField]: resetPasswordData.login,
@@ -356,7 +362,7 @@ export class AngularTokenService implements CanActivate {
356362
*/
357363

358364
private getUserPath(): string {
359-
return (this.userType == null) ? '' : this.userType.path + '/';
365+
return (this.userType.value == null) ? '' : this.userType.value.path + '/';
360366
}
361367

362368
private getApiPath(): string {
@@ -396,8 +402,8 @@ export class AngularTokenService implements CanActivate {
396402
url += `?omniauth_window_type=${windowType}`;
397403
url += `&auth_origin_url=${encodeURIComponent(callbackUrl)}`;
398404

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}`;
401407
}
402408

403409
return url;
@@ -416,7 +422,7 @@ export class AngularTokenService implements CanActivate {
416422
const userType = this.getUserTypeByName(this.localStorage.getItem('userType'));
417423

418424
if (userType) {
419-
this.userType = userType;
425+
this.userType.next(userType);
420426
}
421427

422428
this.getAuthDataFromStorage();
@@ -470,7 +476,7 @@ export class AngularTokenService implements CanActivate {
470476
};
471477

472478
if (this.checkAuthData(authData)) {
473-
this.authData = authData;
479+
this.authData.next(authData);
474480
}
475481
}
476482

@@ -486,7 +492,7 @@ export class AngularTokenService implements CanActivate {
486492
};
487493

488494
if (this.checkAuthData(authData)) {
489-
this.authData = authData;
495+
this.authData.next(authData);
490496
}
491497
});
492498
}
@@ -501,16 +507,16 @@ export class AngularTokenService implements CanActivate {
501507
private setAuthData(authData: AuthData): void {
502508
if (this.checkAuthData(authData)) {
503509

504-
this.authData = authData;
510+
this.authData.next(authData);
505511

506512
this.localStorage.setItem('accessToken', authData.accessToken);
507513
this.localStorage.setItem('client', authData.client);
508514
this.localStorage.setItem('expiry', authData.expiry);
509515
this.localStorage.setItem('tokenType', authData.tokenType);
510516
this.localStorage.setItem('uid', authData.uid);
511517

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);
514520
}
515521

516522
}
@@ -533,14 +539,12 @@ export class AngularTokenService implements CanActivate {
533539
authData.tokenType != null &&
534540
authData.uid != null
535541
) {
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;
540544
}
541-
} else {
542-
return false;
545+
return true;
543546
}
547+
return false;
544548
}
545549

546550

0 commit comments

Comments
 (0)