Amazon-cognito-identity-js: CognitoUser.authenticateUser throws ReferenceError: navigator is not defined

Created on 30 Dec 2016  路  15Comments  路  Source: amazon-archives/amazon-cognito-identity-js

Using v1.10 of this package.

I have written the following:
This call is made after the user registration has been confirmed.
var AWS = require('aws-sdk');
var AWSCognito = require('amazon-cognito-identity-js');

router.post('/emailsignin', function(req, res, next) {
var email = req.body.email;
var pwd = req.body.password;

AWS.config.region = 'eu-west-1';
var poolData = {
  UserPoolId : AWS_USERPOOLID,
  ClientId : AWS_APPCLIENTID
};
var userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var authenticationData = {
  Username : email,
  Password : pwd,
};
var authenticationDetails = new AWS.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var userData = {
  Username : email,
  Pool : userPool
};
var cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userData);

cognitoUser.authenticateUser(authenticationDetails, {
  onSuccess: function (result) {
  },
  onFailure: function(err) {
 }

});
When authenticateUser is called, I get the "ReferenceError: navigator is not defined"

I have not used the Webpack instructions. Is it mandatory? The example for Webpack usage are also not clear and a full example is not provided for nodejs. I have an existing node application in which I m making new changes for Cognito.

bug

Most helpful comment

The stack overflow solution worked for me too, add this to your app:

global.navigator = () => null;

All 15 comments

The following works for me (very similar to your example). This pretty much in a clean directory with a package.json file that brings in amazon-cognito-identity-js, jsbn, the aws sdk, and sjcl.

"jsbn": "^0.1.0",
"sjcl": "^1.0.3"
"amazon-cognito-identity-js": "^1.10.0",
"aws-sdk": "^2.5.3",

The error probably comes from a wrong jsbn version as in the question below:

http://stackoverflow.com/questions/16500499/nodejs-referenceerror-navigator-is-not-defined

var cognito = require('amazon-cognito-identity-js');

var poolData = { UserPoolId : 'YOUR_USER_POOL_ID',
    ClientId : 'YOUR_CLIENT_ID'
};

var userPool = new cognito.CognitoUserPool(poolData);

var authenticationData = {
   Username : 'YOUR_USERNAME',
   Password : 'YOUR_PASSWORD',
};

var authenticationDetails = new cognito.AuthenticationDetails(authenticationData);
var userData = {
    Username : 'YOUR_USERNAME',
    Pool : userPool
};

var cognitoUser = new cognito.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: function (result) {
        console.log('access token + ' + result.getAccessToken().getJwtToken());
    },

    onFailure: function(err) {
        console.log('error ' + err);
    },
});

@itrestian I made the changes to the package versions. Unfortunately it did not work.
I have created a sample nodejs app which tries to authenticate using email id and password.
The Cognito pool is setup with email as an alias. The user has been created, confirmed and email verified.

You can check the changes in this repo : https://github.com/prem911/cognito-nodejs

Hi,

I get the same error when trying to call authenticateUser. Same error is also thrown when I call completeNewPasswordChallenge function. The function seems to fire ok because Cognito user status changes to confirmed and new password and required attributes are set.

I use:
"aws-sdk": "^2.6.0"
"jsbn": "^0.1.0"
"sjcl": "^1.0.3"
"amazon-cognito-identity-js": "^1.10.0"

Hi, Also having the same issue when calling authenticateUser.

Using:

"jsbn": "^0.1.0",
"sjcl": "^1.0.3"
"amazon-cognito-identity-js": "^1.10.0",
"aws-sdk": "^2.5.3"

The example @prem911 provided works for me using node v6.6.0 and doing npm install to pickup the dependencies.

Tested by hitting emailsignin at port 3000. Is that how you are testing it?

What version of node is everyone using? Also is everyone using Express.js similar to the example provided by @prem911?

I am also having an issue with authenticateUser - this was working until the last day or so.

Full details posted, including live URL to failure, on https://github.com/aws/amazon-cognito-identity-js/issues/240

@itrestian I have the node app deployed as a beanstalk app.
Following versions have to be supported as per the documentation.
Node.js 6.9.1, Also supports 6.9.1, 6.2.2, 5.12.0, 4.6.1, 4.4.6, 0.12.17, 0.12.15, 0.10.48, 0.10.46

http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/concepts.platforms.html#concepts.platforms.nodejs

I have 4.4.6 and expected this to work. I tried it with 6.9.1 and found the same issue.

I tested your code with all the node versions below and it works fine for me from my dev box. I can hit the url and authenticate and get credentials.

6.9.1
6.2.2
5.12.0
4.6.1
4.4.6

The other ones don't work cause of different syntax reasons related to your code.

Does the code work for you outside of beanstalk? It might be some beanstalk specific setting that causes this and we need to investigate.

I dont have it working either in my local environment or beanstalk.

Can you try the suggestion in the stack overflow post below? We removed the window dependency but the suggestion regarding navigator.

http://stackoverflow.com/questions/40219518/aws-cognito-unauthenticated-login-error-window-is-not-defined-js

That worked for me, thanks!

The stack overflow solution worked for me too, add this to your app:

global.navigator = () => null;

Also worked for me
just added
global.navigator = () => null;
in app.js , before specifying the routes and am using node version v4.3.1

I observed this issue to occur while running in NodeJS only when you enable "remember your user's devices" in user pool. If i disable the same , the error does not occur.

Set No in all device management sections, if you are running innodejs . CogintoUser API Tries to get UserAgent from the Device and assumes navigator as the Device.

Was this page helpful?
0 / 5 - 0 ratings