UNPKG

flagpole

Version:

Simple and fast DOM integration, headless or headful browser, and REST API testing framework.

105 lines 3.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cli_helper_1 = require("./cli-helper"); const clortho_lite_1 = require("clortho-lite"); const cli_1 = require("./cli"); const prompts = require("prompts"); const request = require("request"); const loginEndPoint = "https://www.flagpolejs.com/api/token"; const serviceName = "Flagpole JS"; const service = new clortho_lite_1.ClorthoService(serviceName); function promptForLogin() { prompts([ { type: "text", name: "email", message: "Email Address", validate: function (input) { return /^[^ @]{1,32}@[^ ]{3,}\.[a-z]{2,8}/i.test(input); } }, { type: "password", name: "password", message: "Password", validate: function (input) { return /^.{3,16}$/i.test(input); } } ]) .then(function (answers) { cli_1.Cli.hideBanner = true; request.post(loginEndPoint, { body: JSON.stringify({ email: answers.email, password: answers.password }), headers: { "Content-Type": "application/json" } }, function (err, response, body) { cli_1.Cli.log(""); if (err) { cli_1.Cli.log(err); cli_1.Cli.log(body); cli_1.Cli.log(""); cli_1.Cli.exit(1); } if (response.statusCode == 201) { let json = JSON.parse(body); if (json.data.token) { service.set("email", answers.email); service .set("token", json.data.token) .then(function (value) { cli_1.Cli.log("Logged in. Saved to your keychain."); cli_1.Cli.log(""); cli_1.Cli.exit(0); }) .catch(function (err) { cli_1.Cli.log("Error saving credentials to your keychain."); cli_1.Cli.log(""); cli_1.Cli.exit(0); }); } else { cli_1.Cli.log("Login failed."); cli_1.Cli.log(body); cli_1.Cli.log(""); cli_1.Cli.exit(1); } } else { cli_1.Cli.log("Error logging in. Status code: " + response.statusCode); cli_1.Cli.log(""); cli_1.Cli.exit(1); } }); }) .catch(function (err) { cli_1.Cli.log("Error: " + err); cli_1.Cli.log(""); cli_1.Cli.exit(1); }); } function login() { cli_helper_1.printHeader(); cli_helper_1.printSubheader("Login to FlagpoleJS.com"); cli_1.Cli.hideBanner = true; cli_1.Cli.log(""); cli_1.Cli.log("This site is in early private beta."); cli_1.Cli.getCredentials() .then(function (credentials) { cli_1.Cli.log(""); cli_1.Cli.log("You are already logged in as " + credentials.email); cli_1.Cli.log(""); cli_1.Cli.log("To sign in with a different account use the command: flagpole logout"); cli_1.Cli.log(""); cli_1.Cli.exit(0); }) .catch(function () { promptForLogin(); }); } exports.login = login; //# sourceMappingURL=login.js.map