Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global.util.crypto.lib.randomBytes is not a function error

I am new to react. I am getting the following error while error while using amazon-cognito-identity-js npm package which states as follows

_global.util.crypto.lib.randomBytes is not a function

My Actions file in react is as follows

    import axios from 'axios';
    import * as actionTypes from './actionTypes';
    import authenticateUser from '../services/cognitoAuthenticateUser';
    import {reset} from 'redux-form';


    const signInUserSuccess = ({...result,email, newPasswordRequired = false}) => {
      return {
        type: actionTypes.SIGNIN_SUCCESS,
        ...result,
        email
      }
    };

    const signInUserFailure = (error) => {
      return {
        type: actionTypes.SIGNIN_FAILURE,
        error
      }
    };


    /* Login a given user based on his credentials added */
    export function LoginFeature(email,password,callback){

      return (dispatch) => {
        authenticateUser(email, password)
         .then(result => {
                          //console.log(result);
                          dispatch(signInUserSuccess({...result, email}));
                          callback();
                          dispatch(reset('loginForm'));
                          })
         .catch(e => {
           dispatch(signInUserFailure(e.message));
           dispatch(reset('loginForm'));
         })
      }
    }

My User Authenticate service file is as follows

import {
    AuthenticationDetails,
    CognitoUserPool,
    CognitoUser} from "amazon-cognito-identity-js";

  import {Config} from "aws-sdk";


  import awsConfig from "../../configs/aws-incognito-credentials";

  /*Credentails for AWS Incognito usage*/
  Config.region = awsConfig.region;

  const userPool = new CognitoUserPool({
    UserPoolId: awsConfig.UserPoolId,
    ClientId: awsConfig.ClientId
  });

  // Use case 4. Authenticating a user and establishing a user session with the Amazon Cognito Identity service.
  export const authenticateTheGivenUser = (username, password) => {
    return new Promise(function (resolve, reject) {

      const user = new CognitoUser({ Username: username, Pool: userPool })
      const authenticationData = { Username: username, Password: password }
      const authenticationDetails = new AuthenticationDetails(authenticationData)

      user.authenticateUser(authenticationDetails, {
        onSuccess: result => {
          console.log(result);
          resolve({...result, newPasswordRequired: false})
        },
        onFailure: err => reject(err),
        newPasswordRequired: (userAttributes, requiredAttributes) => resolve({newPasswordRequired: true})
      })
    })
  }

  export default authenticateTheGivenUser;

My package.json file is as follows

 "devDependencies": {
  "babel-core": "^6.2.1",
  "babel-loader": "^6.2.0",
  "babel-preset-es2015": "^6.1.18",
  "babel-preset-react": "^6.1.18",
  "chai": "^3.5.0",
  "chai-jquery": "^2.0.0",
  "jquery": "^2.2.1",
  "jsdom": "^8.1.0",
  "mocha": "^2.4.5",
  "react-addons-test-utils": "^0.14.7",
  "webpack": "^1.12.9",
  "webpack-dev-server": "^1.14.0",
  "babel-preset-stage-1": "^6.24.1",
  "json-loader": "^0.5.7"
},
"dependencies": {
  "amazon-cognito-identity-js": "^1.5.0",
  "axios": "^0.17.1",
  "babel-preset-stage-1": "^6.1.18",
  "material-ui": "^0.20.0",
  "material-ui-datatables": "^0.18.2",
  "material-ui-icons": "^1.0.0-beta.17",
  "mui-data-table": "^0.1.5",
  "prop-types": "^15.6.0",
  "random-bytes": "^1.0.0",
  "react": "^0.14.3",
  "react-dom": "^0.14.3",
  "react-materialize": "^1.1.2",
  "react-redux": "4.3.0",
  "react-router": "^2.0.1",
  "react-router-dom": "^4.0.0",
  "react-toastify": "^3.2.1",
  "redux": "^3.0.4",
  "redux-form": "^7.2.0",
  "redux-thunk": "^2.2.0"
}

I am unable to understand why I am getting this error? Can somebody point as to what am I doing wrong?

like image 268
shubhamagiwal92 Avatar asked Jan 09 '18 03:01

shubhamagiwal92


1 Answers

Downgrading aws-sdk gets rid of the problem:

yarn add [email protected]

For me aws-sdk is a dependency of aws-amplify and got automatically updated, after that I started seeing the same error.

like image 183
Martin Schulze Avatar answered Oct 18 '22 22:10

Martin Schulze