Auth0 react SDK -- way to check for authed user without using client-side hooks

Is there any way at all to check if a user is authed via an API call or something? Currently we have to show loading spinners in our UI because the useAuth0 hook takes time (and is called after the component/page loads on the client) โ€“ it would make much more sense to make an api call in a route loader instead of doing this logic while we render the UI. Considering moving to session based auth for this reason but would appreciate any thoughts :slight_smile:

Notes:

  • Using an SPA with the auth0-react SDK
  • REST based backend api

Hi @dmb.programmer,

Welcome to the Auth0 Community!

I understand you are looking for a way to check if a user is authenticated with the Auth0 React SDK.

To do so, I recommend using the useAuth0 hook to call the isAuthenticated method.

May I help you with anything else?

Thanks,
Rueben

If you read my question you would understand that my goal is to check if a user is authenticated WITHOUT using the useAuth0 hook โ€“ the problem that I have is that i can only use the hooks when my pages load (I would like to be able to make the auth check anywhere (i.e in functions, route loadings, SSR, etc) โ€“ is there a way via the auth0 api to do this or something?

The useAuth0 hook has to be making some api call tho right?

Hi @dmb.programmer,

Thank you for your responses.

One workaround to using the useAuth0 hook that I can recommend is to leverage the information from the Access Token to determine if the user is authenticated.

For example, using the payload information from the Access Token, you could check if it has expired through the exp claim. If it has not yet expired, we can conclude that the user is authenticated.

Similarly, you could try using the access token to see if you can gain access to an API.

In short, you should be able to gain access if the user is authenticated (non-expired/non-revoked access token).

Does that help?

Thanks,
Rueben

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.