I am using Auth0 with a React Site using TypeScript, VITE and Auth0-js.
When I have left the site for the day, and then come back to it to continue development I get a 401 response from my local server.
My endpoint at the moment, only has an Authorize attribute as per the below code snippet.
[HttpGet("my/invitations/")]
[Authorize]
[SwaggerOperation(
Summary = "Get My Invitations",
Description = "Gets My Invitations",
OperationId = "IISHF.Invitations",
Tags = new[] { "IISHF Invitations" })
]
When I inspect local storage I see that I have the access token
My code for logging in is
const handleLogin = () => {
if (credentials.email === "" || credentials.password === "") {
setMessage({ type: "error", message: "Please fill all the fields" })
return;
}
if (credentials.password.length < 8) {
setMessage({ type: "error", message: "Wrong email or password" })
return;
}
setLoader(true)
auth0Login(credentials)
.then(function (response) {
localStorage.setItem('access_token', response.data.access_token)
localStorage.setItem('id_token', response.data.id_token)
localStorage.setItem('refresh_token', response.data.refresh_token)
localStorage.setItem('expires_at', (response.data.expires_in * 1000 + new Date().getTime()).toString())
dispatch(setUser())
// // navigate('/')
setTimeout(() => {
navigate('/')
}, 500)
}).catch(function (error) {
console.error(error);
setLoader(false)
setMessage({ type: "error", message: error.response.data.error_description })
});
}
I would very much appreciate to understand where and why this is an issue as when this is working, this works perfectly well.
But it appears it takes some time for me dev instance of auth0 to warm up unless Im doing something wrong.
I have since added an interceptor that looks for the 401, and then retries.
I have a confirmed bearer token after getting the 401, but still the api is not authorising \ receiving a token.
However, after a refresh this is passing the token successfully and making the same api call.