From 9e59cc83eb19ada52c805053c8d58e1354cc95d5 Mon Sep 17 00:00:00 2001 From: Millicent Achieng Date: Thu, 27 Jan 2022 03:02:13 +0300 Subject: [PATCH] Update user profile info if they use a different account to consent to permissions --- .../services/actions/permissions-action-creator.ts | 10 +++++++++- src/app/services/actions/profile-action-creators.ts | 11 ++++++----- src/types/profile.ts | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/app/services/actions/permissions-action-creator.ts b/src/app/services/actions/permissions-action-creator.ts index c7ee675e6..a2fc09817 100644 --- a/src/app/services/actions/permissions-action-creator.ts +++ b/src/app/services/actions/permissions-action-creator.ts @@ -20,6 +20,7 @@ import { getAuthTokenSuccess, getConsentedScopesSuccess } from './auth-action-creators'; +import { getProfileInfo } from './profile-action-creators'; import { setQueryResponseStatus } from './query-status-action-creator'; export function fetchScopesSuccess(response: object): IAction { @@ -107,12 +108,19 @@ function getPermissionsScopeType(profile: IUser | null | undefined) { } export function consentToScopes(scopes: string[]): Function { - return async (dispatch: Function) => { + return async (dispatch: Function, getState: Function) => { try { + const { profile }: IRootState = getState(); const authResponse = await authenticationWrapper.consentToScopes(scopes); if (authResponse && authResponse.accessToken) { dispatch(getAuthTokenSuccess(true)); dispatch(getConsentedScopesSuccess(authResponse.scopes)); + if ( + authResponse.account && + authResponse.account.localAccountId !== profile?.id + ) { + dispatch(getProfileInfo()); + } } } catch (error: any) { const { errorCode } = error; diff --git a/src/app/services/actions/profile-action-creators.ts b/src/app/services/actions/profile-action-creators.ts index 3366991ea..2ec49a50d 100644 --- a/src/app/services/actions/profile-action-creators.ts +++ b/src/app/services/actions/profile-action-creators.ts @@ -56,7 +56,7 @@ export function getProfileInfo(): Function { dispatch(queryRunningStatus(true)); try { const profile: IUser = await getProfileInformation(); - const {profileType, ageGroup} = await getBetaProfile(); + const { profileType, ageGroup } = await getBetaProfile(); profile.profileType = profileType; profile.ageGroup = ageGroup; profile.profileImageUrl = await getProfileImage(); @@ -69,6 +69,7 @@ export function getProfileInfo(): Function { async function getProfileInformation(): Promise { const profile: IUser = { + id: '', displayName: '', emailAddress: '', profileImageUrl: '', @@ -78,6 +79,7 @@ async function getProfileInformation(): Promise { try { query.sampleUrl = USER_INFO_URL; const { userInfo } = await getProfileResponse(); + profile.id = userInfo.id; profile.displayName = userInfo.displayName; profile.emailAddress = userInfo.mail || userInfo.userPrincipalName; return profile; @@ -90,11 +92,11 @@ async function getBetaProfile(): Promise { try { query.sampleUrl = BETA_USER_INFO_URL; const { userInfo } = await getProfileResponse(); - const ageGroup = getAgeGroup(userInfo); + const ageGroup = getAgeGroup(userInfo); const profileType = getProfileType(userInfo); - return {ageGroup, profileType}; + return { ageGroup, profileType }; } catch (error) { - return {ageGroup: 0, profileType: ACCOUNT_TYPE.MSA}; + return { ageGroup: 0, profileType: ACCOUNT_TYPE.MSA }; } } @@ -145,4 +147,3 @@ async function getProfileResponse(): Promise { response }; } - diff --git a/src/types/profile.ts b/src/types/profile.ts index 2d20c4de0..29c4c2e6d 100644 --- a/src/types/profile.ts +++ b/src/types/profile.ts @@ -18,8 +18,8 @@ export interface IProfileState { user: IUser; } - export interface IUser { + id: string; displayName: string; emailAddress: string; profileImageUrl: string;