Added Header, Icon and MessageModal. Refactor screens into directories.
This commit is contained in:
41
mobile/src/Auth/ProtectedRoute.js
Normal file
41
mobile/src/Auth/ProtectedRoute.js
Normal file
@@ -0,0 +1,41 @@
|
||||
import React from 'react'
|
||||
import { Route, Redirect } from 'react-router-native'
|
||||
import { PropTypes } from 'prop-types'
|
||||
import { api } from '../API'
|
||||
import autobind from 'autobind-decorator'
|
||||
|
||||
export class ProtectedRoute extends React.Component {
|
||||
static propTypes = {
|
||||
location: PropTypes.shape({ pathname: PropTypes.string, search: PropTypes.string }),
|
||||
admin: PropTypes.bool,
|
||||
}
|
||||
|
||||
@autobind
|
||||
updateComponent() {
|
||||
this.forceUpdate()
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
api.addListener('login', this.updateComponent)
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
api.removeListener('login', this.updateComponent)
|
||||
}
|
||||
|
||||
render(props) {
|
||||
const user = api.loggedInUser
|
||||
|
||||
if (user) {
|
||||
if (user.pending) {
|
||||
// The API might be in the middle of fetching the user information
|
||||
return null
|
||||
} else if (!this.props.admin || (this.props.admin && user.administrator)) {
|
||||
return <Route {...this.props} />
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Can add redirect back in here - see website
|
||||
return <Redirect to='/login' />
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user