-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathSampleAppRedirectOnLaunch.js
101 lines (89 loc) · 3.1 KB
/
SampleAppRedirectOnLaunch.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
import * as React from 'react';
import { AzureAD, LoginType, AuthenticationState } from 'react-aad-msal';
import { basicReduxStore } from './reduxStore';
import GetAccessTokenButton from './GetAccessTokenButton';
import GetIdTokenButton from './GetIdTokenButton';
// Import the authentication provider which holds the default settings
import { authProvider } from './authProvider';
class SampleAppRedirectOnLaunch extends React.Component {
constructor(props) {
super(props);
// Change the login type to execute in a Redirect
const options = authProvider.getProviderOptions();
options.loginType = LoginType.Redirect;
authProvider.setProviderOptions(options);
this.interval = null;
let redirectEnabled = sessionStorage.getItem('redirectEnabled') || false;
this.state = {
counter: 5,
redirectEnabled: redirectEnabled,
};
}
handleCheck = () => {
this.setState(
state => ({
...state,
redirectEnabled: !state.redirectEnabled,
}),
() => {
if (!this.state.redirectEnabled) {
this.clearRedirectInterval();
} else {
sessionStorage.setItem('redirectEnabled', true);
}
},
);
};
countdownToLogin = loginFunction => {
if (this.state.redirectEnabled && !this.interval) {
this.interval = setInterval(() => {
if (this.state.counter > 0) {
this.setState({ counter: this.state.counter - 1 });
} else {
this.clearRedirectInterval();
this.setState({ redirectEnabled: false });
loginFunction();
}
}, 1000);
}
};
clearRedirectInterval() {
clearInterval(this.interval);
this.setState({ counter: 5 });
sessionStorage.removeItem('redirectEnabled');
this.interval = null;
}
render() {
const { redirectEnabled } = this.state;
return (
<AzureAD provider={authProvider} reduxStore={basicReduxStore}>
{({ login, logout, authenticationState }) => {
const isInProgress = authenticationState === AuthenticationState.InProgress;
const isAuthenticated = authenticationState === AuthenticationState.Authenticated;
const isUnauthenticated = authenticationState === AuthenticationState.Unauthenticated;
if (isAuthenticated) {
return (
<React.Fragment>
<p>You're logged in!</p>
<button onClick={logout} className="Button">
Logout
</button>
<GetAccessTokenButton provider={authProvider} />
<GetIdTokenButton provider={authProvider} />
</React.Fragment>
);
} else if (isUnauthenticated || isInProgress) {
this.countdownToLogin(login);
return (
<div>
<input type="checkbox" value={redirectEnabled} onChange={this.handleCheck} /> Enable redirect
{redirectEnabled && <p>Redirecting in {this.state.counter} seconds...</p>}
</div>
);
}
}}
</AzureAD>
);
}
}
export default SampleAppRedirectOnLaunch;