Repository URL to install this package:
|
Version:
0.0.0-eb7f46753a3bdc ▾
|
const revhash = require('rev-hash');
const inquirer = require('inquirer');
const ora = require('ora');
const chalk = require('chalk');
const { getUniqueEmail } = require('../utils/email');
const { obfuscateString } = require('../utils/strings');
const {
DOODLE_ENVS,
CONFIG_EMAIL_KEY,
CONFIG_PASSWORD_KEY,
verboseLogging,
CONFIG_FIRST_NAME_KEY,
} = require('../constants');
const startTrialPrompt = require('./start-trial-prompt');
const {
getServiceAccount,
createNewKeycloakUser,
setPasswordForKeycloakUser,
getKeycloakUserInfo,
} = require('../apis/keycloak');
const hash = revhash(new Date().toUTCString());
/**
* Create a new Keycloak user.
*
* @param {Object} options
* @param {String} options.email
* @param {String} options.password
* @param {String} options.target
* @param {Boolean} options.premium
* @param {Object} config
*/
async function createNewUser(options = {}, config) {
await inquirer
.prompt([
{
type: 'list',
name: 'target',
message: 'Which environment should this user be created?',
choices: Object.keys(DOODLE_ENVS),
default: 'staging',
when: !options.target,
},
{
type: 'input',
name: 'firstName',
message: `What is the user's first name?`,
default: config[CONFIG_FIRST_NAME_KEY],
when: !options.firstName,
},
{
type: 'input',
name: 'lastName',
message: `What is the user's last name? (leave blank to use '${hash}')`,
default: hash,
when: !options.lastName,
},
{
type: 'email',
name: 'email',
message: `Enter an email address to use (leave blank to auto-generate with hash)`,
when: !options.email,
},
{
type: 'password',
name: 'password',
message: `Enter a password to use (leave blank for default: ${obfuscateString(config[CONFIG_PASSWORD_KEY])})`,
default: config[CONFIG_PASSWORD_KEY],
when: !options.password,
},
{
type: 'confirm',
name: 'premium',
message: 'Do you want to sign this user up for premium?',
default: false,
when: !options.premium,
},
])
.then(async answers => {
const { target, email, password, premium, firstName, lastName } = {
...options,
...answers,
};
// Do we need to auto-generate an email?
let username = email;
if (!username) {
username = getUniqueEmail(hash, config[CONFIG_EMAIL_KEY]);
}
if (verboseLogging) {
console.log({ target, email, password, premium });
}
const spinner = ora(`Creating user: ${chalk.yellow(username)}`).start();
let serviceAccountToken;
try {
// Get the service account token to manage users
const serviceAccount = await getServiceAccount(target);
serviceAccountToken = serviceAccount.access_token;
if (verboseLogging) {
console.log({ serviceAccountToken });
}
// Create the new user
await createNewKeycloakUser(target, username, firstName, lastName, serviceAccountToken);
spinner.succeed(`User account created: ${chalk.yellow(username)}`);
// Get the new user's userId
const userData = await getKeycloakUserInfo(target, username, serviceAccountToken);
if (!userData || userData.length === 0) {
throw new Error('Could not create Keycloak user');
}
// Set the user's password
spinner.start('Setting new user password');
await setPasswordForKeycloakUser(target, userData[0].id, password, serviceAccountToken);
spinner.succeed(`Password set: ${chalk.yellow(password.substr(0, 2))}**********`);
if (premium) {
return startTrialPrompt({ ...options, email: username, password, target }, config);
}
} catch (err) {
spinner.fail(err.message || 'Could not create user');
console.error(err);
process.exit(1);
}
});
}
module.exports = createNewUser;