Open Ivo-Evans opened 4 years ago
export default function validateExp(exp) {
const date = new Date();
const now = Math.round(date.getTime() / 1000); // JavaSript dates are accurate to the millisecond but the JWT standard specifies dates accurate to the second
return Number(exp) > now;
}
Sweet cheers for this @Ivo-Evans really useful :)
What we did is write a component called ProtectedRoute that either returned a Route or a Redirect depending on whether the user was logged in. If they were logged in it returned the route, otherwise a redirect to the login page. We then put it in our router switch statement whenever we wanted a protected route. The component ended up looking like this:
And our
authorised()
function looked like thisvalidateExp is another one we wrote ourselves. It compares the time in the JWT against the present.
This method doesn't stop a hacker getting to the route. That would require a server request. It just stops a normal user who isnt logged in getting to a route, so it makes routes semi-protected. For us, the specifics, like the cards on the page, required a server request, and there we checked that the JWT was verified