react-check-auth icon indicating copy to clipboard operation
react-check-auth copied to clipboard

no request when no token

Open moparlakci opened this issue 5 years ago • 2 comments

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?

moparlakci avatar Dec 11 '18 15:12 moparlakci

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.

karthikvt26 avatar Dec 12 '18 11:12 karthikvt26

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..

moparlakci avatar Dec 22 '18 11:12 moparlakci