1

My current log-in page is built using Astro.js and AWS Cognito. I have one user set-up on Cognito and that user could log-in to the app using the credentials I specify on Cognito and in the codes for the utils file for the log-in page, as shown below:

import { CognitoUserPool, CognitoUser, AuthenticationDetails } from 'amazon-cognito-identity-js';

// Configure Cognito
const poolData = {
    UserPoolId: 'xyz',
    ClientId: 'xyz',
};

const userPool = new CognitoUserPool(poolData);
var sessionUserAttributes;
// Function to sign in
export const signIn = (username, password) => {
    const authenticationData = {
        Username: username,
        Password: password,
    };

    const authenticationDetails = new AuthenticationDetails(authenticationData);

    const userData = {
        Username: username,
        Pool: userPool,
    };

    const cognitoUser = new CognitoUser(userData);

    cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: (session) => {
            console.log('Authentication successful');
            // You can redirect or update your app's UI here
            window.location.reload(false);
        },
        onFailure: (err) => {
            console.error('Authentication failed', err);
        },
        newPasswordRequired: (userAttributes, requiredAttributes) => {
            // User was signed up by an admin and must provide new
            // password and required attributes, if any, to complete
            // authentication.

            // the api doesn't accept this field back
            delete userAttributes.email_verified;
            delete userAttributes.email; 

            userAttributes.given_name = "xyz"
            userAttributes.family_name = "xyz"
            userAttributes.phone_number = "xyz"

            console.log('userAttributes', userAttributes)
            console.log('requiredAttributes', requiredAttributes)

            // store userAttributes on global variable
            sessionUserAttributes = userAttributes;
            cognitoUser.completeNewPasswordChallenge("xyz", sessionUserAttributes);
            
        },
    });
};

The issue is when I add in multiple users on Cognito, the new users can't log-in to the app even though on Cognito, the new users are shown to have been verified. How can I rewrite my code above to add in an array of users (including Username & Password) so I could have multiple users logging in?

Thank you

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.