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 | 282 | if (publicId) { |
283 | 283 | return this.publicLogin(publicId).pipe( |
284 | 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 | 286 | return this.procceedJwtTokenValidate(); |
288 | 287 | }), |
289 | 288 | catchError((err) => { |
... | ... | @@ -317,8 +316,7 @@ export class AuthService { |
317 | 316 | }; |
318 | 317 | return this.http.post<LoginResponse>('/api/auth/login', loginRequest, defaultHttpOptions()).pipe( |
319 | 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 | 320 | return this.procceedJwtTokenValidate(); |
323 | 321 | } |
324 | 322 | ) |
... | ... | @@ -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 | 441 | let response: Observable<LoginResponse> = this.refreshTokenSubject; |
444 | 442 | if (this.refreshTokenSubject === null) { |
445 | 443 | this.refreshTokenSubject = new ReplaySubject<LoginResponse>(1); |
... | ... | @@ -456,7 +454,11 @@ export class AuthService { |
456 | 454 | }; |
457 | 455 | const refreshObservable = this.http.post<LoginResponse>('/api/auth/token', refreshTokenRequest, defaultHttpOptions()); |
458 | 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 | 462 | this.refreshTokenSubject.next(loginResponse); |
461 | 463 | this.refreshTokenSubject.complete(); |
462 | 464 | this.refreshTokenSubject = null; |
... | ... | @@ -474,7 +476,7 @@ export class AuthService { |
474 | 476 | const subject = new ReplaySubject<void>(); |
475 | 477 | if (!AuthService.isTokenValid('jwt_token')) { |
476 | 478 | if (doRefresh) { |
477 | - this.refreshJwtToken().subscribe( | |
479 | + this.refreshJwtToken(!doRefresh).subscribe( | |
478 | 480 | () => { |
479 | 481 | subject.next(); |
480 | 482 | subject.complete(); |
... | ... | @@ -505,8 +507,7 @@ export class AuthService { |
505 | 507 | this.notifyUnauthenticated(); |
506 | 508 | } |
507 | 509 | } else { |
508 | - this.updateAndValidateToken(jwtToken, 'jwt_token', true); | |
509 | - this.updateAndValidateToken(refreshToken, 'refresh_token', true); | |
510 | + this.updateAndValidateTokens(jwtToken, refreshToken, true); | |
510 | 511 | if (notify) { |
511 | 512 | this.notifyUserLoaded(false); |
512 | 513 | this.loadUser(false).subscribe( |
... | ... | @@ -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 | 534 | public parsePublicId(): string { |
529 | 535 | const token = AuthService.getJwtToken(); |
530 | 536 | if (token) { | ... | ... |