'Signing into Cognito app through Cypress programmatically , giving userpoolId and clientId is loading the app with out user data

I am trying to automate Sign In flow for an app with Cognito Identity Provider in Cypress. This is the code I am using cypress.json :

"username": :<userName>,
"password":<password>,
"userPoolId":<userPoolId>,
"clientId":<clientId>

commands.js

const Auth = require ( "aws-amplify" ).Auth;
import "cypress-localstorage-commands";
const username = Cypress.env("username");
const password = Cypress. env("password");
const userPoolId = Cypress. env("userPoolId");
const clientId = Cypress. env ("clientId");
const awsconfig = {
    aws_user_pools_web_client_id: clientId,
    aws_user_pools_id: userPoolId
};
Auth. configure (awsconfig) ;


Cypress.Commands.add("signIn", () => {
    cy.then(() => Auth.signIn(username, password)).then((cognitoUser) => {
        console.log("SIGNIN AUTH",cognitoUser)
        const idToken = cognitoUser.signInUserSession.idToken.jwtToken;
        console.log("IDTOKEN", idToken)
        const accessToken = cognitoUser.signInUserSession.accessToken.jwtToken;

        const makeKey = (name) => `CognitoIdentityServiceProvider.${cognitoUser.pool.clientId}.${cognitoUser.username}.${name}`;

        cy.setLocalStorage(makeKey("accessToken"), accessToken);
        cy.setLocalStorage(makeKey("idToken"), idToken);
        cy.setLocalStorage(
            `CognitoIdentityServiceProvider.${cognitoUser.pool.clientId}.LastAuthUser`,
            cognitoUser.username
        );
        cy.setLocalStorage("amplify-signin-with-hostedUI","true")

           cy.visit("<loginUrl>");


         });
        cy.saveLocalStorage();
    })


TestFile.js :
cy.signIn()

App is loading but user data is not loaded. It launches the app with message - "Contact your administrator to start using the app", which is the message shown when the user is not added . But the user is added to the app. Could some one help me understand If I am missing something.User access token , idToken , refreshToken is stored correctly in the local Storage.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source