UNPKG

7.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.LoginCommand = void 0;
4const cli_framework_1 = require("@ionic/cli-framework");
5const chalk = require("chalk");
6const readline = require("readline");
7const color_1 = require("../lib/color");
8const command_1 = require("../lib/command");
9const errors_1 = require("../lib/errors");
10const uuid_1 = require("../lib/utils/uuid");
11class LoginCommand extends command_1.Command {
12 async getMetadata() {
13 return {
14 name: 'login',
15 type: 'global',
16 summary: 'Log in to Ionic',
17 description: `
18Authenticate 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 open a browser where you can submit your credentials.
19
20If 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 <email> <password>')} to log in, then use ${color_1.input('ionic config get -g tokens.user')} to print the token. (${color_1.strong('Note')}: Tokens retrieved from the browser login are short-lived and not recommended for use with ${color_1.input('IONIC_TOKEN')}.)
21
22${color_1.input('ionic login')} will also accept ${color_1.input('password')} through stdin, e.g.: ${color_1.input('echo "<password>" | ionic login <email>')}.
23
24If you need to create an Ionic account, use ${color_1.input('ionic signup')} or the Ionic Website[^signup].
25
26You can reset your password in the Dashboard[^reset-password].
27
28If you are having issues logging in, please get in touch with our Support[^support-request].
29 `,
30 footnotes: [
31 {
32 id: 'signup',
33 url: 'https://ionicframework.com/signup',
34 },
35 {
36 id: 'reset-password',
37 url: 'https://dashboard.ionicframework.com/reset-password',
38 shortUrl: 'https://ion.link/reset-password',
39 },
40 {
41 id: 'support-request',
42 url: 'https://ion.link/support-request',
43 },
44 ],
45 exampleCommands: ['', 'john@example.com', 'hello@example.com secret'],
46 inputs: [
47 {
48 name: 'email',
49 summary: 'Your email address',
50 private: true,
51 },
52 {
53 name: 'password',
54 summary: 'Your password',
55 private: true,
56 },
57 ],
58 };
59 }
60 async preRun(inputs, options) {
61 if (options['email'] || options['password']) {
62 throw new errors_1.FatalException(`${color_1.input('email')} and ${color_1.input('password')} are command arguments, not options. Please try this:\n` +
63 `${color_1.input('ionic login <email> <password>')}\n`);
64 }
65 if (options['sso']) {
66 this.env.log.warn(`The ${color_1.strong('--sso')} flag is no longer necessary.\n` +
67 `SSO login has been upgraded to OpenID login, which is now the new default authentication flow of ${color_1.input('ionic login')}. Refresh tokens are used to automatically re-authenticate sessions.`);
68 this.env.log.nl();
69 }
70 // ask for password only if the user specifies an email
71 const validateEmail = !!inputs[0];
72 const askForPassword = inputs[0] && !inputs[1];
73 if (this.env.session.isLoggedIn()) {
74 const email = this.env.config.get('user.email');
75 const extra = askForPassword
76 ? (this.env.flags.interactive ? `Prompting for new credentials.\n\nUse ${chalk.yellow('Ctrl+C')} to cancel and remain logged in.` : '')
77 : 'You will be logged out beforehand.';
78 if (this.env.flags.interactive) {
79 this.env.log.warn('You will be logged out.\n' +
80 `You are already logged in${email ? ' as ' + color_1.strong(email) : ''}! ${extra}`);
81 this.env.log.nl();
82 }
83 }
84 else {
85 if (this.env.flags.interactive) {
86 this.env.log.info(`Log into your Ionic account!\n` +
87 `If you don't have one yet, create yours by running: ${color_1.input(`ionic signup`)}`);
88 this.env.log.nl();
89 }
90 }
91 // TODO: combine with promptToLogin ?
92 if (validateEmail) {
93 const validatedEmail = cli_framework_1.validators.email(inputs[0]);
94 if (validatedEmail !== true) {
95 this.env.log.warn(`${validatedEmail}. \n Please enter a valid email address.`);
96 if (this.env.flags.interactive) {
97 const email = await this.env.prompt({
98 type: 'input',
99 name: 'email',
100 message: 'Email:',
101 validate: v => cli_framework_1.combine(cli_framework_1.validators.required, cli_framework_1.validators.email)(v),
102 });
103 inputs[0] = email;
104 }
105 else {
106 throw new errors_1.FatalException('Invalid email');
107 }
108 }
109 }
110 if (askForPassword) {
111 if (this.env.flags.interactive) {
112 const password = await this.env.prompt({
113 type: 'password',
114 name: 'password',
115 message: 'Password:',
116 mask: '*',
117 validate: v => cli_framework_1.validators.required(v),
118 });
119 inputs[1] = password;
120 }
121 else {
122 inputs[1] = await this.getPasswordFromStdin();
123 }
124 }
125 }
126 getPasswordFromStdin() {
127 return new Promise(resolve => {
128 const rl = readline.createInterface({
129 input: process.stdin,
130 terminal: false,
131 });
132 rl.on('line', line => {
133 resolve(line);
134 rl.close();
135 });
136 });
137 }
138 async run(inputs, options) {
139 const [email, password] = inputs;
140 if (email && password) {
141 await this.logout();
142 await this.env.session.login(email, password);
143 }
144 else {
145 if (!this.env.flags.interactive) {
146 throw new errors_1.FatalException('Refusing to attempt browser login in non-interactive mode.\n' +
147 `If you are attempting to log in, make sure you are using a modern, interactive terminal. Otherwise, you can log in using inline username and password with ${color_1.input('ionic login <email> <password>')}. See ${color_1.input('ionic login --help')} for more details.`);
148 }
149 this.env.log.info(`During this process, a browser window will open to authenticate you. Please leave this process running until authentication is complete.`);
150 this.env.log.nl();
151 const login = await this.env.prompt({
152 type: 'confirm',
153 name: 'continue',
154 message: 'Open the browser to log in to your Ionic account?',
155 default: true,
156 });
157 if (login) {
158 await this.logout();
159 await this.env.session.webLogin();
160 }
161 else {
162 return;
163 }
164 }
165 this.env.log.ok(color_1.success(color_1.strong('You are logged in!')));
166 }
167 async logout() {
168 if (this.env.session.isLoggedIn()) {
169 await this.env.session.logout();
170 this.env.config.set('tokens.telemetry', uuid_1.generateUUID());
171 }
172 }
173}
174exports.LoginCommand = LoginCommand;