@ionic/cli
Version:
A tool for creating and developing Ionic Framework mobile apps.
150 lines (144 loc) • 6.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cli_framework_1 = require("@ionic/cli-framework");
const chalk = require("chalk");
const readline = require("readline");
const color_1 = require("../lib/color");
const command_1 = require("../lib/command");
const errors_1 = require("../lib/errors");
const uuid_1 = require("../lib/utils/uuid");
class LoginCommand extends command_1.Command {
async getMetadata() {
return {
name: 'login',
type: 'global',
summary: 'Log in to Ionic',
description: `
Authenticate with Ionic and retrieve a user token, which is stored in the CLI config. The most secure way to log in is running ${color_1.input('ionic login')} without arguments, which will prompt you for your credentials.
If the ${color_1.input('IONIC_TOKEN')} environment variable is set, the CLI will automatically authenticate you. To retrieve your user token, first use ${color_1.input('ionic login')}, then print the token by running the ${color_1.input('ionic config get -g tokens.user')} command.
${color_1.input('ionic login')} will also accept ${color_1.input('password')} through stdin, e.g.: ${color_1.input('echo "<password>" | ionic login <email>')}.
If you need to create an Ionic account, use ${color_1.input('ionic signup')}.
You can reset your password in the Dashboard[^reset-password].
If you are having issues logging in, please get in touch with our Support[^support-request].
`,
footnotes: [
{
id: 'reset-password',
url: 'https://dashboard.ionicframework.com/reset-password',
shortUrl: 'https://ion.link/reset-password',
},
{
id: 'support-request',
url: 'https://ion.link/support-request',
},
],
exampleCommands: ['', 'john@example.com', 'hello@example.com secret'],
inputs: [
{
name: 'email',
summary: 'Your email address',
validators: [cli_framework_1.validators.required, cli_framework_1.validators.email],
private: true,
},
{
name: 'password',
summary: 'Your password',
// this is a hack since sso is hidden, no need to make password not required for it
validators: process.argv.includes('--sso') ? [] : [cli_framework_1.validators.required],
private: true,
},
],
options: [
{
name: 'sso',
type: Boolean,
summary: 'Open a window to log in with the SSO provider associated with your email',
groups: ["hidden" /* HIDDEN */],
},
],
};
}
async preRun(inputs, options) {
const sso = !!options['sso'];
if (options['email'] || options['password']) {
throw new errors_1.FatalException(`${color_1.input('email')} and ${color_1.input('password')} are command arguments, not options. Please try this:\n` +
`${color_1.input('ionic login <email> <password>')}\n`);
}
const askForEmail = !inputs[0];
const askForPassword = !sso && !inputs[1];
if (this.env.session.isLoggedIn()) {
const email = this.env.config.get('user.email');
const extra = askForEmail || askForPassword
? (this.env.flags.interactive ? `Prompting for new credentials.\n\nUse ${chalk.yellow('Ctrl+C')} to cancel and remain logged in.` : '')
: 'You will be logged out beforehand.';
if (this.env.flags.interactive) {
this.env.log.warn('You will be logged out.\n' +
`You are already logged in${email ? ' as ' + color_1.strong(email) : ''}! ${extra}`);
this.env.log.nl();
}
}
else {
if (this.env.flags.interactive) {
this.env.log.info(`Log into your Ionic account!\n` +
`If you don't have one yet, create yours by running: ${color_1.input(`ionic signup`)}`);
this.env.log.nl();
}
}
// TODO: combine with promptToLogin ?
if (askForEmail) {
const email = await this.env.prompt({
type: 'input',
name: 'email',
message: 'Email:',
validate: v => cli_framework_1.combine(cli_framework_1.validators.required, cli_framework_1.validators.email)(v),
});
inputs[0] = email;
}
if (askForPassword) {
if (this.env.flags.interactive) {
const password = await this.env.prompt({
type: 'password',
name: 'password',
message: 'Password:',
mask: '*',
validate: v => cli_framework_1.validators.required(v),
});
inputs[1] = password;
}
else {
inputs[1] = await this.getPasswordFromStdin();
}
}
}
getPasswordFromStdin() {
return new Promise(resolve => {
const rl = readline.createInterface({
input: process.stdin,
terminal: false,
});
rl.on('line', line => {
resolve(line);
rl.close();
});
});
}
async run(inputs, options) {
const [email, password] = inputs;
const sso = !!options['sso'];
if (this.env.session.isLoggedIn()) {
await this.env.session.logout();
this.env.config.set('tokens.telemetry', uuid_1.generateUUID());
}
if (sso) {
this.env.log.info(`Ionic SSO Login\n` +
`During this process, a browser window will open to authenticate you with the identity provider for ${color_1.input(email)}. Please leave this process running until authentication is complete.`);
this.env.log.nl();
await this.env.session.ssoLogin(email);
}
else {
await this.env.session.login(email, password);
}
this.env.log.ok(color_1.success(color_1.strong('You are logged in!')));
}
}
exports.LoginCommand = LoginCommand;