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')) {