UNPKG

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