diff --git a/ui-ngx/src/app/core/auth/auth.service.ts b/ui-ngx/src/app/core/auth/auth.service.ts index 1f8788d821..3d4a203252 100644 --- a/ui-ngx/src/app/core/auth/auth.service.ts +++ b/ui-ngx/src/app/core/auth/auth.service.ts @@ -282,8 +282,7 @@ export class AuthService { if (publicId) { return this.publicLogin(publicId).pipe( mergeMap((response) => { - this.updateAndValidateToken(response.token, 'jwt_token', false); - this.updateAndValidateToken(response.refreshToken, 'refresh_token', false); + this.updateAndValidateTokens(response.token, response.refreshToken, false); return this.procceedJwtTokenValidate(); }), catchError((err) => { @@ -317,8 +316,7 @@ export class AuthService { }; return this.http.post('/api/auth/login', loginRequest, defaultHttpOptions()).pipe( mergeMap((loginResponse: LoginResponse) => { - this.updateAndValidateToken(loginResponse.token, 'jwt_token', false); - this.updateAndValidateToken(loginResponse.refreshToken, 'refresh_token', false); + this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, false); return this.procceedJwtTokenValidate(); } ) @@ -439,7 +437,7 @@ export class AuthService { })); } - public refreshJwtToken(): Observable { + public refreshJwtToken(loadUserElseStoreJwtToken = true): Observable { let response: Observable = this.refreshTokenSubject; if (this.refreshTokenSubject === null) { this.refreshTokenSubject = new ReplaySubject(1); @@ -456,7 +454,11 @@ export class AuthService { }; const refreshObservable = this.http.post('/api/auth/token', refreshTokenRequest, defaultHttpOptions()); refreshObservable.subscribe((loginResponse: LoginResponse) => { - this.setUserFromJwtToken(loginResponse.token, loginResponse.refreshToken, false); + if (loadUserElseStoreJwtToken) { + this.setUserFromJwtToken(loginResponse.token, loginResponse.refreshToken, false); + } else { + this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, true); + } this.refreshTokenSubject.next(loginResponse); this.refreshTokenSubject.complete(); this.refreshTokenSubject = null; @@ -474,7 +476,7 @@ export class AuthService { const subject = new ReplaySubject(); if (!AuthService.isTokenValid('jwt_token')) { if (doRefresh) { - this.refreshJwtToken().subscribe( + this.refreshJwtToken(!doRefresh).subscribe( () => { subject.next(); subject.complete(); @@ -505,8 +507,7 @@ export class AuthService { this.notifyUnauthenticated(); } } else { - this.updateAndValidateToken(jwtToken, 'jwt_token', true); - this.updateAndValidateToken(refreshToken, 'refresh_token', true); + this.updateAndValidateTokens(jwtToken, refreshToken, true); if (notify) { this.notifyUserLoaded(false); this.loadUser(false).subscribe( @@ -525,6 +526,11 @@ export class AuthService { } } + private updateAndValidateTokens(jwtToken, refreshToken, notify: boolean) { + this.updateAndValidateToken(jwtToken, 'jwt_token', notify); + this.updateAndValidateToken(refreshToken, 'refresh_token', notify); + } + public parsePublicId(): string { const token = AuthService.getJwtToken(); if (token) {