Merge pull request #9057 from ArtemDzhereleiko/AD/bug-fix/update-current-user

Fixed update user profile
This commit is contained in:
Igor Kulikov 2023-09-04 18:18:25 +03:00 committed by GitHub
commit ae04d1ef98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 3 deletions

View File

@ -15,7 +15,7 @@
/// ///
import { Action } from '@ngrx/store'; 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 { AuthPayload } from '@core/auth/auth.models';
import { UserSettings } from '@shared/models/user-settings.models'; import { UserSettings } from '@shared/models/user-settings.models';
@ -24,6 +24,7 @@ export enum AuthActionTypes {
UNAUTHENTICATED = '[Auth] Unauthenticated', UNAUTHENTICATED = '[Auth] Unauthenticated',
LOAD_USER = '[Auth] Load User', LOAD_USER = '[Auth] Load User',
UPDATE_USER_DETAILS = '[Auth] Update User Details', 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_LAST_PUBLIC_DASHBOARD_ID = '[Auth] Update Last Public Dashboard Id',
UPDATE_HAS_REPOSITORY = '[Auth] Change Has Repository', UPDATE_HAS_REPOSITORY = '[Auth] Change Has Repository',
UPDATE_OPENED_MENU_SECTION = '[Preferences] Update Opened Menu Section', UPDATE_OPENED_MENU_SECTION = '[Preferences] Update Opened Menu Section',
@ -53,6 +54,12 @@ export class ActionAuthUpdateUserDetails implements Action {
constructor(readonly payload: { userDetails: User }) {} constructor(readonly payload: { userDetails: User }) {}
} }
export class ActionAuthUpdateAuthUser implements Action {
readonly type = AuthActionTypes.UPDATE_AUTH_USER;
constructor(readonly payload: Partial<AuthUser>) {}
}
export class ActionAuthUpdateLastPublicDashboardId implements Action { export class ActionAuthUpdateLastPublicDashboardId implements Action {
readonly type = AuthActionTypes.UPDATE_LAST_PUBLIC_DASHBOARD_ID; readonly type = AuthActionTypes.UPDATE_LAST_PUBLIC_DASHBOARD_ID;
@ -85,4 +92,5 @@ export class ActionPreferencesDeleteUserSettings implements Action {
export type AuthActions = ActionAuthAuthenticated | ActionAuthUnauthenticated | export type AuthActions = ActionAuthAuthenticated | ActionAuthUnauthenticated |
ActionAuthLoadUser | ActionAuthUpdateUserDetails | ActionAuthUpdateLastPublicDashboardId | ActionAuthUpdateHasRepository | ActionAuthLoadUser | ActionAuthUpdateUserDetails | ActionAuthUpdateLastPublicDashboardId | ActionAuthUpdateHasRepository |
ActionPreferencesUpdateOpenedMenuSection | ActionPreferencesPutUserSettings | ActionPreferencesDeleteUserSettings; ActionPreferencesUpdateOpenedMenuSection | ActionPreferencesPutUserSettings | ActionPreferencesDeleteUserSettings |
ActionAuthUpdateAuthUser;

View File

@ -58,6 +58,10 @@ export const authReducer = (
case AuthActionTypes.UPDATE_USER_DETAILS: case AuthActionTypes.UPDATE_USER_DETAILS:
return { ...state, ...action.payload}; return { ...state, ...action.payload};
case AuthActionTypes.UPDATE_AUTH_USER:
const authUser = {...state.authUser, ...action.payload};
return { ...state, ...{ authUser }};
case AuthActionTypes.UPDATE_LAST_PUBLIC_DASHBOARD_ID: case AuthActionTypes.UPDATE_LAST_PUBLIC_DASHBOARD_ID:
return { ...state, ...action.payload}; return { ...state, ...action.payload};

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,7 @@ export class AuthService {
} else { } else {
this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, true); this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, true);
} }
this.updatedAuthUserFromToken(loginResponse.token);
this.refreshTokenSubject.next(loginResponse); this.refreshTokenSubject.next(loginResponse);
this.refreshTokenSubject.complete(); this.refreshTokenSubject.complete();
this.refreshTokenSubject = null; this.refreshTokenSubject = null;
@ -493,6 +499,18 @@ export class AuthService {
return response; 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<void> { private validateJwtToken(doRefresh): Observable<void> {
const subject = new ReplaySubject<void>(); const subject = new ReplaySubject<void>();
if (!AuthService.isTokenValid('jwt_token')) { if (!AuthService.isTokenValid('jwt_token')) {

View File

@ -30,6 +30,7 @@ import { ActionSettingsChangeLanguage } from '@core/settings/settings.actions';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { isDefinedAndNotNull } from '@core/utils'; import { isDefinedAndNotNull } from '@core/utils';
import { getCurrentAuthUser } from '@core/auth/auth.selectors'; import { getCurrentAuthUser } from '@core/auth/auth.selectors';
import { AuthService } from '@core/auth/auth.service';
@Component({ @Component({
selector: 'tb-profile', selector: 'tb-profile',
@ -47,6 +48,7 @@ export class ProfileComponent extends PageComponent implements OnInit, HasConfir
constructor(protected store: Store<AppState>, constructor(protected store: Store<AppState>,
private route: ActivatedRoute, private route: ActivatedRoute,
private userService: UserService, private userService: UserService,
private authService: AuthService,
private translate: TranslateService, private translate: TranslateService,
public fb: UntypedFormBuilder) { public fb: UntypedFormBuilder) {
super(store); super(store);
@ -94,6 +96,7 @@ export class ProfileComponent extends PageComponent implements OnInit, HasConfir
lastName: user.lastName, 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);
} }
); );
} }