Initial commit
This commit is contained in:
53
website/src/Auth/ProtectedRoute.js
Normal file
53
website/src/Auth/ProtectedRoute.js
Normal file
@@ -0,0 +1,53 @@
|
||||
import React from 'react'
|
||||
import { Route, Redirect } from 'react-router-dom'
|
||||
import { PropTypes } from 'prop-types'
|
||||
import { api } from '../helpers'
|
||||
|
||||
export class ProtectedRoute extends React.Component {
|
||||
static propTypes = {
|
||||
roles: PropTypes.array,
|
||||
location: PropTypes.shape({
|
||||
pathname: PropTypes.string,
|
||||
search: PropTypes.string
|
||||
})
|
||||
}
|
||||
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.updateComponent = this.updateComponent.bind(this)
|
||||
}
|
||||
|
||||
updateComponent() {
|
||||
this.forceUpdate()
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
api.addListener('login', this.updateComponent)
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
api.removeListener('login', this.updateComponent)
|
||||
}
|
||||
|
||||
render(props) {
|
||||
let user = api.loggedInUser
|
||||
|
||||
if (user) {
|
||||
if (user.pending) {
|
||||
// The Api might be in the middle of fetching the user information
|
||||
return <div />
|
||||
}
|
||||
let roles = this.props.roles
|
||||
|
||||
if (!roles || roles.includes(user.role)) {
|
||||
return <Route {...this.props} />
|
||||
} else if (!!user.role && user.role === 'broker') {
|
||||
return <Redirect to='/broker-dashboard' />
|
||||
} else if (!!user.role && (user.role === 'employee' || 'administrator' || 'executive')) {
|
||||
return <Redirect to='/dashboard' />
|
||||
}
|
||||
} else {
|
||||
return <Redirect to={`/login?redirect=${this.props.location.pathname}${this.props.location.search}`} />
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user