This commit is contained in:
2020-04-25 02:29:08 +02:00
4 changed files with 655 additions and 28 deletions

View File

@@ -16,36 +16,36 @@ import { Redirect } from "react-router-dom";
class ProtectedRoute extends Component {
constructor(props) {
super(props);
this.state = { auth: false };
this.state = { auth: null };
}
render() {
const url = "/simcompanies/API/testlogin"
const checkLogin = async (url) => {
const response = await fetch(url);
if (response.status == "200") {
this.setState = {
auth: true
}
} else {
this.setState = {
auth: false
}
}
async componentWillMount() {
const url = "/simcompanies/API/testlogin";
const response = await fetch(url);
if (response.status == "200") {
this.setState({
auth: true
});
} else {
this.setState({
auth: false
});
}
}
checkLogin(url)
const { component: Component, ...props } = this.props
render() {
const { component: Component, ...props } = this.props;
return (
<Route
{...props}
render={props => (
this.state.authenticated ?
<Component {...props} /> :
<Redirect to='/login' />
)}
/>
this.state.auth === null ?
"Loading..." :
<Route
{...props}
render={props => (
this.state.auth ?
<Component {...props} /> :
<Redirect to='/login' />
)}
/>
)
}
}