Search code examples
javascriptreactjsamazon-web-servicesamazon-cognitoaws-amplify

aws-amplify still returning a promise when using .then on currentSession()


In React / Redux I am trying to map aws-amplify session data to props using redux connect.

My problem is that even when I use a .then I always get a promise returned, the result looks like so:

Promise {<resolved>: CognitoUserSession}
  __proto__: Promise
  [[PromiseStatus]]:"resolved"
  [[PromiseValue]]:CognitoUserSession

And the code is as follows.

import { connect } from 'react-redux';
import LoginForm from './LoginForm';
import {Auth} from 'aws-amplify';

export const mapStateToProps = (state) => { return {}; };
export const mapActionsToProps = () => {
  const session = Auth.currentSession().then((res) => { return res });
  return {
    idToken: session,
  };
};

export default connect(mapStateToProps, mapActionsToProps)(LoginForm);

I tried resolving with JSON.stringify, Promise.resolve(), etc... and I always get just the promise back.

It also doesn't seem to matter if I run it in the redux connector, or directly on the page, also tried double .then thinking it might be a promise wrapped in a promise, but that is not the case either.


Solution

  • mapActionsToProps is not the best spot to do an async fetch. I would suggest either fetching the session in the componentWillMount function of the component and store the session in the component state or create a redux thunk action that will fetch and store the session in the redux state tree.