amplify-js: Hub TypeError: Cannot read property 'setItem' of undefined (AuthClass)

Describe the bug It seems import {AuthClass} from '@aws-amplify/auth'; is defining an instance of AuthClass without any configuration (new AuthClass(null)) as the default instance via these lines https://github.com/aws-amplify/amplify-js/blob/master/packages/auth/src/index.ts#L24.

The AuthClass listens to Hub via these lines, https://github.com/aws-amplify/amplify-js/blob/master/packages/auth/src/Auth.ts#L111-L125 and the storage here would be null throwing a null pointer exception which can lead to console errors like:

[ERROR] 27:39.356 Hub TypeError: Cannot read property 'setItem' of undefined
    at Object.callback (Auth.js:95)
    at Hub.js:122
    at Array.forEach (<anonymous>)
    at HubClass.push../node_modules/@aws-amplify/core/lib/Hub.js.HubClass._toListeners (Hub.js:119)
    at HubClass.push../node_modules/@aws-amplify/core/lib/Hub.js.HubClass.dispatch (Hub.js:77)
    at dispatchAuthEvent (Auth.js:61)
    at AuthClass.<anonymous> (Auth.js:387)
    at step (Auth.js:44)
    at Object.next (Auth.js:25)
    at fulfilled (Auth.js:16)

In my case, I define my own instance of the AuthClass with the proper configuration options, so really this Hub listener is called twice for both instances. My instance that I defined with all the proper configuration seems to have storage correctly configured there and does not throw the error.

To Reproduce Steps to reproduce the behavior:

  1. Use a second AuthClass instance for managing your user pool.

Expected behavior It does not create a default instance? Or maybe it handles the null pointer without exception?

Screenshots

Desktop (please complete the following information):

  • OS: Mac OS
  • Browser Chrome
  • Version 74

Additional context

Sample code

You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = ‘DEBUG’; in your app.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 17 (3 by maintainers)

Most upvoted comments

Soooo…this is the problem i had, and i’ll try to phrase it in a nicer way than i did to mean at the time.

i was using the api and the auth. from their modules

import API, { graphqlOperation } from ‘@aws-amplify/API’;

API.configure(…)

and import Auth from ‘@aws-amplify/Auth’; API.configure(…)

this would throw error in console because api internally uses auth as well and initiate a new instance of auth with null config… problem, but not a huge problem … everything still works

But, when you have API using cognito userpool as authentication it wouldnt work because again API creates its own instance of Auth with null configuration.

the alternative is to use the whole of amplify, which is not tree shakable, it builds with everything…everything, my build size increases by 5mb!

Yeah, if you start to modularise the API and the other parts of Amplify, why not AUTH. We only use AUTH and API. I don’t want the rest polluting my bundle. It is already over 5 mb because of aws-amplify. When can we expect @amplify/auth to be released? It is still sitting at version 1.6.3?