Illep
Illep

Reputation: 16851

Amazon Cognito with Javascript AWSCognito not defined error

Following is my app.js file. On the browser when the user clicks the submit button the method app.post('/ttt', function (req,res) is invoked. I am trying to successfully register a user with Amazon Cognito. However, I get an error message that says AWSCognito is not undefined. How can I solve this issue ?

I am following this tutorial.

var express = require('express');

var AWSCognito = require('/Applications/XAMPP/xamppfiles/htdocs/my/p/js/aws-cognito-sdk.js');

var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());

var urlencodedParser = bodyParser.urlencoded({ extended: false });
var jsonParser = bodyParser.json();

var path = require('path');
app.use(bodyParser.json());

var http = require('http');
var fs = require('fs');
var ejs = require('ejs');
var formidable = require("formidable");
var util = require('util');
var AWS = require('aws-sdk');


app.post('/ttt', function (req,res){

  AWS.config.region = 'us-east-1'; // Region
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: '...' // your identity pool id here
    });

    AWSCognito.config.region = 'us-east-1';
    AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: '...' // your identity pool id here
    });

    // Need to provide placeholder keys unless unauthorised user access is enabled for user pool
    AWSCognito.config.update({accessKeyId: 'anything', secretAccessKey: ''})

    var poolData = { 
        UserPoolId : 'us-east-1_TcoKGbf7n',
        ClientId : '4pe2usejqcdmhi0a25jp4b5sh3'
    };
    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

    var attributeList = [];

    var dataEmail = {
        Name : 'email',
        Value : '[email protected]'
    };
    var dataPhoneNumber = {
        Name : 'phone_number',
        Value : '+15555555555'
    };
    var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
    var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);

    attributeList.push(attributeEmail);
    attributeList.push(attributePhoneNumber);

    userPool.signUp('username', 'password', attributeList, null, function(err, result){
        if (err) {
            alert(err);
            return;
        }
        cognitoUser = result.user;
        console.log('user name is ' + cognitoUser.getUsername());
    });

}

Upvotes: 0

Views: 2767

Answers (1)

patanjal
patanjal

Reputation: 665

AWS currently does not support 'require' to import the Cognito javascript SDK. Can you try again after exporting the SDK manually

Upvotes: 2

Related Questions