amplify-js: Auth.signIn throws (0, _getRandomBase.default) is not a function. (In '(0, _getRandomBase.default)(array.byteLength)', '(0, _getRandomBase.default)' is undefined)

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication

Amplify Categories

auth

Environment information

  System:
    OS: Windows 10 10.0.19042
    CPU: (8) x64 Intel(R) Core(TM) i5-10310U CPU @ 1.70GHz
    Memory: 1.66 GB / 7.77 GB
  Binaries:
    Node: 14.15.3 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 6.14.9 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.906.0), Chromium (89.0.774.77)
    Internet Explorer: 11.0.19041.1
  npmPackages:
    @aws-amplify/api: ^3.2.29 => 3.2.30
    @aws-amplify/auth: ^3.4.29 => 3.4.30
    @aws-amplify/pubsub: ^3.2.27 => 3.2.28
    @aws-amplify/storage: ^3.4.0 => 3.4.0
    @babel/core: ~7.9.0 => 7.9.0
    @babel/preset-typescript: ^7.13.0 => 7.13.0 (7.12.17)
    @react-native-async-storage/async-storage: ^1.13.0 => 1.15.2
    @react-native-community/datetimepicker: 3.2.0 => 3.2.0
    @react-native-community/eslint-config: ^2.0.0 => 1.1.0 (2.0.0)
    @react-native-community/eslint-plugin:  1.0.0
    @react-native-community/masked-view: 0.1.10 => 0.1.10
    @react-native-community/netinfo: 6.0.0 => 6.0.0
    @react-native-community/segmented-control: 2.2.1 => 2.2.1
    @react-navigation/bottom-tabs: ^5.10.1 => 5.11.10
    @react-navigation/native: ^5.8.1 => 5.9.4
    @react-navigation/stack: ^5.11.0 => 5.14.4
    @types/i18n-js: ^3.0.3 => 3.8.0
    @types/jest: ^26.0.15 => 26.0.22
    @types/react: ~16.9.35 => 16.9.56 (17.0.3)
    @types/react-native: ~0.63.2 => 0.63.52 (0.64.2)
    @types/react-test-renderer: ^17.0.0 => 17.0.1
    @types/yup: ^0.29.9 => 0.29.11
    @typescript-eslint/eslint-plugin: ^4.14.0 => 4.22.0 (3.10.1)
    @typescript-eslint/parser: ^4.14.0 => 4.22.0 (3.10.1)
    HelloWorld:  0.0.1
    babel-plugin-inline-dotenv: ^1.6.0 => 1.6.0
    babel-plugin-inline-view-configs:  0.0.5
    babel-plugin-module-resolver: ^4.0.0 => 4.1.0 (3.2.0)
    eslint: ^7.17.0 => 7.24.0
    eslint-plugin-jsx-a11y: ^6.4.1 => 6.4.1
    eslint-plugin-prettier: ^3.1.4 => 3.4.0 (3.1.2)
    expo: ^41.0.0 => 41.0.0
    expo-cellular: ~3.1.0 => 3.1.0
    expo-constants: ~10.1.3 => 10.1.3
    expo-localization: ~10.1.0 => 10.1.0
    expo-location: ~12.0.4 => 12.0.4
    expo-status-bar: ~1.0.4 => 1.0.4
    formik: ^2.2.6 => 2.2.6
    hermes-inspector-msggen:  1.0.0
    husky: ^6.0.0 => 6.0.0
    i18n-js: ^3.8.0 => 3.8.0
    jest-expo: ^41.0.0 => 41.0.0
    lottie-react-native: 3.5.0 => 3.5.0
    prettier: ^2.1.2 => 2.2.1
    react: 16.13.1 => 16.13.1
    react-animated:  0.1.0
    react-async-hook: ^3.6.2 => 3.6.2
    react-dom: 16.13.1 => 16.13.1
    react-native: https://github.com/expo/react-native/archive/sdk-41.0.0.tar.gz => 0.63.2
    react-native-appearance: ~0.3.3 => 0.3.4
    react-native-bouncy-checkbox: ^2.0.0 => 2.0.0
    react-native-codegen:  0.0.2
    react-native-elements: ^3.1.0 => 3.3.2
    react-native-gesture-handler: ~1.10.2 => 1.10.3
    react-native-maps: 0.27.1 => 0.27.1
    react-native-reanimated: ~2.1.0 => 2.1.0
    react-native-safe-area-context: 3.2.0 => 3.2.0
    react-native-screens: ~3.0.0 => 3.0.0
    react-native-skeleton-placeholder: ^3.0.4 => 3.0.4
    react-native-web: ^0.15.7 => 0.15.7
    react-test-renderer: ^17.0.1 => 17.0.2 (16.11.0)
    typescript: ~4.0.0 => 4.0.7
    yup: ^0.32.8 => 0.32.9
  npmGlobalPackages:
    @aws-amplify/cli: 4.41.2
    appium: 1.20.2
    aws-cdk: 1.91.0
    expo-cli: 4.4.1
    yarn: 1.22.10

