2019-05-14 22:47:04 +01:00
|
|
|
import * as React from 'react';
|
|
|
|
import history from '../history'
|
|
|
|
import { withNotifier } from '../components/SnackbarNotification';
|
2019-05-19 17:51:57 +01:00
|
|
|
import { VERIFY_AUTHORIZATION_ENDPOINT } from '../constants/Endpoints';
|
|
|
|
import { ACCESS_TOKEN, authorizedFetch } from './Authentication';
|
2019-05-14 22:47:04 +01:00
|
|
|
import { AuthenticationContext } from './Context';
|
|
|
|
import jwtDecode from 'jwt-decode';
|
2019-05-19 17:51:57 +01:00
|
|
|
import CircularProgress from '@material-ui/core/CircularProgress';
|
|
|
|
import Typography from '@material-ui/core/Typography';
|
|
|
|
import { withStyles } from '@material-ui/core/styles';
|
|
|
|
|
|
|
|
const styles = theme => ({
|
|
|
|
loadingPanel: {
|
|
|
|
padding: theme.spacing.unit * 2,
|
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
|
|
|
height: "100vh",
|
|
|
|
flexDirection: "column"
|
|
|
|
},
|
|
|
|
progress: {
|
|
|
|
margin: theme.spacing.unit * 4,
|
|
|
|
}
|
|
|
|
});
|
2019-05-14 22:47:04 +01:00
|
|
|
|
|
|
|
class AuthenticationWrapper extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.refresh = this.refresh.bind(this);
|
|
|
|
this.signIn = this.signIn.bind(this);
|
|
|
|
this.signOut = this.signOut.bind(this);
|
|
|
|
this.state = {
|
|
|
|
context: {
|
|
|
|
refresh: this.refresh,
|
|
|
|
signIn: this.signIn,
|
|
|
|
signOut: this.signOut
|
|
|
|
},
|
|
|
|
initialized: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
|
|
|
{this.state.initialized ? this.renderContent() : this.renderContentLoading()}
|
|
|
|
</React.Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContent() {
|
|
|
|
return (
|
|
|
|
<AuthenticationContext.Provider value={this.state.context}>
|
|
|
|
{this.props.children}
|
|
|
|
</AuthenticationContext.Provider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContentLoading() {
|
2019-05-19 17:51:57 +01:00
|
|
|
const { classes } = this.props;
|
2019-05-14 22:47:04 +01:00
|
|
|
return (
|
2019-05-19 17:51:57 +01:00
|
|
|
<div className={classes.loadingPanel}>
|
|
|
|
<CircularProgress className={classes.progress} size={100} />
|
|
|
|
<Typography variant="h4" >
|
|
|
|
Loading...
|
|
|
|
</Typography>
|
|
|
|
</div>
|
2019-05-14 22:47:04 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() {
|
|
|
|
var accessToken = localStorage.getItem(ACCESS_TOKEN);
|
|
|
|
if (accessToken) {
|
2019-05-19 17:51:57 +01:00
|
|
|
authorizedFetch(VERIFY_AUTHORIZATION_ENDPOINT)
|
|
|
|
.then(response => {
|
|
|
|
const jwt = response.status === 200 ? jwtDecode(accessToken) : undefined;
|
|
|
|
this.setState({ initialized: true, context: { ...this.state.context, jwt } });
|
|
|
|
}).catch(error => {
|
|
|
|
this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } });
|
|
|
|
this.props.raiseNotification("Error verifying authorization: " + error.message);
|
|
|
|
});
|
2019-05-14 22:47:04 +01:00
|
|
|
} else {
|
2019-05-19 17:51:57 +01:00
|
|
|
this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } });
|
2019-05-14 22:47:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signIn(accessToken) {
|
|
|
|
try {
|
|
|
|
this.setState({ context: { ...this.state.context, jwt: jwtDecode(accessToken) } });
|
|
|
|
localStorage.setItem(ACCESS_TOKEN, accessToken);
|
|
|
|
} catch (err) {
|
2019-05-19 17:51:57 +01:00
|
|
|
this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } });
|
|
|
|
this.props.raiseNotification("Failed to parse JWT " + err.message);
|
2019-05-14 22:47:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signOut() {
|
|
|
|
localStorage.removeItem(ACCESS_TOKEN);
|
|
|
|
this.setState({
|
|
|
|
context: {
|
|
|
|
...this.state.context,
|
2019-05-19 17:51:57 +01:00
|
|
|
jwt: undefined
|
2019-05-14 22:47:04 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.props.raiseNotification("You have signed out.");
|
|
|
|
history.push('/');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-19 17:51:57 +01:00
|
|
|
export default withStyles(styles)(withNotifier(AuthenticationWrapper))
|