Describe the bug
I am using cognito with serverless-http app.
I encountered some error about Reference error in Cloudwatch when I request my serverless app.
ReferenceError: fetch is not defined
at Client.request (/var/task/node_modules/amazon-cognito-identity-js/lib/Client.js:55:5)
at /var/task/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:313:21
at AuthenticationHelper.getLargeAValue (/var/task/node_modules/amazon-cognito-identity-js/lib/AuthenticationHelper.js:85:7)
at CognitoUser.authenticateUserDefaultAuth (/var/task/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:291:26)
at CognitoUser.authenticateUser (/var/task/node_modules/amazon-cognito-identity-js/lib/CognitoUser.js:252:19)
at login (/var/task/src/serverless.js:277:17)
at Layer.handle [as handle_request] (/var/task/node_modules/express/lib/router/layer.js:95:5)
at next (/var/task/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/task/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/task/node_modules/express/lib/router/layer.js:95:5)
To Reproduce
Steps to reproduce the behavior:
Expected behavior
So I tried to fix this error to edit amazon_cognito_identity-js in node_modules.
But it can not be applied, so I carefully change above these commit.
https://github.com/Q00/amazon-cognito-identity-js/commit/6a907ddcbee687495bf9c3a2286480606a454b05
After change, finally I get intended response like this
message: 'User does not exist.',
code: 'UserNotFoundException',
time: 2019-01-20T19:31:41.735Z,
requestId: '0364989e-1cea-11e9-a193-affb587e0cbd',
statusCode: 400,
retryable: false,
retryDelay: 28.517921067981966
I am junior with cognito so that I do not understand your intention perfectly.
But these commit can help me deploy and service app without error.
https://github.com/Q00/amazon-cognito-identity-js/commit/6a907ddcbee687495bf9c3a2286480606a454b05
Screenshots

Desktop (please complete the following information):
Additional context
I want to help people who get related this error.
What can I do for this error?
You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = 'DEBUG'; in your app.
I just tried this on node.js
global.fetch = require('node-fetch');
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
var authenticationData = {
Username: 'xxxxxxxx',
Password: 'xxxxxxxx*',
};
var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
var poolData = {
UserPoolId: 'xx-xxxx-x_xxxxxxxxx', // Your user pool id here
ClientId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx' // Your client id here
};
var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
var userData = {
Username: 'xxxxxxx',
Pool: userPool
};
var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
var accessToken = result.getAccessToken().getJwtToken();
console.log({ accessToken });
},
onFailure: function (err) {
alert(err.message || JSON.stringify(err));
},
});
I installed node-fetch on my node.js app
@Q00 closing the issue for now. Feel free to reopen in case you need more help
How to solve it in Typescript?
@chaitanya11 For Typescript you can do this:
(global as any).fetch = require('node-fetch');
Most helpful comment
I just tried this on node.js
I installed node-fetch on my node.js app