import open from 'open'; import { Spinner } from 'clui'; import { ICli, ICliRoutes } from '../types'; import Config from '../config'; import { InquirerService, ConfigStoreService, AuthService, GraphqlService } from '../services'; import { TryCatch } from '../helpers'; class AuthCli implements ICli { public routes: ICliRoutes[] = []; constructor() { this.routes.push({ command: 'login', description: 'Interactive login', handler: this.handleLogin, }); this.routes.push({ command: 'me', description: 'Get current logged in user info', handler: this.handleMe, }); } public async handleLogin() { const spinner = new Spinner('Authenticating...'); try { const browserSpinner = new Spinner('Opening browser...'); browserSpinner.start(); await open(`${Config.CLI_URL}/cli`); browserSpinner.stop(); const token = await InquirerService.askForAuthToken(); ConfigStoreService.set('token', token); spinner.start(); await AuthService.me(); spinner.stop(); console.log('Login success'); } catch (err) { spinner.stop(); console.log('Login failed'); } } @TryCatch() public async handleMe() { if (!GraphqlService.getToken()) { console.log(`Please run "${Config.PROJECT_NAME} login" first.`); return; } const response = await AuthService.me(); console.log(response); } } export default new AuthCli();