# Load the State from the Session

**URL:** <https://discourse.sst.dev/t/load-the-state-from-the-session/157>\
**Category:** Chapter Comments\
**Created:** [May 9, 2018, 8:16pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157 "2018-05-09T20:16:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:16pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/1 "2018-05-09T20:16:17Z")

</div>

_From @jayair on Mon Apr 10 2017 01:03:55 GMT+0000 (UTC)_  
  
Link to chapter - [http://serverless-stack.com/chapters/load-the-state-from-the-session.html](http://serverless-stack.com/chapters/load-the-state-from-the-session.html)  
  
_Copied from original issue: [https://github.com/AnomalyInnovations/serverless-stack-com/issues/40](https://github.com/AnomalyInnovations/serverless-stack-com/issues/40)_

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:16pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/2 "2018-05-09T20:16:18Z")

</div>

_From @jayair on Thu Jul 27 2017 19:03:21 GMT+0000 (UTC)_  
  
@yashg5 The token does expire after an hour but the way the app is set up, when the user refreshes the page we check the session in the Local Storage (the SDK does this) and re-load it.

However, there is a corner case that we are not handling. If the user does not refresh the page within an hour, then the token will expire and you’ll get an error. We are going to put out an update to address this soon. But you can easily handle this yourself.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:16pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/3 "2018-05-09T20:16:18Z")

</div>

_From @teejK on Sat Oct 07 2017 00:00:04 GMT+0000 (UTC)_  
  
Hey, I’m new to javascript, so maybe this is a noob question.  
But, we seem to define componentDidMount without calling it?

Do async class methods get called automatically? I can’t seem to suss out where it gets called.

EDIT: Nevermind, found it:

> **[React.Component – React](https://reactjs.org/docs/react-component.html)**
>
> A JavaScript library for building user interfaces

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:16pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/4 "2018-05-09T20:16:18Z")

</div>

_From @jayair on Sat Oct 07 2017 01:38:49 GMT+0000 (UTC)_  
  
@teejK No worries. Yup part of the React component lifecycle.

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:16pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/5 "2018-05-09T20:16:19Z")

</div>

_From @TheDagger on Sun Dec 10 2017 21:58:37 GMT+0000 (UTC)_  
  
I have a newbie question as well, the line `!this.state.isAuthenticating &&` proceeds after the && to follow with the page to be rendered and a bunch of the html’ish code. How is this working exactly? The way I understand return functions is they return content, true or false. So if the state is currently authenticating it will return true && the content? How do you return both? This is the part that doesn’t make sense to me. Likewise how does it return false and the content at the same time? Can a return statement return 2 things back like that? I come from PHP where you could only return 1 thing or you’d have to return a JSON or Array to return multiple types

---

<div class="post-metadata">

