UI: Added in refreshJwtToken updated in store Auth user when change property

This commit is contained in:
Vladyslav_Prykhodko 2023-08-31 12:53:59 +03:00
parent 6479a7ec76
commit a8a787b94e
3 changed files with 22 additions and 9 deletions

View File

@ -27,7 +27,12 @@ import { defaultHttpOptions, defaultHttpOptionsFromConfig, RequestConfig } from
import { UserService } from '../http/user.service'; import { UserService } from '../http/user.service';
import { Store } from '@ngrx/store'; import { Store } from '@ngrx/store';
import { AppState } from '../core.state'; 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 { getCurrentAuthState, getCurrentAuthUser } from './auth.selectors';
import { Authority } from '@shared/models/authority.enum'; import { Authority } from '@shared/models/authority.enum';
import { ActionSettingsChangeLanguage } from '@app/core/settings/settings.actions'; import { ActionSettingsChangeLanguage } from '@app/core/settings/settings.actions';
@ -480,6 +485,19 @@ export class AuthService {
} else { } else {
this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, true); 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.next(loginResponse);
this.refreshTokenSubject.complete(); this.refreshTokenSubject.complete();
this.refreshTokenSubject = null; this.refreshTokenSubject = null;

View File

@ -23,7 +23,7 @@ import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state'; import { AppState } from '@core/core.state';
import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { HasConfirmForm } from '@core/guards/confirm-on-exit.guard'; 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 { environment as env } from '@env/environment';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { ActionSettingsChangeLanguage } from '@core/settings/settings.actions'; import { ActionSettingsChangeLanguage } from '@core/settings/settings.actions';
@ -95,11 +95,6 @@ export class ProfileComponent extends PageComponent implements OnInit, HasConfir
id: user.id, id: user.id,
lastName: user.lastName, 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.store.dispatch(new ActionSettingsChangeLanguage({ userLang: user.additionalInfo.lang }));
this.authService.refreshJwtToken(false); this.authService.refreshJwtToken(false);
} }

View File

@ -47,8 +47,8 @@ export interface AuthUser {
sub: string; sub: string;
scopes: string[]; scopes: string[];
userId: string; userId: string;
firstName?: string; firstName: string;
lastName?: string; lastName: string;
enabled: boolean; enabled: boolean;
tenantId: string; tenantId: string;
customerId: string; customerId: string;