My NextJS server executes a function called “loadAllUserPools” during start up. Here’s what the [abbreviated] file with this function looks like:
let userPoolMap = new Map();
export async function loadAllUserPools() {
console.log('loadAllUserPools function started');
// Triggered when the app starts. It gets all user pools
// from the DB.
// TODO: get data from db. Mocking DB data:
const dbUserPoolData = [
{
cognitoUserPoolId: 'user-pool-1111111',
cognitoClientId: '11111111111',
accessUrl: 'tenant1',
},
{
cognitoUserPoolId: 'user-pool-2222222',
cognitoClientId: '222222222222',
accessUrl: 'tenant2',
},
];
cognitoUserPoolList = [];
// Update the cognitoVerifier list and the subdomain map
dbUserPoolData.map((item) => {
cognitoUserPoolList.push({
userPoolId: item.cognitoUserPoolId,
clientId: item.cognitoClientId,
tokenUse: 'access',
});
userPoolMap.set(item.accessUrl, {
userPoolId: item.cognitoUserPoolId,
clientId: item.cognitoClientId,
});
});
console.log('map: ', userPoolMap);
}
export async function getCognitoUserPoolBySubdomain(subdomain: string) {
console.log('map: ', userPoolMap);
const data = userPoolMap.get(subdomain);
console.log(data);
return data;
}
When the “loadAllUserPools” function executes, it prints the Map to the console with the expected values, which tells me that the map values are being properly set:
loadAllUserPools function started
map: Map(2) {
'tenant1' => { userPoolId: 'user-pool-1111111', clientId: '11111111111' },
'tenant2' => { userPoolId: 'user-pool-2222222', clientId: '222222222222' }
}
Later when a user tries to login, I invoke the “getCognitoUserPoolBySubdomain” function, which tries to get a value from the Map. However, the map returns “undefined” when trying to find “tenant1”. Furthermore, printing the Map to the console shows a Map length of zero:
getCognitoUserPoolBySubdomain function started
map: Map(0) {}
undefined
Any ideas on why this is happening?