123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- const tslib_1 = require("tslib");
- const chalk_1 = require("chalk");
- const guards_1 = require("../guards");
- const errors_1 = require("./errors");
- const http_1 = require("./http");
- class BaseSession {
- constructor(config, project, client) {
- this.config = config;
- this.project = project;
- this.client = client;
- }
- isLoggedIn() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const c = yield this.config.load();
- return typeof c.tokens.user === 'string';
- });
- }
- logout() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const c = yield this.config.load();
- c.user = {};
- c.tokens.appUser = {};
- delete c.tokens.user;
- c.git.setup = false;
- });
- }
- getUser() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- throw new errors_1.FatalException('Invalid operation for Cloud session.');
- });
- }
- getUserToken() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const c = yield this.config.load();
- if (!c.tokens.user) {
- throw new errors_1.SessionException(`Oops, sorry! You'll need to log in:\n ${chalk_1.default.green('ionic login')}\n\n` +
- `You can create a new account by signing up:\n\n ${chalk_1.default.green('ionic signup')}\n`);
- }
- return c.tokens.user;
- });
- }
- }
- exports.BaseSession = BaseSession;
- class CloudSession extends BaseSession {
- login(email, password) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const { req } = yield this.client.make('POST', '/login');
- req.send({ email, password });
- try {
- const res = yield this.client.do(req);
- if (!guards_1.isLegacyLoginResponse(res)) {
- throw http_1.createFatalAPIFormat(req, res);
- }
- const { token, user_id } = res.data;
- const c = yield this.config.load();
- if (c.user.id !== Number(user_id)) {
- yield this.logout();
- }
- c.user.id = Number(user_id);
- c.user.email = email;
- c.tokens.user = token;
- }
- catch (e) {
- if (guards_1.isSuperAgentError(e) && e.response.status === 401) {
- throw new errors_1.SessionException('Incorrect email or password.');
- }
- throw e;
- }
- });
- }
- getAppUserToken(app_id) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- if (!app_id) {
- app_id = yield this.project.loadAppId();
- }
- const c = yield this.config.load();
- if (!c.tokens.appUser[app_id]) {
- const token = yield this.getUserToken();
- const paginator = yield this.client.paginate({
- reqgen: () => tslib_1.__awaiter(this, void 0, void 0, function* () {
- const { req } = yield this.client.make('GET', '/auth/tokens');
- req.set('Authorization', `Bearer ${token}`).query({ 'page_size': 100, type: 'app-user' });
- return { req };
- }),
- guard: guards_1.isAuthTokensResponse,
- });
- for (let r of paginator) {
- const res = yield r;
- for (let token of res.data) {
- c.tokens.appUser[token.details.app_id] = token.token;
- }
- }
- }
- // TODO: if this is a new app, an app-user token may not exist for the user
- // TODO: if tokens are invalidated, what do (hint: app tokens)
- if (!c.tokens.appUser[app_id]) {
- throw new errors_1.SessionException(`A token does not exist for your account on app ${chalk_1.default.bold(app_id)}.`);
- }
- return c.tokens.appUser[app_id];
- });
- }
- }
- exports.CloudSession = CloudSession;
- class ProSession extends BaseSession {
- login(email, password) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const { req } = yield this.client.make('POST', '/login');
- req.send({ email, password, source: 'cli' });
- try {
- const res = yield this.client.do(req);
- if (!guards_1.isProLoginResponse(res)) {
- throw http_1.createFatalAPIFormat(req, res);
- }
- const { token, user } = res.data;
- const c = yield this.config.load();
- const user_id = user.id;
- if (c.user.id !== user_id) {
- yield this.logout();
- }
- c.user.id = user_id;
- c.user.email = email;
- c.tokens.user = token;
- }
- catch (e) {
- if (guards_1.isSuperAgentError(e) && e.response.status === 401) {
- throw new errors_1.SessionException('Incorrect email or password.');
- }
- throw e;
- }
- });
- }
- getUser() {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const c = yield this.config.load();
- if (!c.user.id) {
- throw new errors_1.SessionException(`Oops, sorry! You'll need to log in:\n ${chalk_1.default.green('ionic login')}\n\n` +
- `You can create a new account by signing up:\n\n ${chalk_1.default.green('ionic signup')}\n`);
- }
- return { id: Number(c.user.id) };
- });
- }
- getAppUserToken(app_id) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- return this.getUserToken();
- });
- }
- }
- exports.ProSession = ProSession;
- function promptToLogin(env) {
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
- const { validators } = yield Promise.resolve().then(() => require('@ionic/cli-framework/lib'));
- env.log.msg(`Log into your Ionic account\nIf you don't have one yet, create yours by running: ${chalk_1.default.green(`ionic signup`)}\n`);
- const email = yield env.prompt({
- type: 'input',
- name: 'email',
- message: 'Email:',
- validate: v => validators.email(v),
- });
- const password = yield env.prompt({
- type: 'password',
- name: 'password',
- message: 'Password:'
- });
- yield env.session.login(email, password);
- });
- }
- exports.promptToLogin = promptToLogin;
|