UNPKG

2.38 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const command_1 = require("@oclif/command");
4const base_command_1 = require("../base-command");
5const tokenUrl = 'https://app.bearer.sh/settings';
6class Login extends base_command_1.default {
7 async run() {
8 const { flags } = this.parse(Login);
9 const email = process.env.BEARER_EMAIL || flags.email || (await this.askForString('Enter your email'));
10 const token = process.env.BEARER_TOKEN || (await this.askToken());
11 this.ux.action.start('Logging you in');
12 const status = await this.logUser(email, token);
13 this.ux.action.stop();
14 this.log(status);
15 }
16 async logUser(username, accessToken) {
17 try {
18 const { statusCode, body } = await this.serviceClient.login({ Username: username, Password: accessToken });
19 switch (statusCode) {
20 case 200: {
21 this.bearerConfig.storeBearerConfig(Object.assign({}, body.user, { ExpiresAt: Date.now() + body.authorization.AuthenticationResult.ExpiresIn * 1000, authorization: body.authorization }));
22 return `Successfully logged in as ${username}! 🤘`;
23 }
24 case 401: {
25 this.error('Unauthorized: Invalid credentials');
26 return 'There was an error while trying to login to bearer';
27 }
28 default: {
29 this.error('Unhandled status');
30 this.debug('status:', statusCode, 'body:', JSON.stringify(body));
31 return 'An error occured. Please retry';
32 }
33 }
34 }
35 catch (e) {
36 this.error(e);
37 return 'An error occured';
38 }
39 }
40 async askToken() {
41 this.log('');
42 this.log(this.colors.italic(`Find your token at this location: ${this.colors.bold(tokenUrl)}`));
43 const { token } = await this.inquirer.prompt([
44 {
45 message: 'Enter your token:',
46 type: 'password',
47 name: 'token'
48 }
49 ]);
50 return token;
51 }
52}
53Login.description = 'Login to Bearer platform';
54Login.flags = Object.assign({}, base_command_1.default.flags, { email: command_1.flags.string({ char: 'e' }) });
55Login.args = [];
56exports.default = Login;