I am working on a React project. I tried to create a protected route component for the React application that I am building, but I am not sure why I can get response.status print properly but cannot get the component return properly. Can anybody please help?
import React from 'react';
import { Route, Redirect } from 'react-router-dom';
function ProtectedRoute({ component: Component, ...rest }) {
return (
<Route {...rest} render={(props) => {
const identity = JSON.parse(localStorage.getItem('identity'));
if (identity !== null && identity !== undefined) {
if (identity.id !== null && identity.email !== null && identity.id !== undefined && identity.email !== undefined) {
// Check with backend
Api.get('/checkProtection', {
withCredentials: true,
headers: {
'identity': localStorage.getItem('identity')
}
}).then((response) => {
console.log(response.status);
return (
<Component />
);
}).catch((error) => {
console.log(error);
return (
<Redirect to={{ pathname:'/login', state: {from: props.location} }} />
);
});
} else {
return (
<Redirect to={{ pathname:'/login', state: {from: props.location} }} />
);
}
} else {
return (
<Redirect to={{ pathname:'/login', state: {from: props.location} }} />
);
}
}}
/>
);
}
export default ProtectedRoute;