1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const cli_framework_1 = require("@ionic/cli-framework");
|
4 | const guards_1 = require("../guards");
|
5 | const color_1 = require("./color");
|
6 | const errors_1 = require("./errors");
|
7 | const http_1 = require("./http");
|
8 | const open_1 = require("./open");
|
9 | class BaseSession {
|
10 | constructor(e) {
|
11 | this.e = e;
|
12 | }
|
13 | async logout() {
|
14 | this.e.config.unset('org.id');
|
15 | this.e.config.unset('user.id');
|
16 | this.e.config.unset('user.email');
|
17 | this.e.config.unset('tokens.user');
|
18 | this.e.config.set('git.setup', false);
|
19 | }
|
20 | isLoggedIn() {
|
21 | return typeof this.e.config.get('tokens.user') === 'string';
|
22 | }
|
23 | getUser() {
|
24 | const userId = this.e.config.get('user.id');
|
25 | if (!userId) {
|
26 | throw new errors_1.SessionException(`Oops, sorry! You'll need to log in:\n ${color_1.input('ionic login')}\n\n` +
|
27 | `You can create a new account by signing up:\n\n ${color_1.input('ionic signup')}\n`);
|
28 | }
|
29 | return { id: userId };
|
30 | }
|
31 | getUserToken() {
|
32 | const userToken = this.e.config.get('tokens.user');
|
33 | if (!userToken) {
|
34 | throw new errors_1.SessionException(`Oops, sorry! You'll need to log in:\n ${color_1.input('ionic login')}\n\n` +
|
35 | `You can create a new account by signing up:\n\n ${color_1.input('ionic signup')}\n`);
|
36 | }
|
37 | return userToken;
|
38 | }
|
39 | }
|
40 | exports.BaseSession = BaseSession;
|
41 | class ProSession extends BaseSession {
|
42 | async login(email, password) {
|
43 | const { req } = await this.e.client.make('POST', '/login');
|
44 | req.send({ email, password, source: 'cli' });
|
45 | try {
|
46 | const res = await this.e.client.do(req);
|
47 | if (!guards_1.isLoginResponse(res)) {
|
48 | const data = res.data;
|
49 | if (hasTokenAttribute(data)) {
|
50 | data.token = '*****';
|
51 | }
|
52 | throw new errors_1.FatalException('API request was successful, but the response format was unrecognized.\n' +
|
53 | http_1.formatResponseError(req, res.meta.status, data));
|
54 | }
|
55 | const { token, user } = res.data;
|
56 | if (this.e.config.get('user.id') !== user.id) {
|
57 | await this.logout();
|
58 | }
|
59 | this.e.config.set('user.id', user.id);
|
60 | this.e.config.set('user.email', email);
|
61 | this.e.config.set('tokens.user', token);
|
62 | }
|
63 | catch (e) {
|
64 | if (guards_1.isSuperAgentError(e) && (e.response.status === 401 || e.response.status === 403)) {
|
65 | throw new errors_1.SessionException('Incorrect email or password.');
|
66 | }
|
67 | throw e;
|
68 | }
|
69 | }
|
70 | async ssoLogin(email) {
|
71 | const { AuthClient } = await Promise.resolve().then(() => require('./auth'));
|
72 | const { Auth0OAuth2Flow } = await Promise.resolve().then(() => require('./sso'));
|
73 | const authClient = new AuthClient(this.e);
|
74 | const { uuid: connection } = await authClient.connections.load(email);
|
75 | const flow = new Auth0OAuth2Flow({ audience: this.e.config.get('urls.api'), email, connection }, this.e);
|
76 | const token = await flow.run();
|
77 | await this.tokenLogin(token);
|
78 | this.e.config.set('org.id', connection);
|
79 | }
|
80 | async tokenLogin(token) {
|
81 | const { UserClient } = await Promise.resolve().then(() => require('./user'));
|
82 | const userClient = new UserClient(token, this.e);
|
83 | try {
|
84 | const user = await userClient.loadSelf();
|
85 | const user_id = user.id;
|
86 | if (this.e.config.get('user.id') !== user_id) {
|
87 | await this.logout();
|
88 | }
|
89 | this.e.config.set('user.id', user_id);
|
90 | this.e.config.set('user.email', user.email);
|
91 | this.e.config.set('tokens.user', token);
|
92 | }
|
93 | catch (e) {
|
94 | if (guards_1.isSuperAgentError(e) && (e.response.status === 401 || e.response.status === 403)) {
|
95 | throw new errors_1.SessionException('Invalid auth token.');
|
96 | }
|
97 | throw e;
|
98 | }
|
99 | }
|
100 | }
|
101 | exports.ProSession = ProSession;
|
102 | async function promptToLogin(env) {
|
103 | const { validators } = await Promise.resolve().then(() => require('@ionic/cli-framework'));
|
104 | env.log.nl();
|
105 | env.log.msg(`Log in to your Ionic account!\n` +
|
106 | `If you don't have one yet, create yours by running: ${color_1.input(`ionic signup`)}\n`);
|
107 | const email = await env.prompt({
|
108 | type: 'input',
|
109 | name: 'email',
|
110 | message: 'Email:',
|
111 | validate: v => cli_framework_1.combine(validators.required, validators.email)(v),
|
112 | });
|
113 | const password = await env.prompt({
|
114 | type: 'password',
|
115 | name: 'password',
|
116 | message: 'Password:',
|
117 | mask: '*',
|
118 | validate: v => validators.required(v),
|
119 | });
|
120 | await env.session.login(email, password);
|
121 | }
|
122 | exports.promptToLogin = promptToLogin;
|
123 | async function promptToSignup(env) {
|
124 | env.log.nl();
|
125 | env.log.msg(`Join the Ionic Community! 💙\n` +
|
126 | `Connect with millions of developers on the Ionic Forum and get access to live events, news updates, and more.\n\n`);
|
127 | const create = await env.prompt({
|
128 | type: 'confirm',
|
129 | name: 'create',
|
130 | message: 'Create free Ionic account?',
|
131 | default: false,
|
132 | });
|
133 | if (create) {
|
134 | const dashUrl = env.config.getDashUrl();
|
135 | await open_1.openUrl(`${dashUrl}/signup?source=cli`);
|
136 | }
|
137 | }
|
138 | exports.promptToSignup = promptToSignup;
|
139 | function hasTokenAttribute(r) {
|
140 | return r && typeof r === 'object' && typeof r.token === 'string';
|
141 | }
|