javascriptreactjsreact-nativeaws-amplifyamplifyjs

Amplify JS no longer working as a node module


I created a module for use across a web project and a react-native project which was working fine until I updated all my dependencies recently to use Amplify v6

I really didn't want to be repeating boilerplate code across multiple codebases when a lot of it can be much cleaner with my module.

In essence I have a web app which will has some admin functionality, that uses amplify graphql & cognito.

I also have a react native app that uses the same amplify project for users (cognito, graphql, storage etc)

To avoid duplicate functions like all of the auth implementations / API queries and have the ability to have a web version of the native app just incase, I created a private node module full of helper functions.

I then include my module as a dependency in my web react app and react native app which makes total sense to me.

This was all working fine until the amplify-js v6 update and now my web app keeps giving me AuthUserPoolException: Auth UserPool not configured. errors.

Ironically it still appears to be working in react native however I am worried it will break sometime soon and there doesn't seem to be a reason why.

Has anyone else used amplify in this way?

Any help would be greatly appreciated.


Solution

  • OK I found a solution.

    Not really sure how my module has worked up until this point but there we go.

    The module itself needs to connect to amplify but for some reason up until this point, I've never needed to configure it.

    I guess because the native app and web app call Amplify.configure and maybe that passed itself down.

    Today I added a function to the module itself:

    import amplifyconfig from './amplifyconfiguration.json';
    
    const setupCommonAmplifyModule = (): void => {
      Amplify.configure(amplifyconfig);
      const amplifyConfig = Amplify.getConfig();
      console.log('COMMON MODULE AMPLIFY SETUP: ', amplifyConfig);
    };
    

    And then from my web app in the index file I don't bother calling Amplify.configure anymore I just call:

    import {setupCommonAmplifyModule} from '@me/my-module'
    
    setupCommonAmplifyModule()
    

    Now all of my amplify related module functions appear to be working again.

    Hopefully this helps anyone else who wants to offset amplify app functions into a common module for use across multiple applications.