diff --git a/ui-ngx/src/app/core/auth/auth.service.ts b/ui-ngx/src/app/core/auth/auth.service.ts index 8a838b4130..d2cb53aa11 100644 --- a/ui-ngx/src/app/core/auth/auth.service.ts +++ b/ui-ngx/src/app/core/auth/auth.service.ts @@ -27,7 +27,12 @@ import { defaultHttpOptions, defaultHttpOptionsFromConfig, RequestConfig } from import { UserService } from '../http/user.service'; import { Store } from '@ngrx/store'; import { AppState } from '../core.state'; -import { ActionAuthAuthenticated, ActionAuthLoadUser, ActionAuthUnauthenticated } from './auth.actions'; +import { + ActionAuthAuthenticated, + ActionAuthLoadUser, + ActionAuthUnauthenticated, + ActionAuthUpdateAuthUser +} from './auth.actions'; import { getCurrentAuthState, getCurrentAuthUser } from './auth.selectors'; import { Authority } from '@shared/models/authority.enum'; import { ActionSettingsChangeLanguage } from '@app/core/settings/settings.actions'; @@ -480,6 +485,19 @@ export class AuthService { } else { this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, true); } + const authUser = getCurrentAuthUser(this.store); + const tokenData = this.jwtHelper.decodeToken(loginResponse.token); + if (['sub', 'firstName', 'lastName'].some(value => authUser[value] !== tokenData[value])) { + this.store.dispatch(new ActionAuthUpdateAuthUser({ + authUser: { + ...authUser, ...{ + sub: tokenData.sub, + firstName: tokenData.firstName, + lastName: tokenData.lastName, + } + } + })); + } this.refreshTokenSubject.next(loginResponse); this.refreshTokenSubject.complete(); this.refreshTokenSubject = null; diff --git a/ui-ngx/src/app/modules/home/pages/profile/profile.component.ts b/ui-ngx/src/app/modules/home/pages/profile/profile.component.ts index 2c22cbaa8a..cf6b3d34cb 100644 --- a/ui-ngx/src/app/modules/home/pages/profile/profile.component.ts +++ b/ui-ngx/src/app/modules/home/pages/profile/profile.component.ts @@ -23,7 +23,7 @@ import { Store } from '@ngrx/store'; import { AppState } from '@core/core.state'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard'; -import { ActionAuthUpdateAuthUser, ActionAuthUpdateUserDetails } from '@core/auth/auth.actions'; +import { ActionAuthUpdateUserDetails } from '@core/auth/auth.actions'; import { environment as env } from '@env/environment'; import { TranslateService } from '@ngx-translate/core'; import { ActionSettingsChangeLanguage } from '@core/settings/settings.actions'; @@ -95,11 +95,6 @@ export class ProfileComponent extends PageComponent implements OnInit, HasConfir id: user.id, lastName: user.lastName, } })); - this.store.dispatch(new ActionAuthUpdateAuthUser({ authUser: {...this.authUser, ...{ - sub: user.email, - firstName: user.firstName, - lastName: user.lastName, - }}})); this.store.dispatch(new ActionSettingsChangeLanguage({ userLang: user.additionalInfo.lang })); this.authService.refreshJwtToken(false); } diff --git a/ui-ngx/src/app/shared/models/user.model.ts b/ui-ngx/src/app/shared/models/user.model.ts index 53a79d3173..02a8c657f9 100644 --- a/ui-ngx/src/app/shared/models/user.model.ts +++ b/ui-ngx/src/app/shared/models/user.model.ts @@ -47,8 +47,8 @@ export interface AuthUser { sub: string; scopes: string[]; userId: string; - firstName?: string; - lastName?: string; + firstName: string; + lastName: string; enabled: boolean; tenantId: string; customerId: string;