@qnet/cli
Version:
A powerful command-line interface tool designed for managing and interacting with Quantex projects and services efficiently. Built with modern features and smooth integrations to streamline your workflow.
51 lines (44 loc) • 1.89 kB
JavaScript
import inquirer from 'inquirer';
import chalk from 'chalk';
import figlet from 'figlet';
import gradient from 'gradient-string';
import { createSpinner } from 'nanospinner';
import fetch from 'node-fetch';
import { saveSession } from '../utils/session.js';
export const name = 'login';
export const description = 'Log in to your linked Discord server';
export async function action() {
const title = figlet.textSync('QNET CLI', { horizontalLayout: 'default' });
console.log(gradient.morning.multiline(title));
console.log('Welcome to the authorization page! You are currently linking your CLI to your server.');
console.log();
const { authCode } = await inquirer.prompt([
{
type: 'input',
name: 'authCode',
message: 'Enter your authorization code:',
validate: input => input.trim() ? true : 'Authorization code cannot be empty.'
}
]);
const spinner = createSpinner('Validating authorization code...').start();
try {
const response = await fetch(`https://network.quantexcorp.net/api/utils/authenticate?authCode=${encodeURIComponent(authCode.trim())}`);
if (response.status === 200) {
const data = await response.json();
spinner.success({ text: chalk.green('Successfully authorized your connection.') });
saveSession({
authCode: authCode.trim(),
guildName: data.guildName || 'Unknown',
});
console.log(
chalk.green(`\nLogged in as ${chalk.bold(data.guildName)}.`)
);
} else if (response.status === 403) {
spinner.error({ text: chalk.red('Invalid or expired authorization code.') });
} else {
spinner.error({ text: chalk.red(`Unexpected response status: ${response.status}`) });
}
} catch (err) {
spinner.error({ text: chalk.red('Failed to connect to authentication server.') });
}
}