-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathApp.js
129 lines (116 loc) · 4.5 KB
/
App.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import React, { Component } from 'react';
import { AzureAD, AuthenticationState } from 'react-aad-msal';
import { basicReduxStore } from './reduxStore';
// Import the authentication provider which holds the default settings
import { authProvider } from './authProvider';
import SampleAppButtonLaunch from './SampleAppButtonLaunch';
import SampleAppRedirectOnLaunch from './SampleAppRedirectOnLaunch';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
accountInfo: null,
sampleType: null,
};
const sampleType = localStorage.getItem('sampleType');
if (sampleType) {
this.state.sampleType = sampleType;
}
}
handleClick = sampleType => {
this.setState({ sampleType });
localStorage.setItem('sampleType', sampleType);
};
render() {
let sampleBox;
switch (this.state.sampleType) {
case 'popup':
sampleBox = (
<div className="SampleBox">
<h2 className="SampleHeader">Button Login</h2>
<p>This example will launch a popup dialog to allow for authentication with Azure Active Directory</p>
<SampleAppButtonLaunch />
</div>
);
break;
case 'redirect':
sampleBox = (
<div className="SampleBox">
<h2 className="SampleHeader">Automatic Redirect</h2>
<p>
This example shows how you can use the AzureAD component to redirect the login screen automatically on
page load. Click the checkbox below to enable the redirect and refresh your browser window.
</p>
<SampleAppRedirectOnLaunch accountInfo={this.state.accountInfo} />
</div>
);
break;
default:
break;
}
return (
<div className="App">
<header className="App-header">
<h1 className="App-title">Welcome to the react-aad-msal sample</h1>
</header>
<AzureAD provider={authProvider} reduxStore={basicReduxStore}>
{({ accountInfo, authenticationState, error }) => {
return (
<React.Fragment>
{authenticationState === AuthenticationState.Unauthenticated && (
<div>
<button onClick={() => this.handleClick('popup')} className="Button">
Popup Sample
</button>{' '}
<button onClick={() => this.handleClick('redirect')} className="Button">
Redirect Sample
</button>
</div>
)}
<div className="SampleContainer">
{sampleBox}
<div className="SampleBox">
<h2 className="SampleHeader">Authenticated Values</h2>
<p>When logged in, this box will show your tokens and user info</p>
{accountInfo && (
<div style={{ wordWrap: 'break-word' }}>
<p>
<span style={{ fontWeight: 'bold' }}>ID Token:</span> {accountInfo.jwtIdToken}
</p>
<p>
<span style={{ fontWeight: 'bold' }}>Username:</span> {accountInfo.account.userName}
</p>
<p>
<span style={{ fontWeight: 'bold' }}>Access Token:</span> {accountInfo.jwtAccessToken}
</p>
<p>
<span style={{ fontWeight: 'bold' }}>Name:</span> {accountInfo.account.name}
</p>
</div>
)}
</div>
<div className="SampleBox">
<h2 className="SampleHeader">Errors</h2>
<p>If authentication fails, this box will have the errors that occurred</p>
{error && (
<div style={{ wordWrap: 'break-word' }}>
<p>
<span style={{ fontWeight: 'bold' }}>errorCode:</span> {error.errorCode}
</p>
<p>
<span style={{ fontWeight: 'bold' }}>errorMessage:</span> {error.errorMessage}
</p>
</div>
)}
</div>
</div>
</React.Fragment>
);
}}
</AzureAD>
</div>
);
}
}
export default App;