Mileta Dulovic
Mileta Dulovic

Reputation: 1064

AWS Cognito auth fails with Vanilla JS

I am trying to auth the user with AWS Cognito and get a token from AWS with Vanilla JS.

In their docs they have this example. The thing is that when I follow it I get error bellow

ErrorValidationException: 3 validation errors detected: Value 'value' at 'accountId' failed to satisfy constraint: Member must have length less than or equal to 15; Value 'value' at 'accountId' failed to satisfy constraint: Member must satisfy regular expression pattern: \d+; Value 'value' at 'identityPoolId' failed to satisfy constraint: Member must satisfy regular expression pattern: [\w-]+:[0-9a-f-]+

For these variables

var IDENTITY_POOL_ID = "value";
var ACCOUNT_ID = "value";
var REGION = "value";

I used data that AWS generated for me (I don't want to display them here).

I am just confused about why this doesn't work since this is a really simple example from their docs, and it's using data that has been generated by AWS

Upvotes: 0

Views: 1043

Answers (1)

Mileta Dulovic
Mileta Dulovic

Reputation: 1064

I finally made it

This is correct configuration for authenticating user

First you need to load these libraries

<script src="https://sdk.amazonaws.com/js/aws-sdk-2.3.5.js"></script>
<script src="https://rawgit.com/aws/amazon-cognito-identity-js/master/dist/aws-cognito-sdk.min.js"></script>
<script src="https://rawgit.com/aws/amazon-cognito-identity-js/master/dist/amazon-cognito-identity.min.js"></script>

Then just add this configuration with your data

var authenticationData = {
          Username: username,
          Password: password,
        };
        var authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(
          authenticationData
        );
        var poolData = {
          UserPoolId: "user_pool_id",
          ClientId: "client_id",
        };
        var userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
        var userData = {
          Username: username,
          Pool: userPool,
        };
        var cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
        cognitoUser.authenticateUser(authenticationDetails, {
          onSuccess: function (result) {
            var accessToken = result.getAccessToken().getJwtToken();

            /* Use the idToken for Logins Map when Federating User Pools with identity pools or when passing through an Authorization Header to an API Gateway Authorizer */
            var idToken = result.idToken.jwtToken;
          },

          onFailure: function (err) {
            alert(err);
          },
        });

Upvotes: 3

Related Questions