Commit bc2d04525783d64881beddf3cecba385877014c9
1 parent
87a8cec7
Do not load user on token refresh during initial user load
Showing
1 changed file
with
15 additions
and
9 deletions
@@ -282,8 +282,7 @@ export class AuthService { | @@ -282,8 +282,7 @@ export class AuthService { | ||
282 | if (publicId) { | 282 | if (publicId) { |
283 | return this.publicLogin(publicId).pipe( | 283 | return this.publicLogin(publicId).pipe( |
284 | mergeMap((response) => { | 284 | mergeMap((response) => { |
285 | - this.updateAndValidateToken(response.token, 'jwt_token', false); | ||
286 | - this.updateAndValidateToken(response.refreshToken, 'refresh_token', false); | 285 | + this.updateAndValidateTokens(response.token, response.refreshToken, false); |
287 | return this.procceedJwtTokenValidate(); | 286 | return this.procceedJwtTokenValidate(); |
288 | }), | 287 | }), |
289 | catchError((err) => { | 288 | catchError((err) => { |
@@ -317,8 +316,7 @@ export class AuthService { | @@ -317,8 +316,7 @@ export class AuthService { | ||
317 | }; | 316 | }; |
318 | return this.http.post<LoginResponse>('/api/auth/login', loginRequest, defaultHttpOptions()).pipe( | 317 | return this.http.post<LoginResponse>('/api/auth/login', loginRequest, defaultHttpOptions()).pipe( |
319 | mergeMap((loginResponse: LoginResponse) => { | 318 | mergeMap((loginResponse: LoginResponse) => { |
320 | - this.updateAndValidateToken(loginResponse.token, 'jwt_token', false); | ||
321 | - this.updateAndValidateToken(loginResponse.refreshToken, 'refresh_token', false); | 319 | + this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, false); |
322 | return this.procceedJwtTokenValidate(); | 320 | return this.procceedJwtTokenValidate(); |
323 | } | 321 | } |
324 | ) | 322 | ) |
@@ -439,7 +437,7 @@ export class AuthService { | @@ -439,7 +437,7 @@ export class AuthService { | ||
439 | })); | 437 | })); |
440 | } | 438 | } |
441 | 439 | ||
442 | - public refreshJwtToken(): Observable<LoginResponse> { | 440 | + public refreshJwtToken(loadUserElseStoreJwtToken = true): Observable<LoginResponse> { |
443 | let response: Observable<LoginResponse> = this.refreshTokenSubject; | 441 | let response: Observable<LoginResponse> = this.refreshTokenSubject; |
444 | if (this.refreshTokenSubject === null) { | 442 | if (this.refreshTokenSubject === null) { |
445 | this.refreshTokenSubject = new ReplaySubject<LoginResponse>(1); | 443 | this.refreshTokenSubject = new ReplaySubject<LoginResponse>(1); |
@@ -456,7 +454,11 @@ export class AuthService { | @@ -456,7 +454,11 @@ export class AuthService { | ||
456 | }; | 454 | }; |
457 | const refreshObservable = this.http.post<LoginResponse>('/api/auth/token', refreshTokenRequest, defaultHttpOptions()); | 455 | const refreshObservable = this.http.post<LoginResponse>('/api/auth/token', refreshTokenRequest, defaultHttpOptions()); |
458 | refreshObservable.subscribe((loginResponse: LoginResponse) => { | 456 | refreshObservable.subscribe((loginResponse: LoginResponse) => { |
459 | - this.setUserFromJwtToken(loginResponse.token, loginResponse.refreshToken, false); | 457 | + if (loadUserElseStoreJwtToken) { |
458 | + this.setUserFromJwtToken(loginResponse.token, loginResponse.refreshToken, false); | ||
459 | + } else { | ||
460 | + this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, true); | ||
461 | + } | ||
460 | this.refreshTokenSubject.next(loginResponse); | 462 | this.refreshTokenSubject.next(loginResponse); |
461 | this.refreshTokenSubject.complete(); | 463 | this.refreshTokenSubject.complete(); |
462 | this.refreshTokenSubject = null; | 464 | this.refreshTokenSubject = null; |
@@ -474,7 +476,7 @@ export class AuthService { | @@ -474,7 +476,7 @@ export class AuthService { | ||
474 | const subject = new ReplaySubject<void>(); | 476 | const subject = new ReplaySubject<void>(); |
475 | if (!AuthService.isTokenValid('jwt_token')) { | 477 | if (!AuthService.isTokenValid('jwt_token')) { |
476 | if (doRefresh) { | 478 | if (doRefresh) { |
477 | - this.refreshJwtToken().subscribe( | 479 | + this.refreshJwtToken(!doRefresh).subscribe( |
478 | () => { | 480 | () => { |
479 | subject.next(); | 481 | subject.next(); |
480 | subject.complete(); | 482 | subject.complete(); |
@@ -505,8 +507,7 @@ export class AuthService { | @@ -505,8 +507,7 @@ export class AuthService { | ||
505 | this.notifyUnauthenticated(); | 507 | this.notifyUnauthenticated(); |
506 | } | 508 | } |
507 | } else { | 509 | } else { |
508 | - this.updateAndValidateToken(jwtToken, 'jwt_token', true); | ||
509 | - this.updateAndValidateToken(refreshToken, 'refresh_token', true); | 510 | + this.updateAndValidateTokens(jwtToken, refreshToken, true); |
510 | if (notify) { | 511 | if (notify) { |
511 | this.notifyUserLoaded(false); | 512 | this.notifyUserLoaded(false); |
512 | this.loadUser(false).subscribe( | 513 | this.loadUser(false).subscribe( |
@@ -525,6 +526,11 @@ export class AuthService { | @@ -525,6 +526,11 @@ export class AuthService { | ||
525 | } | 526 | } |
526 | } | 527 | } |
527 | 528 | ||
529 | + private updateAndValidateTokens(jwtToken, refreshToken, notify: boolean) { | ||
530 | + this.updateAndValidateToken(jwtToken, 'jwt_token', notify); | ||
531 | + this.updateAndValidateToken(refreshToken, 'refresh_token', notify); | ||
532 | + } | ||
533 | + | ||
528 | public parsePublicId(): string { | 534 | public parsePublicId(): string { |
529 | const token = AuthService.getJwtToken(); | 535 | const token = AuthService.getJwtToken(); |
530 | if (token) { | 536 | if (token) { |