Answer the question
In order to leave comments, you need to log in
PrivateRoute how to implement token validation?
Hello!
The question is how to make a request for a back in PrivateRoute and, depending on the answer, render the page!
App Component
export default function App() {
return (
<Router>
<div>
<Switch>
<Route path="/login">
<LoginPage />
</Route>
<PrivateRoute path="/messenger">
<Messenger />
</PrivateRoute>
</Switch>
</div>
</Router>
);
}
function PrivateRoute({children, ...rest}) {
return (
<Route
{...rest}
render={({location}) =>
tokenStatus ? (
children
) : (
<Redirect
to={{
pathname: "/login",
state: {from: location}
}}
/>
)
}
/>
);
}
let token = Cookies.get('token');
isTokenValid(token)
.then(response => {
if (response.ok) {
// tokenStatus = true или вызов action
}
}
)
}
export function isTokenValid(token) {
return fetch(`${Url}:${Port}/is-token-valid`, {
method: 'POST',
body: JSON.stringify(token)
})
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question