question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cognitoUser.authenticateUser() -> Unknown error, the response body from fetch is: undefined

See original GitHub issue

Do you want to request a feature or report a bug? BUG

What is the current behavior? When trying to loggin with the amazon-cognito-identity.js API, using the autenticateUser() function form the CognitoUser class, I receive the following error

`Unknown error, the response body from fetch is: undefined`

When setting a breakpoint in the onFailure event, and checking the trace, you can see 2 calls done to the cognito-idp endpoint. The response is empty in this moment, but after releasing the breakpoint and checking the calls again you can see the response body, with the tokens and all the information I need. But due to this error, the onSuccess event doesn’t execute.

Here my code

userSignIn(email, password) {
    var username = email.split("@")[0]
    var userPool = this.getUserPool();
    var authenticationData = {
      Username: username,
      Password: password,
    };
    var authenticationDetails = new AuthenticationDetails(authenticationData);

    var userData = {
      Username: username,
      Pool: userPool
    };
    var cognitoUser = new CognitoUser(userData);

    cognitoUser.authenticateUser(authenticationDetails, {
      onSuccess: function (result) {
        var accessToken = result.getAccessToken().getJwtToken();

        //POTENTIAL: Region needs to be set if not already set previously elsewhere.
        AWS.config.region = this.getRegion();

        AWS.config.credentials = new AWS.CognitoIdentityCredentials({
          IdentityPoolId: this.getIdentityPoolId(), // your identity pool id here
          Logins: {
            // Change the key below according to the specific region your user pool is in.
            [this.getCognitoIdpEndpoint()]: result.getIdToken().getJwtToken()
          }
        });

        console.log(AWS.config.credentials);
        debugger;

        // //refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity()
        // AWS.config.credentials.refresh((error) => {
        //   if (error) {
        //     console.error(error);
        //   } else {
        //     // Instantiate aws sdk service objects now that the credentials have been updated.
        //     // example: var s3 = new AWS.S3();
        //     console.log('Successfully logged!');
        //   }
        // });
      },

      onFailure: function (err) {
        debugger
        alert(err.message || JSON.stringify(err));
      }

    });
  }

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than AWS Amplify.

What is the expected behavior? request should be successful, returning the tokens and firing the “onSuccess” event

Which versions of Amplify, and which browser / OS are affected by this issue? Did this work in previous versions? Using: “amazon-cognito-identity-js”: “^2.0.23” Browser: Chrome 68.0.3440.106 OS: Same error on Win10 and mac OSX

You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = 'DEBUG'; in your app.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
rlopmarcommented, Sep 2, 2018

All right I found the problem. I didn’t pass the ‘this’ context to the ‘onSuccess’ event, so it failed when doing this.getRegion().

However, instead of trowin the “getRegion is not defined” error, it falls back to a previous catch() in Client.js file line 75, which forces the error to be “Unknown error, the response body from fetch is: undefined”.

Please, you need to improve this error handling, as it makes debugging a imposible task. At least print the original error in the console before forcing a new error message. Or even better, check only for a set of errors you know and if it’s none of them, just console.error() the error and stop the execution.

https://github.com/aws-amplify/amplify-js/blob/05cbe5913d3f8583ac5dfeab514f0f1af2e3c012/packages/amazon-cognito-identity-js/es/Client.js#L74

3reactions
ToddHoffcommented, Dec 24, 2018

All right I found the problem. I didn’t pass the ‘this’ context to the ‘onSuccess’ event, so it failed when doing this.getRegion().

However, instead of trowin the “getRegion is not defined” error, it falls back to a previous catch() in Client.js file line 75, which forces the error to be “Unknown error, the response body from fetch is: undefined”.

Please, you need to improve this error handling, as it makes debugging a imposible task. At least print the original error in the console before forcing a new error message. Or even better, check only for a set of errors you know and if it’s none of them, just console.error() the error and stop the execution.

amplify-js/packages/amazon-cognito-identity-js/es/Client.js

Line 74 in 05cbe59

var error = { code: ‘UnknownError’, message: ‘Unknown error’ };

Could you please be more specific about how you fixed the error? How would you pass ‘this’ into the callback?

thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unknown error, the response body from fetch is: undefined
Now I am getting the error "Unknown error, the response body from fetch is: undefined" when I call authenticateUser method.
Read more >
Unknown error, the response body from fetch is undefined
The problem is that i get an error: Unknown error, the response body from fetch is undefined. I already have node-fetch on the...
Read more >
amazon-cognito-identity-js - npm
authenticateUser throws ReferenceError: navigator is not defined when running on Node.js, follow the instructions on the following Stack ...
Read more >
pass value in session next auth get from api - You.com
In your case, you called res.redirect() , which caused the response to become Finished. Then your code threw an error ( res.req is...
Read more >
AWS Cognitoを用いたログイン認証でUnknown Errorが出る - Teratail
'use strict'; const AWS = require("aws-sdk"); global.fetch ... { "code": "UnknownError", "message": "Unknown error, the response body from fetch is: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found