How to reload localStorage saved aws-amplify auth?

Viewed 866

I am successfully using AWS Amplify and AWS Cognito for my spa authorization. The only issue is that when I refresh the page, the auth seems to be lost, even if I see amplify and cognito items in LocalStorage.

This is my configuration section:

Amplify.configure({
  Auth: {
    oauth: {
      domain: 'xxxxx.auth.eu-west-1.amazoncognito.com',
      scope: ['phone', 'email', 'profile', 'openid'],
      responseType: 'code',
      oauth.redirectSignIn: 'http://localhost:3000/',
      oauth.redirectSignOut: 'http://localhost:3000/',
    },
    region: process.env.REACT_APP_REGION,
    userPoolId: process.env.REACT_APP_USER_POOL_ID,
    userPoolWebClientId: process.env.REACT_APP_USER_POOL_WEB_CLIENT_ID,
    storage: localStorage,
  },
});

In practice, I don't know how to ask amplify to use localStorage when user is authenticated and app restarts (page reload).
Possibly I should use Auth.currentAuthenticatedUser(), but I suppose I can't call it instead of Auth.configure()...

1 Answers

You can fetch the user from storage by calling Auth.currentAuthenticatedUser().

It isn't properly documented but you can refer to the implementation to see the behavior (amplify team must believe in self-documenting code).

Related