aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

Analytics - updateEndpoint failed TypeError: Network request failed on latest version #9357

Closed paulsjohnson91 closed 2 years ago

paulsjohnson91 commented 2 years ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication, Analytics

Amplify Categories

analytics

Environment information

``` System: OS: macOS 12.0.1 CPU: (8) arm64 Apple M1 Pro Memory: 78.89 MB / 16.00 GB Shell: 5.8 - /bin/zsh Binaries: Node: 17.0.1 - /opt/homebrew/bin/node Yarn: 1.22.17 - /opt/homebrew/bin/yarn npm: 8.1.0 - /opt/homebrew/bin/npm Watchman: 2021.11.15.00 - /opt/homebrew/bin/watchman Browsers: Chrome: 96.0.4664.93 Firefox: 95.0 Safari: 15.1 npmPackages: @aws-amplify/analytics: ^5.1.8 => 5.1.8 @aws-amplify/api: ^4.0.28 => 4.0.28 @aws-amplify/auth: ^4.3.18 => 4.3.18 @aws-amplify/ui-components: ^1.9.6 => 1.9.6 @babel/core: ^7.8.4 => 7.16.5 @babel/runtime: ^7.8.4 => 7.16.5 @react-native-async-storage/async-storage: ^1.15.13 => 1.15.14 @react-native-community/checkbox: ^0.5.9 => 0.5.9 @react-native-community/datetimepicker: ^4.0.1 => 4.0.1 @react-native-community/eslint-config: ^1.1.0 => 1.1.0 @react-native-community/google-signin: ^4.0.3 => 4.0.3 @react-native-community/masked-view: ^0.1.10 => 0.1.11 @react-native-community/netinfo: ^7.1.2 => 7.1.6 @react-native-community/push-notification-ios: ^1.8.0 => 1.10.1 @react-native-community/toolbar-android: ^0.1.0-rc.2 => 0.1.0-rc.2 @react-native-firebase/analytics: ^13.1.0 => 13.1.0 @react-native-firebase/app: 13.1.0 => 13.1.0 @react-native-firebase/messaging: 13.1.0 => 13.1.0 @react-native-picker/picker: ^2.2.1 => 2.2.1 @react-navigation/bottom-tabs: 5.11.11 => 5.11.11 @react-navigation/native: 5.9.4 => 5.9.4 @react-navigation/stack: 5.14.5 => 5.14.5 @sentry/react-native: ^3.1.1 => 3.2.8 HelloWorld: 0.0.1 amazon-cognito-identity-js: ^5.2.3 => 5.2.3 amplify-ui-components-loader: undefined () aws-amplify: ^4.3.10 => 4.3.10 aws-amplify-react-native: ^5.0.5 => 5.0.5 babel-jest: ^25.1.0 => 25.5.1 (26.6.3) crypto-js: 3.3.0 => 3.3.0 eslint: ^6.8.0 => 6.8.0 fuse.js: ^6.4.0 => 6.4.6 fuzzy: ^0.1.3 => 0.1.3 fuzzy-search: ^3.2.1 => 3.2.1 hermes-inspector-msggen: 1.0.0 jest: ^26.0.1 => 26.6.3 metro-react-native-babel-preset: ^0.59.0 => 0.59.0 moment: ^2.24.0 => 2.29.1 prop-types: ^15.7.2 => 15.7.2 react: 16.13.1 => 16.13.1 react-animated: 0.1.0 react-native: 0.63.3 => 0.63.3 react-native-app-intro-slider: ^4.0.4 => 4.0.4 react-native-background-timer: 2.2.0 => 2.2.0 react-native-calendars: ^1.1129.0 => 1.1272.0 react-native-collapsible: ^1.5.2 => 1.6.0 react-native-config: ^1.2.0 => 1.4.5 react-native-deep-linking: ^2.2.0 => 2.2.0 react-native-device-info: ^8.4.1 => 8.4.8 react-native-dialog: ^5.6.0 => 5.6.0 react-native-elements: ^2.0.0 => 2.3.2 react-native-fast-image: ^8.1.5 => 8.5.11 react-native-fbsdk: ^2.0.0 => 2.0.0 react-native-floating-action: https://github.com/ramonvermeulen/react-native-floating-action.git#fixForNativeDriverWarning => 1.20.0 react-native-floating-label: ^0.2.3 => 0.2.3 react-native-gesture-handler: ^1.6.1 => 1.10.3 react-native-inappbrowser-reborn: ^3.6.3 => 3.6.3 react-native-iphone-x-helper: ^1.3.1 => 1.3.1 react-native-keychain: ^8.0.0 => 8.0.0 react-native-linear-gradient: ^2.5.6 => 2.5.6 react-native-markdown-display: ^7.0.0-alpha.2 => 7.0.0-alpha.2 react-native-modal-datetime-picker: ^10.2.0 => 10.2.0 react-native-modest-checkbox: ^3.3.0 => 3.3.0 react-native-popup-menu: ^0.15.7 => 0.15.12 react-native-push-notification: ^7.4.0 => 7.4.0 react-native-reanimated: ^1.9.0 => 1.13.3 react-native-safe-area-context: ^3.2.0 => 3.3.2 react-native-screens: ^2.8.0 => 2.18.1 react-native-share: ^3.3.2 => 3.8.5 react-native-side-drawer: ^1.2.6 => 1.2.9 react-native-simple-toast: ^1.1.2 => 1.1.3 react-native-size-matters: ^0.4.0 => 0.4.0 react-native-snap-carousel: 3.9.1 => 3.9.1 react-native-spinkit: ^1.5.0 => 1.5.1 react-native-splash-screen: ^3.2.0 => 3.3.0 react-native-svg: ^12.1.0 => 12.1.1 react-native-svg-transformer: ^0.14.3 => 0.14.3 react-native-tab-view: ^2.14.2 => 2.16.0 react-native-vector-icons: ^9.0.0 => 9.0.0 react-native-view-shot: ^3.1.2 => 3.1.2 react-native-walkthrough-tooltip: ^1.1.9 => 1.3.0 react-redux: ^7.2.0 => 7.2.6 react-test-renderer: 16.13.1 => 16.13.1 redux: ^4.0.5 => 4.1.2 redux-persist: ^6.0.0 => 6.0.0 redux-persist/integration/react: undefined () redux-saga: ^1.1.3 => 1.1.3 redux-saga/effects: undefined () redux-thunk: ^2.3.0 => 2.4.1 tcomb-form-native: ^0.6.20 => 0.6.20 url-parse: ^1.4.7 => 1.5.3 npmGlobalPackages: npm: 8.1.0 ```

