1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const cli_helper_1 = require("./cli-helper");
|
4 | const clortho_lite_1 = require("clortho-lite");
|
5 | const cli_1 = require("./cli");
|
6 | const prompts = require("prompts");
|
7 | const request = require("request");
|
8 | const loginEndPoint = "https://www.flagpolejs.com/api/token";
|
9 | const serviceName = "Flagpole JS";
|
10 | const service = new clortho_lite_1.ClorthoService(serviceName);
|
11 | function promptForLogin() {
|
12 | prompts([
|
13 | {
|
14 | type: "text",
|
15 | name: "email",
|
16 | message: "Email Address",
|
17 | validate: function (input) {
|
18 | return /^[^ @]{1,32}@[^ ]{3,}\.[a-z]{2,8}/i.test(input);
|
19 | }
|
20 | },
|
21 | {
|
22 | type: "password",
|
23 | name: "password",
|
24 | message: "Password",
|
25 | validate: function (input) {
|
26 | return /^.{3,16}$/i.test(input);
|
27 | }
|
28 | }
|
29 | ])
|
30 | .then(function (answers) {
|
31 | cli_1.Cli.hideBanner = true;
|
32 | request.post(loginEndPoint, {
|
33 | body: JSON.stringify({
|
34 | email: answers.email,
|
35 | password: answers.password
|
36 | }),
|
37 | headers: {
|
38 | "Content-Type": "application/json"
|
39 | }
|
40 | }, function (err, response, body) {
|
41 | cli_1.Cli.log("");
|
42 | if (err) {
|
43 | cli_1.Cli.log(err);
|
44 | cli_1.Cli.log(body);
|
45 | cli_1.Cli.log("");
|
46 | cli_1.Cli.exit(1);
|
47 | }
|
48 | if (response.statusCode == 201) {
|
49 | let json = JSON.parse(body);
|
50 | if (json.data.token) {
|
51 | service.set("email", answers.email);
|
52 | service
|
53 | .set("token", json.data.token)
|
54 | .then(function (value) {
|
55 | cli_1.Cli.log("Logged in. Saved to your keychain.");
|
56 | cli_1.Cli.log("");
|
57 | cli_1.Cli.exit(0);
|
58 | })
|
59 | .catch(function (err) {
|
60 | cli_1.Cli.log("Error saving credentials to your keychain.");
|
61 | cli_1.Cli.log("");
|
62 | cli_1.Cli.exit(0);
|
63 | });
|
64 | }
|
65 | else {
|
66 | cli_1.Cli.log("Login failed.");
|
67 | cli_1.Cli.log(body);
|
68 | cli_1.Cli.log("");
|
69 | cli_1.Cli.exit(1);
|
70 | }
|
71 | }
|
72 | else {
|
73 | cli_1.Cli.log("Error logging in. Status code: " + response.statusCode);
|
74 | cli_1.Cli.log("");
|
75 | cli_1.Cli.exit(1);
|
76 | }
|
77 | });
|
78 | })
|
79 | .catch(function (err) {
|
80 | cli_1.Cli.log("Error: " + err);
|
81 | cli_1.Cli.log("");
|
82 | cli_1.Cli.exit(1);
|
83 | });
|
84 | }
|
85 | function login() {
|
86 | cli_helper_1.printHeader();
|
87 | cli_helper_1.printSubheader("Login to FlagpoleJS.com");
|
88 | cli_1.Cli.hideBanner = true;
|
89 | cli_1.Cli.log("");
|
90 | cli_1.Cli.log("This site is in early private beta.");
|
91 | cli_1.Cli.getCredentials()
|
92 | .then(function (credentials) {
|
93 | cli_1.Cli.log("");
|
94 | cli_1.Cli.log("You are already logged in as " + credentials.email);
|
95 | cli_1.Cli.log("");
|
96 | cli_1.Cli.log("To sign in with a different account use the command: flagpole logout");
|
97 | cli_1.Cli.log("");
|
98 | cli_1.Cli.exit(0);
|
99 | })
|
100 | .catch(function () {
|
101 | promptForLogin();
|
102 | });
|
103 | }
|
104 | exports.login = login;
|
105 |
|
\ | No newline at end of file |