react-check-auth
react-check-auth copied to clipboard
no request when no token
Hi is there a way, to not to do a request when there is no jwt token in localStorage? It should just render the login page
Now it is doing an unneccessary empty request, and the response is also empty. so the userInfo is null. We should be able to skip that.., do you know how?
What you can do is, wrap with AuthProvider
only when the token is available in the localStorage
. Otherwise don't wrap with AuthProvider
?
For example: if you have a Login
component which might need to check whether user has a valid session or not depending on whether token is available in the localStorage
, here is how the React
code will look like
...
...
const token = ''; // Retrieve from the localStorage
{ token ? (
<AuthProvider authUrl={'https://website.com/get/userInfo'}>
<Login { ...props } />
</AuthProvider>
) : (
<Login />
) }
...
...
Hope this helps. Feel free to reach out to us if you face any issues.
My whole application is wrapped with the AuthProvider which has my App Router beneath. Actually a prop in the AuthProvider could fix this very easily.. Like makeRequest={true || false} or so..