/
lumber-login.js
53 lines (45 loc) · 1.81 KB
/
lumber-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
const program = require('commander');
const inquirer = require('inquirer');
const context = require('./context');
const initContext = require('./context/init');
const { EMAIL_REGEX } = require('./utils/regexs');
initContext(context);
const {
logger, authenticator, oidcAuthenticator, errorHandler, applicationTokenService,
} = context.inject();
if (!logger) throw new Error('Missing dependency logger');
if (!authenticator) throw new Error('Missing dependency authenticator');
if (!errorHandler) throw new Error('Missing dependency errorHandler');
if (!applicationTokenService) throw new Error('Missing dependency applicationTokenService');
program
.description('Log into Forest Admin API')
.option('-e, --email <email>', 'Your Forest Admin account email')
.option('-P, --password <password>', 'Your Forest Admin account password (ignored if token is set)')
.option('-t, --token <token>', 'Your Forest Admin account token (replaces password)')
.parse(process.argv);
(async () => {
let { email, token } = program;
const { password } = program;
if (!token && !password) {
const sessionToken = await oidcAuthenticator.authenticate();
token = await applicationTokenService.generateApplicationToken(sessionToken);
} else {
if (!email) {
({ email } = await inquirer.prompt([{
type: 'input',
name: 'email',
message: 'What\'s your email address?',
validate: (input) => {
if (EMAIL_REGEX.test(input)) { return true; }
return input ? 'Invalid email' : 'Please enter your email address.';
},
}]));
}
token = await authenticator.loginWithEmailOrTokenArgv({ ...program, email });
}
authenticator.saveToken(token);
logger.success('Login successful');
process.exit(0);
})().catch(async (error) => {
await errorHandler.handle(error);
});