Describe the bug

Auth.signIn throws an exception:

(0, _getRandomBase.default) is not a function. (In '(0, _getRandomBase.default)(array.byteLength)', '(0, _getRandomBase.default)' is undefined)

Expected behavior

A successful sign in

Reproduction steps

Try await Auth.signIn(...), otherwise the source code of my project can be checked (specifically at line: https://github.com/gimmickless/iyiye-native-app/blob/121af2c64aaecac02a6e9cd759f28182908ee0ed/src/contexts/Auth.tsx#L224)

Code Snippet

// Put your code below this line.

Log output

(0, _getRandomBase.default) is not a function. (In '(0, _getRandomBase.default)(array.byteLength)', '(0, _getRandomBase.default)' is undefined)

Stack trace:
  node_modules\amazon-cognito-identity-js\src\utils\getRandomValues.native.js:56:2 in getRandomValues
  node_modules\amazon-cognito-identity-js\src\utils\cryptoSecureRandomInt.native.js:8:8 in cryptoSecureRandomInt
  node_modules\amazon-cognito-identity-js\src\utils\WordArray.js:41:14 in random
  node_modules\amazon-cognito-identity-js\src\AuthenticationHelper.js:32:20 in randomBytes
  node_modules\amazon-cognito-identity-js\src\AuthenticationHelper.js:117:31 in generateRandomSmallA
  node_modules\amazon-cognito-identity-js\src\AuthenticationHelper.js:76:21 in constructor
  node_modules\amazon-cognito-identity-js\src\CognitoUser.js:241:31 in authenticateUserDefaultAuth
  node_modules\amazon-cognito-identity-js\src\CognitoUser.js:216:10 in authenticateUser
  node_modules\@aws-amplify\auth\lib-esm\Auth.js:533:12 in Promise$argument_0
  node_modules\react-native\node_modules\promise\setimmediate\core.js:45:6 in tryCallTwo
  node_modules\react-native\node_modules\promise\setimmediate\core.js:200:22 in doResolve
  node_modules\react-native\node_modules\promise\setimmediate\core.js:66:11 in Promise
  node_modules\@aws-amplify\auth\lib-esm\Auth.js:532:29 in AuthClass.prototype.signInWithPassword
  node_modules\@aws-amplify\auth\lib-esm\Auth.js:415:19 in AuthClass.prototype.signIn
  src\contexts\Auth.tsx:224:45 in login


aws-exports.js

No response

Manual configuration

export const AwsConfig = {
  // S3
  Storage: {
    AWSS3: {
      bucket: process.env.REACT_APP_S3_USER_STORAGE_BUCKET,
      region: process.env.REACT_APP_AWS_REGION
    }
  },
  // Cognito
  Auth: {
    mandatorySignIn: false,
    region: process.env.REACT_APP_AWS_REGION,
    userPoolId: process.env.REACT_APP_COGNITO_USER_POOL_ID,
    identityPoolId: process.env.REACT_APP_COGNITO_IDENTITY_POOL_ID,
    userPoolWebClientId: process.env.REACT_APP_COGNITO_USER_POOL_CLIENT_ID
  },
  // AppSync
  aws_appsync_graphqlEndpoint: process.env.REACT_APP_APPSYNC_GRAPHQL_ENDPOINT,
  aws_appsync_region: process.env.REACT_APP_AWS_REGION,
  aws_appsync_authenticationType: GRAPHQL_AUTH_MODE.AWS_IAM
}

Additional configuration

No response

Mobile Device

Samsung Galaxy A51

Mobile Operating System

Android11

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

About this issue

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

Most upvoted comments

I ran this same error on my Expo (React Native) project that I’ve been developing long time. I upgraded to latest Expo SDK 41, which was released last week, and my authentication broke down. I also know what problem is. It is Expo deprecating *.expo.js filetype handling, which amazon-cognito-identity-js is using. This is stated in Expo update blog post: https://blog.expo.io/expo-sdk-41-12cc5232f2ef#6bab And here is more in-depth details about this change: https://github.com/expo/fyi/blob/master/expo-extension-migration.md

How to reproduce this problem:

  1. Start new Expo project (with SDK 41, which is latest and automatically installed)
  2. Add aws-amplify to it
  3. Configure manual Auth.signIn
  4. signIn call throws exception as described in issue

I hope this information helps to get amazon-cognito-identity-js updated.

@vahdet There is workaround for time being for Expo SDK 41 users, while we wait library providers like AWS Amplify to make changes in their libraries. When starting expo-cli, pass EXPO_LEGACY_IMPORTS=1 as environment variable to it. For example: EXPO_LEGACY_IMPORTS=1 expo-cli start

That enabled my project to continue function for time being.

hello! i’m from the expo team. sorry we missed this library, we tried to track down usages of the .expo.js extension and updated them wherever we found them, but i didn’t think to check amplify-js and send a pull request to fix it.

thank you @svidgen for resolving this in https://github.com/aws-amplify/amplify-js/commit/2d4052da555709fe0f759fecb2df4b4b9604461c

i can confirm that if you set the EXPO_LEGACY_IMPORTS=1 environment variable this will resolve the issue temporarily, but keep in mind that you need to also set that value when you publish your app. it’s best to update to the latest version that includes the fix.

tl;dr PowerShell not handling EXPO_LEGACY_IMPORTS=1 expo start may not imply it is a wrong command

I think if you use PowerShell, you set environment variable like this:

PS> $env:EXPO_LEGACY_IMPORTS=1; expo start

Syntax I gave earlier is for different unix variants, so it works in Git Bash as you used, but also in WSL distros and such. I’ve actually dockerized expo-cli, and use that to keep whole development environment similar on all computers I work on.

And yea, this is just workaround for time being, actual code needs modifications. This env variable might be removed in future Expo releases, and as it is Expo specific code in this repo, it just needs to be updated.

@vahdet that EXPO_LEGACY_IMPORTS=1 was presented when I ran expo upgrade from SDK 40 project to SDK 41 project. This is what it output at that time:

✔ Installed node_modules and rebuilt package-lock.json.
✔ Cleared packager cache.
✖ Found project files with deprecated features
Project is using deprecated .expo.* file extensions.
Please refactor the following files and upgrade modules accordingly:

- /app/node_modules/amazon-cognito-identity-js/es/utils/getRandomBase64.expo.js
- /app/node_modules/amazon-cognito-identity-js/lib/utils/getRandomBase64.expo.js
- /app/node_modules/amazon-cognito-identity-js/src/utils/getRandomBase64.expo.js

Dangerously disable this check with EXPO_LEGACY_IMPORTS=1
Learn more: http://expo.fyi/expo-extension-migration


👏 Automated upgrade steps complete.
...but this doesn't mean everything is done yet!

Expo has quite many environment variables it takes on startup. But pretty much none is documented properly anywhere. You get to those usually just by reading either Expo’s source code, or asking help in issues with really specific use case, then you’re told about such environment variable that does magic for you. 😁

Sounds good. Thank-you!

The changes from #8162 were published late last week and are expected to resolve this.

@vahdet, can you confirm whether this resolves your issue?

@EvanErickson, have you tried clobbering your node_modules? Are you also on the latest version of aws-amplify?

@EvanErickson I might not be best to help, but you could double check your installed version with this: npm ls --depth=2 | grep "amazon-cognito" It should print out: │ ├─┬ amazon-cognito-identity-js@4.6.1 If that version is less than 4.6.1, then you still have old version installed, and needs to be updated. You might need to add more information about your project, deps, etc and maybe even try out to make minimal working example which you can share, where problem occurs.

When I run expo upgrade I see this:

✖ Found project files with deprecated features Project is using deprecated .expo.* file extensions. Please refactor the following files and upgrade modules accordingly:

  • /Users/headj/Sites/app2/node_modules/amazon-cognito-identity-js/es/utils/getRandomBase64.expo.js
  • /Users/headj/Sites/app2/node_modules/amazon-cognito-identity-js/lib/utils/getRandomBase64.expo.js
  • /Users/headj/Sites/app2/node_modules/amazon-cognito-identity-js/src/utils/getRandomBase64.expo.js

When I run EXPO_LEGACY_IMPORTS=1 expo start it works, but with this warning:

Dangerously enabled the deprecated .expo extensions feature, this functionality may be removed between SDK cycles.