diff --git a/interface/src/authentication/AuthenticatedRoute.js b/interface/src/authentication/AuthenticatedRoute.js index 231eead..8461bad 100644 --- a/interface/src/authentication/AuthenticatedRoute.js +++ b/interface/src/authentication/AuthenticatedRoute.js @@ -13,7 +13,7 @@ export class AuthenticatedRoute extends React.Component { const { raiseNotification, authenticationContext, component: Component, ...rest } = this.props; const { location } = this.props; const renderComponent = (props) => { - if (authenticationContext.jwt) { + if (authenticationContext.isAuthenticated()) { return ( ); diff --git a/interface/src/authentication/AuthenticationWrapper.js b/interface/src/authentication/AuthenticationWrapper.js index 81b3fa0..258ae8e 100644 --- a/interface/src/authentication/AuthenticationWrapper.js +++ b/interface/src/authentication/AuthenticationWrapper.js @@ -27,14 +27,13 @@ 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 + signOut: this.signOut, + isAuthenticated: this.isAuthenticated, + isAdmin: this.isAdmin }, initialized: false }; @@ -72,44 +71,53 @@ class AuthenticationWrapper extends React.Component { ); } - refresh() { + refresh = () => { var accessToken = localStorage.getItem(ACCESS_TOKEN); if (accessToken) { authorizedFetch(VERIFY_AUTHORIZATION_ENDPOINT) .then(response => { - const jwt = response.status === 200 ? jwtDecode(accessToken) : undefined; - this.setState({ initialized: true, context: { ...this.state.context, jwt } }); + const user = response.status === 200 ? jwtDecode(accessToken) : undefined; + this.setState({ initialized: true, context: { ...this.state.context, user } }); }).catch(error => { - this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } }); + this.setState({ initialized: true, context: { ...this.state.context, user: undefined } }); this.props.raiseNotification("Error verifying authorization: " + error.message); }); } else { - this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } }); + this.setState({ initialized: true, context: { ...this.state.context, user: undefined } }); } } - signIn(accessToken) { + signIn = (accessToken) => { try { - this.setState({ context: { ...this.state.context, jwt: jwtDecode(accessToken) } }); + this.setState({ context: { ...this.state.context, user: jwtDecode(accessToken) } }); localStorage.setItem(ACCESS_TOKEN, accessToken); } catch (err) { - this.setState({ initialized: true, context: { ...this.state.context, jwt: undefined } }); + this.setState({ initialized: true, context: { ...this.state.context, user: undefined } }); this.props.raiseNotification("Failed to parse JWT " + err.message); } } - signOut() { + signOut = () => { localStorage.removeItem(ACCESS_TOKEN); this.setState({ context: { ...this.state.context, - jwt: undefined + user: undefined } }); this.props.raiseNotification("You have signed out."); history.push('/'); } + isAuthenticated = () => { + return this.state.context.user; + } + + isAdmin = () => { + const { context } = this.state; + return context.user && context.user.admin; + } + } export default withStyles(styles)(withNotifier(AuthenticationWrapper)) diff --git a/interface/src/authentication/UnauthenticatedRoute.js b/interface/src/authentication/UnauthenticatedRoute.js index 28fbbd6..321cdd2 100644 --- a/interface/src/authentication/UnauthenticatedRoute.js +++ b/interface/src/authentication/UnauthenticatedRoute.js @@ -8,9 +8,9 @@ import * as Authentication from './Authentication'; class UnauthenticatedRoute extends React.Component { render() { - const { component:Component, ...rest } = this.props; + const { authenticationContext, component:Component, ...rest } = this.props; const renderComponent = (props) => { - if (this.props.authenticationContext.jwt) { + if (authenticationContext.isAuthenticated()) { return (); } return (); diff --git a/interface/src/components/MenuAppBar.js b/interface/src/components/MenuAppBar.js index 2678d42..593cd49 100644 --- a/interface/src/components/MenuAppBar.js +++ b/interface/src/components/MenuAppBar.js @@ -189,7 +189,7 @@ class MenuAppBar extends React.Component { - +