lumber-forestadmin
Version:
Forest Admin for Lumber.
58 lines (51 loc) • 1.64 kB
JavaScript
const chalk = require('chalk');
const Authenticator = require('./services/authenticator');
const api = require('./services/api');
module.exports = async (command, logger, inquirer) => {
const auth = new Authenticator(logger, inquirer);
switch (command) {
case 'login': {
const { email } = await inquirer.prompt([{
type: 'input',
name: 'email',
message: 'What\'s your email address?',
validate: (input) => {
if (input) { return true; }
return 'Please enter your email address.';
},
}]);
const isGoogleAccount = await api.isGoogleAccount(email);
if (isGoogleAccount) {
await auth.loginWithGoogle(email);
} else {
const { password } = await inquirer.prompt([{
type: 'password',
name: 'password',
message: 'What\'s your password?',
validate: (input) => {
if (input) { return true; }
return 'Please enter your password.';
},
}]);
try {
await auth.login(email, password);
logger.success('Login successful.');
} catch (err) {
if (err.message === 'Unauthorized') {
logger.error('Incorrect email or password.');
} else {
logger.error(`An unexpected error occured. Please create a Github issue with following error: ${chalk.red(err)}`);
}
process.exit(1);
}
}
break;
}
case 'logout':
await auth.logout();
break;
default:
logger.error(`Unknown command: ${command}`);
process.exit(1);
}
};