Describe the bug

I've recently migrated to using the withAuthenticator built in HOC Authenticator provided by the amplify library. I have a listener set up to configure analytics once the user is logged in. When I try Analytics.configure I get the error

[ERROR] 06:06.746 AWSPinpointProvider - updateEndpoint failed TypeError: Network request failed

Previously this was working as expected.

Expected behavior

No error message, and querying the users endpoints should show the new endpoint.

Reproduction steps

Configure Auth

Auth.configure({
  // ...awsConfig,
  userPoolId: '<poolid>',
  userPoolWebClientId: '<clientid>',
  identityPoolId: '<idpoolid>',
  logins: {
    google:
      <id>,
  },
  region: 'eu-west-1',
  federatedTarget: 'COGNITO_USER_POOLS',
  authenticationFlowType: 'USER_PASSWORD_AUTH',
})

Log in using authenticator

Listened on home page for auth state change

  useEffect(() => {
    console.log('setting auth ' + authState);
    if (authState === 'signedIn') {
      const firebaseToken = await messaging().getToken();
      console.log(authData.attributes?.preferred_username);
      const analyticsConfig = {
        AWSPinpoint: {
          appId: PINPOINT_APP_ID,
          region: 'eu-west-1',
          mandatorySignIn: false,
          endpointId: 'GCM' + hashFnv32a(firebaseToken),
          endpoint: {
            optOut: 'NONE',
            channelType: 'GCM',
            attributes: {
              environment: [config.ENV],
            },
            userId: authData.attributes?.preferred_username,
            address: firebaseToken,
          },
        },
      };
      console.log(analyticsConfig);
      Analytics.configure(analyticsConfig);
      Analytics.autoTrack('session', {
        enable: true,
        provider: 'AWSPinpoint',
      });
    }
  }, [authState]);

The authData print:

{
  "username": "test@test3.com",
  "pool": {
    "userPoolId": "<poolid>",
    "clientId": "<clientid>",
    "client": {
      "endpoint": "https://cognito-idp.eu-west-1.amazonaws.com/",
      "fetchOptions": {}
    },
    "advancedSecurityDataCollectionFlag": true,
    "storage": "function MemoryStorage() {}",
    "wrapRefreshSessionCallback": "function (callback) {\n        var wrapped = function wrapped(error, data) {\n          if (data) {\n            dispatchAuthEvent('tokenRefresh', undefined, \"New token retrieved\");\n          } else {\n            dispatchAuthEvent('tokenRefresh_failure', error, \"Failed to retrieve new token\");\n          }\n\n          return callback(error, data);\n        };\n\n        return wrapped;\n      }"
  },
  "Session": null,
  "client": {
    "endpoint": "https://cognito-idp.eu-west-1.amazonaws.com/",
    "fetchOptions": {}
  },
  "signInUserSession": {
    "idToken": {
      "jwtToken": "<token>",
      "payload": {<user info>}
    },
    "refreshToken": {<token stuff>},
    "accessToken": {<token stuff>},
    "clockDrift": 0
  },
  "authenticationFlowType": "USER_SRP_AUTH",
  "storage": "function MemoryStorage() {}",
  "keyPrefix": "CognitoIdentityServiceProvider.<key>",
  "userDataKey": "CognitoIdentityServiceProvider.<key>.test@test3.com.userData",
  "attributes": {<attributes>},
  "preferredMFA": "NOMFA"
}

The config object passed to Analytics.configure

{
  "AWSPinpoint": {
    "appId": "<my app id>",
    "region": "eu-west-1",
    "mandatorySignIn": false,
    "endpointId": "GCM181518251",
    "endpoint": {
      "optOut": "NONE",
      "channelType": "GCM",
      "attributes": {
        "environment": [
          "preprod"
        ]
      },
      "userId": "d3e903cicc9d8",
      "address": "<a firebase token id>"
    }
  }
}

Code Snippet

above ^

Log output

``` [ERROR] 06:06.746 AWSPinpointProvider - updateEndpoint failed ```

aws-exports.js

provided in the example above

Manual configuration

No response

Additional configuration

No response

Mobile Device

run on android simulator

Mobile Operating System

Android 12

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

paulsjohnson91 commented 2 years ago

@chrisbonifacio , fyi. I did a little more investigating and I believe that the reason analytics failed to initialise, is that the authState was updated by Amplify to signedIn before the token was refreshed on app start. So it was being initialised with a stale token.

I've accounted for this by calling await Auth.currentAuthenticatedUser() before attepting to initialise analytics.

Maybe something to note in the documentation of the Authenticator HOC.

I'll close this ticket now.

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.