UI: Refactoring updated Auth User in store

This commit is contained in:
Vladyslav_Prykhodko 2023-09-04 11:22:06 +03:00
parent a8a787b94e
commit 7cc087a3ce
3 changed files with 16 additions and 15 deletions

View File

@ -57,7 +57,7 @@ export class ActionAuthUpdateUserDetails implements Action {
export class ActionAuthUpdateAuthUser implements Action { export class ActionAuthUpdateAuthUser implements Action {
readonly type = AuthActionTypes.UPDATE_AUTH_USER; readonly type = AuthActionTypes.UPDATE_AUTH_USER;
constructor(readonly payload: { authUser: AuthUser }) {} constructor(readonly payload: Partial<AuthUser>) {}
} }
export class ActionAuthUpdateLastPublicDashboardId implements Action { export class ActionAuthUpdateLastPublicDashboardId implements Action {

View File

@ -59,7 +59,8 @@ export const authReducer = (
return { ...state, ...action.payload}; return { ...state, ...action.payload};
case AuthActionTypes.UPDATE_AUTH_USER: 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: case AuthActionTypes.UPDATE_LAST_PUBLIC_DASHBOARD_ID:
return { ...state, ...action.payload}; return { ...state, ...action.payload};

View File

@ -485,19 +485,7 @@ export class AuthService {
} else { } else {
this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, true); this.updateAndValidateTokens(loginResponse.token, loginResponse.refreshToken, true);
} }
const authUser = getCurrentAuthUser(this.store); this.updatedAuthUserFromToken(loginResponse.token);
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;
@ -511,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')) {