**Author:** ![anomalyinnovations](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/anomalyinnovations/32/10_2.png) [@anomalyinnovations](https://discourse.sst.dev/u/anomalyinnovations)\
**Post date:** [May 9, 2018, 8:16pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/6 "2018-05-09T20:16:19Z")

</div>

_From @jayair on Mon Dec 11 2017 11:59:23 GMT+0000 (UTC)_  
  
@TheDagger Yeah that line is basically using [short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation) to conditionally render our component.

> The short-circuit expression `x and y` is equivalent to the conditional expression `if x then y else false`…

So if `!this.state.isAuthenticating` is true then render our component (return the JSX) or just return false.

Here is a bit more info on it - [https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator](https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator)

Hopefully that makes sense.

---

<div class="post-metadata">

**Author:** ![esbenvb](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/esbenvb/32/126_2.png) [@esbenvb](https://discourse.sst.dev/u/esbenvb)\
**Post date:** [June 20, 2018, 7:54pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/7 "2018-06-20T19:54:03Z")

</div>

```auto
const childProps = {
    isAuthenticated: this.state.isAuthenticated,
    userHasAuthenticated: this.userHasAuthenticated
  };

```

Wouldn’t you normally use a Redux Store for sharing the state across containers, rather than depend on props in so many layers?

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [June 21, 2018, 5:36pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/8 "2018-06-21T17:36:39Z")

</div>

It is okay to [lift your state up this way](https://reactjs.org/docs/lifting-state-up.html). You don’t have to rely on Redux for this. Redux can be quite useful but might not be a great choice when you are first starting out. This article can help you decide if you want to use Redux.

> **[You Might Not Need Redux](https://medium.com/@dan_abramov/you-might-not-need-redux-be46360cf367)**
>
> People often choose Redux before they need it. “What if our app doesn’t scale without it?” Later, developers frown at the indirection Redux…

---

<div class="post-metadata">

**Author:** ![AlacrossWW](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@AlacrossWW](https://discourse.sst.dev/u/AlacrossWW)\
**Post date:** [June 27, 2018, 11:17pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/9 "2018-06-27T23:17:55Z")

</div>

I haven’t been able to get the session loading working properly. I have no clue what’s wrong. I have gone to the point where I literally copy and pasted the file from github and it still does not save my session upon refresh.

I confirmed that isAuthenticated is changing to false after the refresh.

Any ideas? the only page that was edited in this chapter is App.js and I literally have that copied off of the github page for this project

---

<div class="post-metadata">

**Author:** ![AlacrossWW](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@AlacrossWW](https://discourse.sst.dev/u/AlacrossWW)\
**Post date:** [June 28, 2018, 1:32pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/10 "2018-06-28T13:32:26Z")

</div>

The “isAuthenticated” variable is true but nothing is saved to local storage

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [June 29, 2018, 8:54pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/11 "2018-06-29T20:54:35Z")

</div>

What is not working for the session loading? It doesn’t log the user out or doesn’t keep them logged in?

---

<div class="post-metadata">

**Author:** ![AlacrossWW](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@AlacrossWW](https://discourse.sst.dev/u/AlacrossWW)\
**Post date:** [July 2, 2018, 12:41pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/12 "2018-07-02T12:41:43Z")

</div>

It logs the user out upon refresh

---

<div class="post-metadata">

**Author:** ![AlacrossWW](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@AlacrossWW](https://discourse.sst.dev/u/AlacrossWW)\
**Post date:** [July 2, 2018, 5:57pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/13 "2018-07-02T17:57:24Z")

</div>

Figured it out. I had gone back and manually created a user in the console. This meant that my user was still on status FORCE\_CHANGE\_PASSWORD. That was the problem ☹

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [July 3, 2018, 7:09pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/14 "2018-07-03T19:09:23Z")

</div>

I see. Glad you figured it out.

---

<div class="post-metadata">

**Author:** ![jplds](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jplds](https://discourse.sst.dev/u/jplds)\
**Post date:** [July 25, 2018, 9:35pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/15 "2018-07-25T21:35:07Z")

</div>

Thanks for clearing this. I ran into the same issue.

This is quite a weird case as the account seems to be in limbo - it can be signed in without errors, but is unusable (at least in this Lessons context).

---

<div class="post-metadata">

**Author:** ![ehog213](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@ehog213](https://discourse.sst.dev/u/ehog213)\
**Post date:** [April 6, 2019, 9:44pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/16 "2019-04-06T21:44:27Z")

</div>

Hi, I have a question regarding Auth.currentSession(). It always returns a state of “rejected” with the reason being “No userPool”.

I have checked both of my Cognito User Pool and my Identity Pool. The [admin@example.com](mailto:admin@example.com) user has an Account Status of “Enabled / CONFIRMED” and the Identity Pool has been properly configured.

I also tried testing Invoking the API Gateway using the IAM Policy Simulator and when I ran the simulation, I got “allowed” as the result so it seems that it should be fine. But when I tried actually testing it out on the browser, I always get the “no userPool” error upon the component mounting.

I was wondering if you guys have encountered this problem before or know of a way to fix it?

---

<div class="post-metadata">

**Author:** ![ehog213](https://avatars.discourse-cdn.com/v4/letter/e/9dc877/32.png) [@ehog213](https://discourse.sst.dev/u/ehog213)\
**Post date:** [April 6, 2019, 10:29pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/17 "2019-04-06T22:29:44Z")

</div>

Just an update to the situation. I seemed to have found out the reason why it wasn’t working. I did not configure Amplify before rendering App.

I mistakenly added Amplify.configure() below ReactDOM.render() in the index.js file. After moving Amplify.configure() above ReactDOM.render(), the “No userPool” error went away.

---

<div class="post-metadata">

**Author:** ![jayair](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jayair/32/9_2.png) [@jayair](https://discourse.sst.dev/u/jayair)\
**Post date:** [April 6, 2019, 10:49pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/18 "2019-04-06T22:49:25Z")

</div>

Oh good catch. Glad you figured it out.

---

<div class="post-metadata">

**Author:** ![ztc212](https://avatars.discourse-cdn.com/v4/letter/z/5e9695/32.png) [@ztc212](https://discourse.sst.dev/u/ztc212)\
**Post date:** [August 20, 2019, 9:05pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/19 "2019-08-20T21:05:47Z")

</div>

While I understand what the conditional expression is supposed to accomplish here, I am confused about the actual implementation. The docs state `isAuthenticating` it’s there because “loading the user session is an asynchronous process” but we’re using `await` before `Auth.currentSession()` and then setting `state.isAuthenticated` to `true` via `userHasAuthenticated(true)` so shouldn’t the user session be guaranteed to have already loaded by the time isAuthenticating is set to false? I also see that `userHasAuthenticated()` calls `setState()` which we can’t be sure will run right away due to batching, but I don’t see anything else that triggers another `render()` prior to that specific `setState()` being set…?

Separate but related, the only time `state.isAuthenticating = true` is right after the App is constructed. Assuming someone explains what I am missing above, shouldn’t we set this back to `true` at some point when the user logs out in preparation for another async user session call?

---

<div class="post-metadata">

**Author:** ![jimgroome](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/jimgroome/32/589_2.png) [@jimgroome](https://discourse.sst.dev/u/jimgroome)\
**Post date:** [August 28, 2019, 2:14pm UTC](https://discourse.sst.dev/t/load-the-state-from-the-session/157/20 "2019-08-28T14:14:09Z")

</div>

Just thought I’d chime in here to say I had the same issue, which I fixed by changing my user’s password.

AWS wouldn’t let me do it via the console, so I ended up working out how to do it via the CLI.

For anyone else who wants to do this, here’s the command to run:

```bash
aws cognito-idp admin-set-user-password --user-pool-id [User pool ID] --username [Username] --password [New password] --permanent

```

[Next page](https://discourse.sst.dev/t/load-the-state-from-the-session/157.md?page=2)
