forked from flagpolejs/flagpole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.js
executable file
·93 lines (93 loc) · 3.5 KB
/
login.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cli_helper_1 = require("./cli-helper");
const clortho_lite_1 = require("clortho-lite");
const request = require('request');
const { prompt } = require('enquirer');
const loginEndPoint = 'https://us-central1-flagpolejs-5ea61.cloudfunctions.net/api/token';
const serviceName = 'Flagpole JS';
const service = new clortho_lite_1.ClorthoService(serviceName);
function promptForLogin() {
prompt([
{
type: 'input',
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_helper_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_helper_1.Cli.log('');
if (err) {
cli_helper_1.Cli.log(err);
cli_helper_1.Cli.log('');
cli_helper_1.Cli.exit(1);
}
if (response.statusCode == 201) {
let json = JSON.parse(body);
if (/[a-z0-9]{16}/.test(json.data.token)) {
service.set('email', answers.email);
service.set('token', json.data.token)
.then(function (value) {
cli_helper_1.Cli.log('Logged in. Saved to your keychain.');
cli_helper_1.Cli.log('');
cli_helper_1.Cli.exit(0);
})
.catch(function (err) {
cli_helper_1.Cli.log('Error saving credentials to your keychain.');
cli_helper_1.Cli.log('');
cli_helper_1.Cli.exit(0);
});
}
else {
cli_helper_1.Cli.log('Login failed.');
cli_helper_1.Cli.log('');
cli_helper_1.Cli.exit(1);
}
}
else {
cli_helper_1.Cli.log('Error logging in. Status code: ' + response.statusCode);
cli_helper_1.Cli.log('');
cli_helper_1.Cli.exit(1);
}
});
}).catch(function (err) {
cli_helper_1.Cli.log('Error: ' + err);
cli_helper_1.Cli.log('');
cli_helper_1.Cli.exit(1);
});
}
function login() {
cli_helper_1.printHeader();
cli_helper_1.printSubheader('Login to FlagpoleJS.com');
cli_helper_1.Cli.hideBanner = true;
cli_helper_1.Cli.log('');
cli_helper_1.Cli.log('This site is in early private beta.');
cli_helper_1.Cli.getCredentials().then(function (credentials) {
cli_helper_1.Cli.log('');
cli_helper_1.Cli.log('You are already logged in as ' + credentials.email);
cli_helper_1.Cli.log('');
cli_helper_1.Cli.log('To sign in with a different account use the command: flagpole logout');
cli_helper_1.Cli.log('');
cli_helper_1.Cli.exit(0);
}).catch(function () {
promptForLogin();
});
}
exports.login = login;