From 1882cbdf4abf9682f9d94ffb953234ad6465331a Mon Sep 17 00:00:00 2001 From: Artem Dzhereleiko Date: Tue, 8 Aug 2023 15:30:46 +0300 Subject: [PATCH 1/4] UI: Fixed profile update --- ui-ngx/src/app/core/auth/auth.actions.ts | 12 ++++++++++-- ui-ngx/src/app/core/auth/auth.reducer.ts | 3 +++ .../modules/home/pages/profile/profile.component.ts | 11 ++++++++++- ui-ngx/src/app/shared/models/user.model.ts | 1 + 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/ui-ngx/src/app/core/auth/auth.actions.ts b/ui-ngx/src/app/core/auth/auth.actions.ts index 9e5640a97d..7ba4892476 100644 --- a/ui-ngx/src/app/core/auth/auth.actions.ts +++ b/ui-ngx/src/app/core/auth/auth.actions.ts @@ -15,7 +15,7 @@ /// import { Action } from '@ngrx/store'; -import { User } from '@shared/models/user.model'; +import { AuthUser, User } from '@shared/models/user.model'; import { AuthPayload } from '@core/auth/auth.models'; import { UserSettings } from '@shared/models/user-settings.models'; @@ -24,6 +24,7 @@ export enum AuthActionTypes { UNAUTHENTICATED = '[Auth] Unauthenticated', LOAD_USER = '[Auth] Load User', UPDATE_USER_DETAILS = '[Auth] Update User Details', + UPDATE_AUTH_USER = '[Auth] Update Auth User', UPDATE_LAST_PUBLIC_DASHBOARD_ID = '[Auth] Update Last Public Dashboard Id', UPDATE_HAS_REPOSITORY = '[Auth] Change Has Repository', UPDATE_OPENED_MENU_SECTION = '[Preferences] Update Opened Menu Section', @@ -53,6 +54,12 @@ export class ActionAuthUpdateUserDetails implements Action { constructor(readonly payload: { userDetails: User }) {} } +export class ActionAuthUpdateAuthUser implements Action { + readonly type = AuthActionTypes.UPDATE_AUTH_USER; + + constructor(readonly payload: { authUser: AuthUser }) {} +} + export class ActionAuthUpdateLastPublicDashboardId implements Action { readonly type = AuthActionTypes.UPDATE_LAST_PUBLIC_DASHBOARD_ID; @@ -85,4 +92,5 @@ export class ActionPreferencesDeleteUserSettings implements Action { export type AuthActions = ActionAuthAuthenticated | ActionAuthUnauthenticated | ActionAuthLoadUser | ActionAuthUpdateUserDetails | ActionAuthUpdateLastPublicDashboardId | ActionAuthUpdateHasRepository | - ActionPreferencesUpdateOpenedMenuSection | ActionPreferencesPutUserSettings | ActionPreferencesDeleteUserSettings; + ActionPreferencesUpdateOpenedMenuSection | ActionPreferencesPutUserSettings | ActionPreferencesDeleteUserSettings | + ActionAuthUpdateAuthUser; diff --git a/ui-ngx/src/app/core/auth/auth.reducer.ts b/ui-ngx/src/app/core/auth/auth.reducer.ts index 4bcf71104b..146470b0f6 100644 --- a/ui-ngx/src/app/core/auth/auth.reducer.ts +++ b/ui-ngx/src/app/core/auth/auth.reducer.ts @@ -58,6 +58,9 @@ export const authReducer = ( case AuthActionTypes.UPDATE_USER_DETAILS: return { ...state, ...action.payload}; + case AuthActionTypes.UPDATE_AUTH_USER: + return { ...state, ...action.payload}; + case AuthActionTypes.UPDATE_LAST_PUBLIC_DASHBOARD_ID: return { ...state, ...action.payload}; 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 0633e696dd..da5490cefa 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,13 +23,14 @@ 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 { ActionAuthUpdateUserDetails } from '@core/auth/auth.actions'; +import { ActionAuthUpdateAuthUser, 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'; import { ActivatedRoute } from '@angular/router'; import { isDefinedAndNotNull } from '@core/utils'; import { getCurrentAuthUser } from '@core/auth/auth.selectors'; +import { AuthService } from '@core/auth/auth.service'; @Component({ selector: 'tb-profile', @@ -47,6 +48,7 @@ export class ProfileComponent extends PageComponent implements OnInit, HasConfir constructor(protected store: Store, private route: ActivatedRoute, private userService: UserService, + private authService: AuthService, private translate: TranslateService, public fb: UntypedFormBuilder) { super(store); @@ -93,7 +95,14 @@ 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, + phone: user.phone, + 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 02a8c657f9..709e621568 100644 --- a/ui-ngx/src/app/shared/models/user.model.ts +++ b/ui-ngx/src/app/shared/models/user.model.ts @@ -54,6 +54,7 @@ export interface AuthUser { customerId: string; isPublic: boolean; authority: Authority; + phone: string; } export interface UserEmailInfo { From 5dd95cf00b4151553d1d809011a2a2a0b81f1b55 Mon Sep 17 00:00:00 2001 From: Artem Dzhereleiko Date: Wed, 9 Aug 2023 11:14:47 +0300 Subject: [PATCH 2/4] UI: Refactoring --- .../src/app/modules/home/pages/profile/profile.component.ts | 1 - ui-ngx/src/app/shared/models/user.model.ts | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) 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 da5490cefa..2c22cbaa8a 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 @@ -97,7 +97,6 @@ export class ProfileComponent extends PageComponent implements OnInit, HasConfir } })); this.store.dispatch(new ActionAuthUpdateAuthUser({ authUser: {...this.authUser, ...{ sub: user.email, - phone: user.phone, firstName: user.firstName, lastName: user.lastName, }}})); diff --git a/ui-ngx/src/app/shared/models/user.model.ts b/ui-ngx/src/app/shared/models/user.model.ts index 709e621568..53a79d3173 100644 --- a/ui-ngx/src/app/shared/models/user.model.ts +++ b/ui-ngx/src/app/shared/models/user.model.ts @@ -47,14 +47,13 @@ export interface AuthUser { sub: string; scopes: string[]; userId: string; - firstName: string; - lastName: string; + firstName?: string; + lastName?: string; enabled: boolean; tenantId: string; customerId: string; isPublic: boolean; authority: Authority; - phone: string; } export interface UserEmailInfo { From a8a787b94e5450ff18ed7cff8eef9cc13cb3ce46 Mon Sep 17 00:00:00 2001 From: Vladyslav_Prykhodko Date: Thu, 31 Aug 2023 12:53:59 +0300 Subject: [PATCH 3/4] UI: Added in refreshJwtToken updated in store Auth user when change property --- ui-ngx/src/app/core/auth/auth.service.ts | 20 ++++++++++++++++++- .../home/pages/profile/profile.component.ts | 7 +------ ui-ngx/src/app/shared/models/user.model.ts | 4 ++-- 3 files changed, 22 insertions(+), 9 deletions(-) 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; From 7cc087a3ce34681f1870e3c8bc043e78399f9745 Mon Sep 17 00:00:00 2001 From: Vladyslav_Prykhodko Date: Mon, 4 Sep 2023 11:22:06 +0300 Subject: [PATCH 4/4] UI: Refactoring updated Auth User in store --- ui-ngx/src/app/core/auth/auth.actions.ts | 2 +- ui-ngx/src/app/core/auth/auth.reducer.ts | 3 ++- ui-ngx/src/app/core/auth/auth.service.ts | 26 ++++++++++++------------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/ui-ngx/src/app/core/auth/auth.actions.ts b/ui-ngx/src/app/core/auth/auth.actions.ts index 7ba4892476..a6bf97c527 100644 --- a/ui-ngx/src/app/core/auth/auth.actions.ts +++ b/ui-ngx/src/app/core/auth/auth.actions.ts @@ -57,7 +57,7 @@ export class ActionAuthUpdateUserDetails implements Action { export class ActionAuthUpdateAuthUser implements Action { readonly type = AuthActionTypes.UPDATE_AUTH_USER; - constructor(readonly payload: { authUser: AuthUser }) {} + constructor(readonly payload: Partial) {} } export class ActionAuthUpdateLastPublicDashboardId implements Action { diff --git a/ui-ngx/src/app/core/auth/auth.reducer.ts b/ui-ngx/src/app/core/auth/auth.reducer.ts index 146470b0f6..c0ce66e990 100644 --- a/ui-ngx/src/app/core/auth/auth.reducer.ts +++ b/ui-ngx/src/app/core/auth/auth.reducer.ts @@ -59,7 +59,8 @@ export const authReducer = ( return { ...state, ...action.payload}; case AuthActionTypes.UPDATE_AUTH_USER: - return { ...state, ...action.payload}; + const authUser = {...state.authUser, ...action.payload}; + return { ...state, ...{ authUser }}; case AuthActionTypes.UPDATE_LAST_PUBLIC_DASHBOARD_ID: return { ...state, ...action.payload}; diff --git a/ui-ngx/src/app/core/auth/auth.service.ts b/ui-ngx/src/app/core/auth/auth.service.ts index d2cb53aa11..e969c8129a 100644 --- a/ui-ngx/src/app/core/auth/auth.service.ts +++ b/ui-ngx/src/app/core/auth/auth.service.ts @@ -485,19 +485,7 @@ 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.updatedAuthUserFromToken(loginResponse.token); this.refreshTokenSubject.next(loginResponse); this.refreshTokenSubject.complete(); this.refreshTokenSubject = null; @@ -511,6 +499,18 @@ export class AuthService { return response; } + private updatedAuthUserFromToken(token: string) { + const authUser = getCurrentAuthUser(this.store); + const tokenData = this.jwtHelper.decodeToken(token); + if (['sub', 'firstName', 'lastName'].some(value => authUser[value] !== tokenData[value])) { + this.store.dispatch(new ActionAuthUpdateAuthUser({ + sub: tokenData.sub, + firstName: tokenData.firstName, + lastName: tokenData.lastName, + })); + } + } + private validateJwtToken(doRefresh): Observable { const subject = new ReplaySubject(); if (!AuthService.isTokenValid('jwt_token')) {