login.ts 1.19 KB
Newer Older
Bradley Hilton's avatar
Bradley Hilton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import { Command, flags } from '@oclif/command';
import chalk from 'chalk';
import cli from 'cli-ux';
import * as inquirer from 'inquirer';

import { CloudAuth } from '../misc/cloudAuth';

export default class Login extends Command {
    public static description = 'steps through the process to log in with Rocket.Chat Cloud';

    public static flags = {
        help: flags.help({ char: 'h' }),
    };

    public async run() {
        inquirer.registerPrompt('checkbox-plus', require('inquirer-checkbox-plus-prompt'));

        const cloudAuth = new CloudAuth();
        const hasToken = await cloudAuth.hasToken();
        if (hasToken) {
21
            cli.action.start(chalk.green('verifying') + ' your token...');
Bradley Hilton's avatar
Bradley Hilton committed
22
            await cloudAuth.getToken();
23
            cli.action.stop(chalk.green('success, you are already logged in!'));
Bradley Hilton's avatar
Bradley Hilton committed
24 25
        } else {
            try {
26
                cli.action.start(chalk.green('waiting') + ' for authorization...');
Bradley Hilton's avatar
Bradley Hilton committed
27
                await cloudAuth.executeAuthFlow();
28
                cli.action.stop(chalk.green('success!'));
Bradley Hilton's avatar
Bradley Hilton committed
29
            } catch (e) {
30
                cli.action.stop(chalk.red('failed to authenticate.'));
Bradley Hilton's avatar
Bradley Hilton committed
31 32 33 34 35
                return;
            }
        }
    }
}