Fix admin/user login issues

This commit is contained in:
John Lyon-Smith
2018-05-25 10:16:28 -07:00
parent a33ca57d58
commit 587052a509
22 changed files with 255 additions and 92 deletions

View File

@@ -1,8 +1,7 @@
import React from 'react'
import { Route, Redirect } from 'react-router'
import { PropTypes } from 'prop-types'
import { api } from 'src/API'
import autobind from 'autobind-decorator'
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 = {
@@ -13,33 +12,32 @@ export class ProtectedRoute extends React.Component {
admin: PropTypes.bool,
}
@autobind
updateComponent() {
this.forceUpdate()
}
componentDidMount() {
api.addListener("login", this.updateComponent)
api.addListener("logout", this.updateComponent)
}
componentWillUnmount() {
api.removeListener("login", this.updateComponent)
api.removeListener("logout", this.updateComponent)
}
render(props) {
const user = api.loggedInUser
if (user.pending) {
return null
// 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 (
<Redirect
to={`/login?redirect=${this.props.location.pathname}${
this.props.location.search
}`}
<Route
render={() => (
<Redirect
to={`/login?redirect=${this.props.location.pathname}${
this.props.location.search
}`}
/>
)}
/>
)
} else {