Skip to content
Open
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
21 changes: 15 additions & 6 deletions app/frontend/src/context/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import React, { useContext, createContext, useState, MouseEventHandler, useEffect } from "react";
import { AuthCodeMSALBrowserAuthenticationProvider } from "@microsoft/microsoft-graph-client/authProviders/authCodeMsalBrowser";
import { InteractionType, PublicClientApplication } from "@azure/msal-browser";
import { InteractionType, PublicClientApplication, AuthenticationResult } from "@azure/msal-browser";
import { useMsal } from "@azure/msal-react";
import { getUser, getProfilePhoto } from "../GraphService";
import { v4 as uuidv4 } from "uuid";
Expand Down Expand Up @@ -103,7 +103,7 @@ function useProvideAppContext() {
// Get the user from Microsoft Graph
const user = await getUser(authProvider);
console.log("user", user);
const avatar = await getProfilePhoto(authProvider);
const avatar = await getProfilePhoto(authProvider).catch(() => undefined);
setUser({
displayName: user.displayName || "",
email: user.mail || "",
Expand All @@ -126,6 +126,7 @@ function useProvideAppContext() {
setSessionId({
sessionId: uuidv4()
});
setIsAuthenticated(true);
}
} catch (err: any) {
displayError(err.message);
Expand All @@ -136,10 +137,18 @@ function useProvideAppContext() {
});

const signIn = async () => {
await msal.instance.loginPopup({
scopes: ["user.read"],
prompt: "select_account"
});
try {
// signin then set access token and session id after successful signin
await msal.instance.loginPopup({
scopes: ["user.read"],
prompt: "select_account"
}).then((response: AuthenticationResult) => {
setAccessToken({ accessToken: response.accessToken });
setSessionId({ sessionId: uuidv4() });
}, (error: any) => { console.log(error) });
} catch (err: any) {
displayError(err.message);
}

// Get the user from Microsoft Graph
const user = await getUser(authProvider);
Expand Down