# Remember logged in user with React

**URL:** <https://community.auth0.com/t/remember-logged-in-user-with-react/55404>\
**Category:** Get Help\
**Tags:** react, auth0\
**Created:** [December 28, 2020, 11:13am UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404 "2020-12-28T11:13:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ReneCode](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/renecode/32/26531_2.png) [@ReneCode](https://community.auth0.com/u/ReneCode)\
**Post date:** [December 28, 2020, 11:13am UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/1 "2020-12-28T11:13:59Z")

</div>

I took the template to start with auth0 and React SPA. It works fine.

After refreshing the page I can’t get the previous logged-in user. (to do so I have to `loginWithRedirect()` that works fine).

I don’t want the user to force login, but if the user was logged in and refreshes the page I want the user to be logged in automaticly.

How to do so?

---

<div class="post-metadata">

**Author:** ![Saltuk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/saltuk/32/14364_2.png) [@Saltuk](https://community.auth0.com/u/Saltuk)\
**Post date:** [December 28, 2020, 6:46pm UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/2 "2020-12-28T18:46:21Z")

</div>

Hi @ReneCode

Though the reference app on this Github issue has changed a little bit since then, the [code snippet on the issue](https://github.com/auth0/auth0-spa-js/issues/58#issuecomment-510655826) should be helpful.

After page refresh and the components are mounted, you need to call `getTokenSilently()`. If there is a valid session for the user, this will return the tokens.

---

<div class="post-metadata">

**Author:** ![ReneCode](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/renecode/32/26531_2.png) [@ReneCode](https://community.auth0.com/u/ReneCode)\
**Post date:** [January 1, 2021, 2:18pm UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/3 "2021-01-01T14:18:10Z")

</div>

hi @Saltuk,  
thanks for your hint - but unfortunately it does not work.  
I use it with react, so using @auth0-react package.

after refresh the page it throws an error `Login required`.  
try-catch that error does not help.

How can I check if there is a valid session ?

```auto
  const { isLoading, getAccessTokenSilently } = useAuth0();
  useEffect(() => {
    const callAPI = async () => {
        const token = await getAccessTokenSilently();
    };
    if (!isLoading) {
      callAPI();
    }
  }, [isLoading, getAccessTokenSilently]);

```

---

<div class="post-metadata">

**Author:** ![Saltuk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/saltuk/32/14364_2.png) [@Saltuk](https://community.auth0.com/u/Saltuk)\
**Post date:** [January 1, 2021, 5:11pm UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/4 "2021-01-01T17:11:49Z")

</div>

Hi @ReneCode

Could you send me a sample HAR file with a direct message showing the issue? Please capture the initial login as well as the silent token renewal request after the page refresh that fails. You may find how to capture the HAR file [here](https://auth0.com/docs/troubleshoot/generate-and-analyze-har-files).

---

<div class="post-metadata">

**Author:** ![Saltuk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/saltuk/32/14364_2.png) [@Saltuk](https://community.auth0.com/u/Saltuk)\
**Post date:** [January 6, 2021, 8:26pm UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/7 "2021-01-06T20:26:24Z")

</div>

Hi, @ReneCode I’m sharing a brief note as the issue was resolved by switching the application to support refresh tokens and storing the tokens in the local storage. React SDK is using the auth0-spa-js so the following links were useful.

> **[Auth0 Single Page App SDK](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#change-storage-options)**
>
> Auth0 SDK for single page applications using Authorization Code Grant Flow with PKCE.

> **[Auth0 Single Page App SDK](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#use-rotating-refresh-tokens)**
>
> Auth0 SDK for single page applications using Authorization Code Grant Flow with PKCE.

---

<div class="post-metadata">

**Author:** ![ReneCode](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/renecode/32/26531_2.png) [@ReneCode](https://community.auth0.com/u/ReneCode)\
**Post date:** [January 7, 2021, 7:32am UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/8 "2021-01-07T07:32:32Z")

</div>

I think a hint in the “Quick Start” repo would be helpful

> **[auth0-react-samples/Sample-01 at master · auth0-samples/auth0-react-samples](https://github.com/auth0-samples/auth0-react-samples/tree/master/Sample-01)**
>
> master/Sample-01

may be I will create a PR.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [January 7, 2021, 8:48am UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/9 "2021-01-07T08:48:55Z")

</div>

Thanks! Please share the link to it once you have it so we can share it with the repo maintainers!

---

<div class="post-metadata">

**Author:** ![ReneCode](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/renecode/32/26531_2.png) [@ReneCode](https://community.auth0.com/u/ReneCode)\
**Post date:** [January 7, 2021, 4:58pm UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/10 "2021-01-07T16:58:26Z")

</div>

[https://github.com/auth0-samples/auth0-react-samples/pull/215](https://github.com/auth0-samples/auth0-react-samples/pull/215)

(The circleci checks seems to be “hanging”.)

---

<div class="post-metadata">

**Author:** ![steve.hobbs](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/steve.hobbs/32/13234_2.png) [@steve.hobbs](https://community.auth0.com/u/steve.hobbs)\
**Post date:** [January 8, 2021, 12:07pm UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/11 "2021-01-08T12:07:21Z")

</div>

Thanks for raising this @ReneCode; I have responded on the GitHub PR but will relay my answer here as well so that it’s more widely available.

For posterity, the documentation you would like to the sample reads:

> By default the token is stored in memory  
> If you want to be logged in automaticly after refreshing the web-page  
> the token has to be stored in “localstorage”

This unfortunately doesn’t tell the whole story about why a user might not be automatically logged in on page refresh, and it only delays the problem. What I mean is that there are a few reasons why you might not be automatically signed in on page refresh, including:

- Third-party cookies being blocked in the browser (Safari, Brave, etc)
- Classic login page being used with a social provider (such as Google)
- Improper configuration in your Auth0 app

Also, when the token you’re storing in local storage expires, you will end up in exactly the same situation as you are now. To properly support automatic login across page refreshes in browsers that block third-party cookies, you will need to use [rotating refresh tokens](https://auth0.com/docs/libraries/auth0-single-page-app-sdk#use-rotating-refresh-tokens), or use a [custom domain](https://auth0.com/docs/custom-domains) (a paid feature).

The point is, that there are many caveats to this problem that is difficult to convey in a code comment and the information would be better boosted elsewhere. For example, we could instead provide a link to more information on the SDK docs for `cacheLocation` ([https://auth0.github.io/auth0-react/interfaces/auth0provideroptions.html#cachelocation](https://auth0.github.io/auth0-react/interfaces/auth0provideroptions.html#cachelocation)).

Today, we cover this information in our [auth0-spa-js FAQ](https://github.com/auth0/auth0-spa-js/blob/master/FAQ.md#why-is-the-user-logged-out-when-they-refresh-the-page-in-their-spa) which is linked to in the readme for this sample, as well as the [FAQ for auth0-react](https://github.com/auth0/auth0-react/blob/master/FAQ.md#1-user-is-not-logged-in-after-page-refresh) (which we don’t link to currently, but I will raise a PR to change that).

I can appreciate that the documentation could have been missed or confusing, as it’s not obvious that many of the responsibilities of the React library are delegated to the Auth0 SPA SDK under the hood, but that’s an area I’m going to look at now to see how we can clear that up.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [January 8, 2021, 12:24pm UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/12 "2021-01-08T12:24:02Z")

</div>

Thanks for addressing this one @steve.hobbs!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [January 23, 2021, 12:24pm UTC](https://community.auth0.com/t/remember-logged-in-user-with-react/55404/13 "2021-01-23T12:24:02Z")

</div>

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