UNPKG

6.56 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const cli_framework_1 = require("@ionic/cli-framework");
4const chalk_1 = require("chalk");
5const readline = require("readline");
6const color_1 = require("../lib/color");
7const command_1 = require("../lib/command");
8const errors_1 = require("../lib/errors");
9const uuid_1 = require("../lib/utils/uuid");
10class LoginCommand extends command_1.Command {
11 async getMetadata() {
12 return {
13 name: 'login',
14 type: 'global',
15 summary: 'Log in to Ionic',
16 description: `
17Authenticate 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.
18
19If 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.
20
21${color_1.input('ionic login')} will also accept ${color_1.input('password')} through stdin, e.g.: ${color_1.input('echo "<password>" | ionic login <email>')}.
22
23If you need to create an Ionic account, use ${color_1.input('ionic signup')}.
24
25You can reset your password in the Dashboard[^reset-password].
26
27If you are having issues logging in, please get in touch with our Support[^support-request].
28 `,
29 footnotes: [
30 {
31 id: 'reset-password',
32 url: 'https://dashboard.ionicframework.com/reset-password',
33 shortUrl: 'https://ion.link/reset-password',
34 },
35 {
36 id: 'support-request',
37 url: 'https://ion.link/support-request',
38 },
39 ],
40 exampleCommands: ['', 'john@example.com', 'hello@example.com secret'],
41 inputs: [
42 {
43 name: 'email',
44 summary: 'Your email address',
45 validators: [cli_framework_1.validators.required, cli_framework_1.validators.email],
46 private: true,
47 },
48 {
49 name: 'password',
50 summary: 'Your password',
51 // this is a hack since sso is hidden, no need to make password not required for it
52 validators: process.argv.includes('--sso') ? [] : [cli_framework_1.validators.required],
53 private: true,
54 },
55 ],
56 options: [
57 {
58 name: 'sso',
59 type: Boolean,
60 summary: 'Open a window to log in with the SSO provider associated with your email',
61 groups: ["hidden" /* HIDDEN */],
62 },
63 ],
64 };
65 }
66 async preRun(inputs, options) {
67 const sso = !!options['sso'];
68 if (options['email'] || options['password']) {
69 throw new errors_1.FatalException(`${color_1.input('email')} and ${color_1.input('password')} are command arguments, not options. Please try this:\n` +
70 `${color_1.input('ionic login <email> <password>')}\n`);
71 }
72 const askForEmail = !inputs[0];
73 const askForPassword = !sso && !inputs[1];
74 if (this.env.session.isLoggedIn()) {
75 const email = this.env.config.get('user.email');
76 const extra = askForEmail || askForPassword
77 ? (this.env.flags.interactive ? `Prompting for new credentials.\n\nUse ${chalk_1.default.yellow('Ctrl+C')} to cancel and remain logged in.` : '')
78 : 'You will be logged out beforehand.';
79 if (this.env.flags.interactive) {
80 this.env.log.warn('You will be logged out.\n' +
81 `You are already logged in${email ? ' as ' + color_1.strong(email) : ''}! ${extra}`);
82 this.env.log.nl();
83 }
84 }
85 else {
86 if (this.env.flags.interactive) {
87 this.env.log.info(`Log into your Ionic account!\n` +
88 `If you don't have one yet, create yours by running: ${color_1.input(`ionic signup`)}`);
89 this.env.log.nl();
90 }
91 }
92 // TODO: combine with promptToLogin ?
93 if (askForEmail) {
94 const email = await this.env.prompt({
95 type: 'input',
96 name: 'email',
97 message: 'Email:',
98 validate: v => cli_framework_1.combine(cli_framework_1.validators.required, cli_framework_1.validators.email)(v),
99 });
100 inputs[0] = email;
101 }
102 if (askForPassword) {
103 if (this.env.flags.interactive) {
104 const password = await this.env.prompt({
105 type: 'password',
106 name: 'password',
107 message: 'Password:',
108 mask: '*',
109 validate: v => cli_framework_1.validators.required(v),
110 });
111 inputs[1] = password;
112 }
113 else {
114 inputs[1] = await this.getPasswordFromStdin();
115 }
116 }
117 }
118 getPasswordFromStdin() {
119 return new Promise(resolve => {
120 const rl = readline.createInterface({
121 input: process.stdin,
122 terminal: false,
123 });
124 rl.on('line', line => {
125 resolve(line);
126 rl.close();
127 });
128 });
129 }
130 async run(inputs, options) {
131 const [email, password] = inputs;
132 const sso = !!options['sso'];
133 if (this.env.session.isLoggedIn()) {
134 await this.env.session.logout();
135 this.env.config.set('tokens.telemetry', uuid_1.generateUUID());
136 }
137 if (sso) {
138 this.env.log.info(`Ionic SSO Login\n` +
139 `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.`);
140 this.env.log.nl();
141 await this.env.session.ssoLogin(email);
142 }
143 else {
144 await this.env.session.login(email, password);
145 }
146 this.env.log.ok(color_1.success(color_1.strong('You are logged in!')));
147 }
148}
149exports.LoginCommand = LoginCommand;