Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate Cognito user pool in node js

Using v1.10 of amazon-cognito-identity-js in the following nodeapp.

I have written the following: This call is made after the user registration has been confirmed and the email has been verified.

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"

It looks like a browser call is being attempted inside the node server.

I had created a github issue on this and the recommendation was to refer to "jsbn": "^0.1.0", "sjcl": "^1.0.3", "amazon-cognito-identity-js": "^1.10.0", "aws-sdk": "^2.5.3"

I made the changes to the package versions. Unfortunately it did not work with this too. I have created a sample nodejs app which tries to authenticate using email id and password.

NB : The Cognito pool is setup with email as an alias. The user has been created, confirmed and email verified.

Source repo : https://github.com/prem911/cognito-nodejs

Any pointers on how to solve the "navigator not found"?

like image 704
prem911 Avatar asked Jan 03 '17 11:01

prem911


People also ask

How do I use AWS Cognito in node JS?

First login to your aws account and go to cognito section. If you don't have an aws account yet, create a free account which will be free for an year. Then go to Cognito section and then create a user pool. Give a name for the pool.

How do you authenticate with Cognito?

Go to AWS Cognito service and click “Manage Identity Pools”. 2. Enter “Identity pool name”, expand the “Authentication providers” section and select “Cognito” tab. This is where the Cognito authentication provider will be registered with the Identity pool.

What is the main difference between Cognito user pool and Cognito identity pool?

With a user pool, your app users can sign in through the user pool or federate through a third-party identity provider (IdP). Identity pools are for authorization (access control). You can use identity pools to create unique identities for users and give them access to other AWS services.


1 Answers

To save some googling time there is no fix for the above error at the time of writing this but there is a workaround:

AWS Cognito unauthenticated login error (window is not defined) [JS]

like image 164
Tomas Dermisek Avatar answered Sep 21 '22 16:09

Tomas Dermisek