in packages/amplify-category-auth/src/provider-utils/awscloudformation/service-walkthroughs/auth-questions.js [13:212]
async function serviceWalkthrough(context, defaultValuesFilename, stringMapsFilename, serviceMetadata, coreAnswers = {}) {
const { inputs } = serviceMetadata;
const { amplify } = context;
const { parseInputs } = require(`${__dirname}/../question-factories/core-questions.js`);
const projectType = amplify.getProjectConfig().frontend;
const defaultValuesSrc = `${__dirname}/../assets/${defaultValuesFilename}`;
const { getAllDefaults } = require(defaultValuesSrc);
let userPoolGroupList = context.amplify.getUserPoolGroupList(context);
let adminQueryGroup;
handleUpdates(context, coreAnswers);
// QUESTION LOOP
let j = 0;
while (j < inputs.length) {
const questionObj = inputs[j];
// CREATE QUESTION OBJECT
const q = await parseInputs(questionObj, amplify, defaultValuesFilename, stringMapsFilename, coreAnswers, context);
// ASK QUESTION
const answer = await inquirer.prompt(q);
if ('signinwithapplePrivateKeyUserPool' in answer) {
answer.signinwithapplePrivateKeyUserPool = extractApplePrivateKey(answer.signinwithapplePrivateKeyUserPool);
}
if (answer.userPoolGroups === true) {
userPoolGroupList = await updateUserPoolGroups(context);
}
if (answer.adminQueries === true) {
adminQueryGroup = await updateAdminQuery(context, userPoolGroupList);
}
if (answer.triggers && answer.triggers !== '{}') {
const tempTriggers = context.updatingAuth && context.updatingAuth.triggers ? JSON.parse(context.updatingAuth.triggers) : {};
const selectionMetadata = capabilities;
/* eslint-disable no-loop-func */
selectionMetadata.forEach(s => {
Object.keys(s.triggers).forEach(t => {
if (!tempTriggers[t] && answer.triggers.includes(s.value)) {
tempTriggers[t] = s.triggers[t];
} else if (tempTriggers[t] && answer.triggers.includes(s.value)) {
tempTriggers[t] = uniq(tempTriggers[t].concat(s.triggers[t]));
} else if (tempTriggers[t] && !answer.triggers.includes(s.value)) {
const tempForDiff = Object.assign([], tempTriggers[t]);
const remainder = pullAll(tempForDiff, s.triggers[t]);
if (remainder && remainder.length > 0) {
tempTriggers[t] = remainder;
} else {
delete tempTriggers[t];
}
}
});
});
answer.triggers = tempTriggers;
}
// LEARN MORE BLOCK
if (new RegExp(/learn/i).test(answer[questionObj.key]) && questionObj.learnMore) {
const helpText = `\n${questionObj.learnMore.replace(new RegExp('[\\n]', 'g'), '\n\n')}\n\n`;
questionObj.prefix = chalk.green(helpText);
// ITERATOR BLOCK
} else if (
/*
if the input has an 'iterator' value, we generate a loop which uses the iterator value as a
key to find the array of values it should splice into.
*/
questionObj.iterator &&
answer[questionObj.key] &&
answer[questionObj.key].length > 0
) {
const replacementArray = context.updatingAuth[questionObj.iterator];
for (let t = 0; t < answer[questionObj.key].length; t += 1) {
questionObj.validation = questionObj.iteratorValidation;
const newValue = await inquirer.prompt({
name: 'updated',
message: `Update ${answer[questionObj.key][t]}`,
validate: amplify.inputValidation(questionObj),
});
replacementArray.splice(replacementArray.indexOf(answer[questionObj.key][t]), 1, newValue.updated);
}
j += 1;
// ADD-ANOTHER BLOCK
} else if (questionObj.addAnotherLoop && Object.keys(answer).length > 0) {
/*
if the input has an 'addAnotherLoop' value, we first make sure that the answer
will be recorded as an array index, and if it is already an array we push the new value.
We then ask the user if they want to add another url. If not, we increment our counter (j)
so that the next question is appears in the prompt. If the counter isn't incremented,
the same question is reapated.
*/
if (!coreAnswers[questionObj.key]) {
answer[questionObj.key] = [answer[questionObj.key]];
coreAnswers = { ...coreAnswers, ...answer };
} else {
coreAnswers[questionObj.key].push(answer[questionObj.key]);
}
const addAnother = await inquirer.prompt({
name: 'repeater',
type: 'confirm',
default: false,
message: `Do you want to add another ${questionObj.addAnotherLoop}`,
});
if (!addAnother.repeater) {
j += 1;
}
} else if (questionObj.key === 'updateFlow') {
/*
if the user selects a default or fully manual config option during an update,
we set the useDefault value so that the appropriate questions are displayed
*/
if (answer.updateFlow === 'updateUserPoolGroups') {
userPoolGroupList = await updateUserPoolGroups(context);
} else if (answer.updateFlow === 'updateAdminQueries') {
adminQueryGroup = await updateAdminQuery(context, userPoolGroupList);
} else if (['manual', 'defaultSocial', 'default'].includes(answer.updateFlow)) {
answer.useDefault = answer.updateFlow;
if (answer.useDefault === 'defaultSocial') {
coreAnswers.hostedUI = true;
}
if (answer.useDefault === 'default') {
coreAnswers.hostedUI = false;
}
delete answer.updateFlow;
}
coreAnswers = { ...coreAnswers, ...answer };
j += 1;
} else if (!context.updatingAuth && answer.useDefault && ['default', 'defaultSocial'].includes(answer.useDefault)) {
// if the user selects defaultSocial, we set hostedUI to true to avoid reasking this question
coreAnswers = { ...coreAnswers, ...answer };
coreAnswers.authSelections = 'identityPoolAndUserPool';
if (coreAnswers.useDefault === 'defaultSocial') {
coreAnswers.hostedUI = true;
}
j += 1;
} else {
coreAnswers = { ...coreAnswers, ...answer };
j += 1;
}
}
// POST-QUESTION LOOP PARSING
// if user selects user pool only, ensure that we clean id pool options
if (coreAnswers.authSelections === 'userPoolOnly' && context.updatingAuth) {
context.print.warning(
`Warning! Your existing IdentityPool: ${context.updatingAuth.identityPoolName} will be deleted upon the next “amplify push”!`,
);
delete context.updatingAuth.identityPoolName;
delete context.updatingAuth.allowUnauthenticatedIdentities;
delete context.updatingAuth.thirdPartyAuth;
delete context.updatingAuth.authProviders;
delete context.updatingAuth.facebookAppId;
delete context.updatingAuth.googleClientId;
delete context.updatingAuth.googleIos;
delete context.updatingAuth.googleAndroid;
delete context.updatingAuth.amazonAppId;
delete context.updatingAuth.appleAppId;
}
// formatting data for identity pool providers
if (coreAnswers.thirdPartyAuth) {
identityPoolProviders(coreAnswers, projectType);
}
const isPullOrEnvCommand = context.input.command === 'pull' || context.input.command === 'env';
if (coreAnswers.authSelections !== 'identityPoolOnly' && context.input.command != 'init' && !isPullOrEnvCommand) {
if (coreAnswers.useDefault === 'manual') {
coreAnswers.triggers = await lambdaFlow(context, coreAnswers.triggers);
}
}
// formatting data for user pool providers / hosted UI
if (coreAnswers.authProvidersUserPool) {
/* eslint-disable */
coreAnswers = Object.assign(coreAnswers, userPoolProviders(coreAnswers.authProvidersUserPool, coreAnswers, context.updatingAuth));
/* eslint-enable */
}
// formatting oAuthMetaData
structureOAuthMetadata(coreAnswers, context, getAllDefaults, amplify);
if (coreAnswers.usernameAttributes && !Array.isArray(coreAnswers.usernameAttributes)) {
if (coreAnswers.usernameAttributes === 'username') {
delete coreAnswers.usernameAttributes;
} else {
coreAnswers.usernameAttributes = coreAnswers.usernameAttributes.split();
}
}
return {
...coreAnswers,
userPoolGroupList,
adminQueryGroup,
serviceName: 'Cognito',
};
}