Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update user profile information if a different account has been used to consent to permissions #1380

Merged
merged 4 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/app/services/actions/permissions-action-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/app/services/actions/profile-action-creators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -69,6 +69,7 @@ export function getProfileInfo(): Function {

async function getProfileInformation(): Promise<IUser> {
const profile: IUser = {
id: '',
displayName: '',
emailAddress: '',
profileImageUrl: '',
Expand All @@ -78,6 +79,7 @@ async function getProfileInformation(): Promise<IUser> {
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;
Expand All @@ -90,11 +92,11 @@ async function getBetaProfile(): Promise<IBetaProfile> {
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 };
}
}

Expand Down Expand Up @@ -145,4 +147,3 @@ async function getProfileResponse(): Promise<IProfileResponse> {
response
};
}

2 changes: 1 addition & 1 deletion src/types/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export interface IProfileState {
user: IUser;
}


export interface IUser {
id: string;
displayName: string;
emailAddress: string;
profileImageUrl: string;
Expand Down