49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
import React from "react"
|
|
import { Route, Redirect } from "react-router"
|
|
import { PropTypes } from "prop-types"
|
|
import { api } from "src/API"
|
|
|
|
export class ProtectedRoute extends React.Component {
|
|
static propTypes = {
|
|
location: PropTypes.shape({
|
|
pathname: PropTypes.string,
|
|
search: PropTypes.string,
|
|
}),
|
|
admin: PropTypes.bool,
|
|
}
|
|
|
|
render(props) {
|
|
const user = api.loggedInUser
|
|
|
|
if (user.pending) {
|
|
// If login token has not yet been confirmed, park until it is and redirect back here
|
|
return (
|
|
<Route
|
|
render={() => (
|
|
<Redirect
|
|
to={`/parking?redirect=${this.props.location.pathname}`}
|
|
/>
|
|
)}
|
|
/>
|
|
)
|
|
} else {
|
|
// If we are not a user or an admin go to the login page
|
|
if (!user._id || (this.props.admin && !user.administrator)) {
|
|
return (
|
|
<Route
|
|
render={() => (
|
|
<Redirect
|
|
to={`/login?redirect=${this.props.location.pathname}${
|
|
this.props.location.search
|
|
}`}
|
|
/>
|
|
)}
|
|
/>
|
|
)
|
|
} else {
|
|
return <Route {...this.props} />
|
|
}
|
|
}
|
|
}
|
|
}
|