shu*_*l92 5 amazon-web-services node.js npm reactjs
我是新来的反应.使用amazon-cognito-identity-js npm包时出现错误时出现以下错误,如下所示
_global.util.crypto.lib.randomBytes is not a function
Run Code Online (Sandbox Code Playgroud)
我的Actions文件反应如下
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'));
})
}
}
Run Code Online (Sandbox Code Playgroud)
我的User Authenticate service文件如下
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;
Run Code Online (Sandbox Code Playgroud)
我的package.json文件如下
"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"
}
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么会收到此错误?有人可以指出我做错了什么吗?
降级 aws-sdk 可以解决该问题:
yarn add aws-sdk@2.177.0
Run Code Online (Sandbox Code Playgroud)
对我来说,aws-sdk 是 aws-amplify 的依赖项并自动更新,之后我开始看到相同的错误。
| 归档时间: |
|
| 查看次数: |
439 次 |
| 最近记录: |