-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathApp.jsx
More file actions
69 lines (60 loc) · 2.05 KB
/
App.jsx
File metadata and controls
69 lines (60 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React, { useState } from 'react';
import { PageLayout } from './components/PageLayout';
import { loginRequest } from './authConfig';
import { callMsGraph } from './graph';
import { ProfileData } from './components/ProfileData';
import { AuthenticatedTemplate, UnauthenticatedTemplate, useMsal } from '@azure/msal-react';
import './App.css';
import Button from 'react-bootstrap/Button';
/**
* Renders information about the signed-in user or a button to retrieve data about the user
*/
const ProfileContent = () => {
const { instance, accounts } = useMsal();
const [graphData, setGraphData] = useState(null);
function RequestProfileData() {
// Silently acquires an access token which is then attached to a request for MS Graph data
instance
.acquireTokenSilent({
...loginRequest,
account: accounts[0],
})
.then((response) => {
callMsGraph(response.accessToken).then((response) => setGraphData(response));
});
}
return (
<>
{accounts[0] && <h5 className="profileContent">Welcome {accounts[0].name}</h5>}
{graphData ? (
<ProfileData graphData={graphData} />
) : (
<Button variant="secondary" onClick={RequestProfileData}>
Request Profile
</Button>
)}
</>
);
};
/**
* If a user is authenticated the ProfileContent component above is rendered. Otherwise a message indicating a user is not authenticated is rendered.
*/
const MainContent = () => {
return (
<div className="App">
<AuthenticatedTemplate>
<ProfileContent />
</AuthenticatedTemplate>
<UnauthenticatedTemplate>
<h5 className="card-title">Please sign-in to see your profile information.</h5>
</UnauthenticatedTemplate>
</div>
);
};
export default function App() {
return (
<PageLayout>
<MainContent />
</PageLayout>
);
}