UNPKG

@simbachain/hardhat

Version:
119 lines 6.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.login = void 0; const web3_suites_1 = require("@simbachain/web3-suites"); const axios_1 = __importDefault(require("axios")); const chalk_1 = __importDefault(require("chalk")); /** * command for user to login (acquire auth token, and choose org and app from blocks) * @param interactive - if true, then choose org and app from prompts * Also, if true, auth token will be generated through device login flow * If false, auth token will be generated through client creds flow, * so SIMBA_AUTH_CLIENT_ID and SIMBA_AUTH_CLIENT_SECRET need to be present * in .simbachain.env, simbachain.env, or .env in project root or SIMBA_HOME * @param org - if !interactive, org must be specified, or be present in simba.json * @param app - if !interactive, app must be specified or be present in simba.json * @returns */ const login = async (interactive = true, org, app) => { web3_suites_1.SimbaConfig.log.debug(`:: ENTER : interactive: ${interactive}, org: ${org}, app: ${app}`); const simbaConfig = new web3_suites_1.SimbaConfig(); const authStore = await simbaConfig.authStore(); const previousSimbaJson = web3_suites_1.SimbaConfig.ProjectConfigStore.all; if (!authStore) { web3_suites_1.SimbaConfig.log.error(web3_suites_1.authErrors.badAuthProviderInfo); return Promise.resolve(new Error(web3_suites_1.authErrors.badAuthProviderInfo)); } if (!interactive) { if (org && !app) { const message = `\nsimba: if specifying an org in non-interactive mode, you must specify an app.`; web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`${message}`)}`); throw new Error(message); } let orgData; let appData; if (!org || !app) { const orgFromSimbaJson = web3_suites_1.SimbaConfig.ProjectConfigStore.get("organisation"); try { const orgName = orgFromSimbaJson.name; web3_suites_1.SimbaConfig.log.info(`${chalk_1.default.cyanBright(`no org was specified in login command; logging in using org ${orgName} from simba.json`)}`); } catch (error) { const message = `no organisation specified in your login command, and no organisation present in your simba.json. Please login in interactive mode and choose your organisation, or use the --org <org> flag in your non-interactive login command.`; web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`${message}`)}`); throw new Error(message); } const appFromSimbaJson = web3_suites_1.SimbaConfig.ProjectConfigStore.get("application"); try { const appName = appFromSimbaJson.name; web3_suites_1.SimbaConfig.log.info(`${chalk_1.default.cyanBright(`no app was specified in login command; logging in using app ${appName} from simba.json`)}`); } catch (error) { const message = `no app specified in your login command, and no application present in your simba.json. Please login in interactive mode and choose your application, or use the --app <app> flag in your non-interactive login command.`; web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`${message}`)}`); throw new Error(message); } } authStore.logout(); try { await authStore.performLogin(interactive); if (org) { orgData = await (0, web3_suites_1.chooseOrganisationFromName)(simbaConfig, org); } if (app) { appData = await (0, web3_suites_1.chooseApplicationFromName)(simbaConfig, app); } if (orgData.id != appData.organisation.id) { web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`the selected app is not part of the selected organisation. Please login in interactive mode and choose your application, or use the --app <app> flag in your non-interactive login command.`)}`); return; } web3_suites_1.SimbaConfig.resetSimbaJson(previousSimbaJson, org); web3_suites_1.SimbaConfig.log.info(`${chalk_1.default.greenBright(`Logged in to SIMBA Chain!`)}`); web3_suites_1.SimbaConfig.log.debug(`:: EXIT :`); return; } catch (error) { return; } } try { authStore.logout(); if (!authStore.isLoggedIn()) { await authStore.performLogin(); } else { try { await authStore.refreshToken(); } catch (e) { await authStore.performLogin(); } } const org = await (0, web3_suites_1.chooseOrganisationFromList)(simbaConfig); if (!org) { web3_suites_1.SimbaConfig.log.debug(`:: EXIT :`); return Promise.resolve(new Error('No Organisation Selected!')); } const app = await (0, web3_suites_1.chooseApplicationFromList)(simbaConfig); if (!app) { web3_suites_1.SimbaConfig.log.debug(`:: ENTER :`); return Promise.resolve(new Error('No Application Selected!')); } web3_suites_1.SimbaConfig.resetSimbaJson(previousSimbaJson, org); web3_suites_1.SimbaConfig.log.info(`${chalk_1.default.cyanBright('\nsimba: Logged in with organisation')} ${chalk_1.default.greenBright(org.display_name)} ${chalk_1.default.cyanBright('and application')} ${chalk_1.default.greenBright(app.display_name)}`); } catch (error) { if (axios_1.default.isAxiosError(error) && error.response) { web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: EXIT : ${JSON.stringify(error.response.data)}`)}`); } else { web3_suites_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: EXIT : ${JSON.stringify(error)}`)}`); } return; } }; exports.login = login; //# sourceMappingURL=login.js.map