Repository URL to install this package:
|
Version:
0.0.0-e6a1a867dd348d ▾
|
const inquirer = require('inquirer');
const { DOODLE_ENVS } = require('../constants');
const { CONFIG_PASSWORD_KEY, CONFIG_EMAIL_KEY, verboseLogging } = require('../constants');
const { getTokenForUser } = require('../apis/keycloak');
const { obfuscateString } = require('../utils/strings');
/**
* Log the user in and get a JWT Token from Keycloak
*
* @param {Object} options
* @param {Object} options.email
* @param {Object} options.password
* @param {Object} options.target
* @param {Object} options.print
* @param {Object} config
* @returns {Object}
*/
async function loginUser(options = {}, config) {
const obfuscatedPassword = obfuscateString(config[CONFIG_PASSWORD_KEY]);
await inquirer
.prompt([
{
type: 'list',
name: 'target',
message: 'Which environment are you logging into?',
choices: Object.keys(DOODLE_ENVS),
default: 'staging',
when: typeof options.target === 'undefined',
},
{
type: 'input',
name: 'email',
message: `Enter user's email`,
default: config[CONFIG_EMAIL_KEY],
when: typeof options.email === 'undefined',
},
{
type: 'password',
name: 'password',
message: `What is the user's account password? (leave blank for default: ${obfuscatedPassword})`,
default: config[CONFIG_PASSWORD_KEY],
when: typeof options.password === 'undefined',
},
])
.then(async answers => {
const { target, email, password } = {
...options,
...answers,
};
let data;
try {
data = await getTokenForUser(target, email, password);
} catch (err) {
console.error(err);
}
if (options.print || verboseLogging) {
console.log(JSON.stringify({ target, email, password, data }, null, 2));
}
return { target, email, password, data };
});
}
module.exports = loginUser;