@aerocorp/cli
Version:
AeroCorp CLI 5.1.0 - Future-Proofed Enterprise Infrastructure with Live Preview, Tunneling & Advanced DevOps
179 lines • 8.27 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthService = void 0;
const axios_1 = __importDefault(require("axios"));
const chalk_1 = __importDefault(require("chalk"));
const inquirer_1 = __importDefault(require("inquirer"));
const ora_1 = __importDefault(require("ora"));
const config_1 = require("./config");
class AuthService {
constructor() {
this.configService = new config_1.ConfigService();
}
async login(options = {}) {
console.log(chalk_1.default.cyan('╔══════════════════╗'));
console.log(chalk_1.default.cyan('║ AeroCorp Login ║'));
console.log(chalk_1.default.cyan('╚══════════════════╝'));
// Check for root API token in environment
const rootToken = process.env.AEROCORP_CLI_ROOT_API_TOKEN;
if (rootToken) {
console.log(chalk_1.default.yellow('🔑 Root API token detected in environment'));
return await this.authenticateWithRootToken(rootToken);
}
// Get URL and token from options or prompt
let coolifyUrl = options.url || this.configService.get('coolify_url');
let apiToken = options.token;
if (!coolifyUrl) {
const urlAnswer = await inquirer_1.default.prompt([
{
type: 'input',
name: 'url',
message: 'Enter your Coolify instance URL:',
default: 'https://coolify.aerocorpindustries.org',
validate: (input) => {
if (!input.startsWith('http')) {
return 'Please enter a valid URL starting with http:// or https://';
}
return true;
}
}
]);
coolifyUrl = urlAnswer.url;
}
if (!apiToken) {
const tokenAnswer = await inquirer_1.default.prompt([
{
type: 'password',
name: 'token',
message: 'Enter your API token:',
mask: '*'
}
]);
apiToken = tokenAnswer.token;
}
const spinner = (0, ora_1.default)('Authenticating...').start();
try {
// Test the API token
const response = await axios_1.default.get(`${coolifyUrl}/api/v1/projects`, {
headers: {
'Authorization': `Bearer ${apiToken}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
timeout: 10000
});
if (response.status === 200) {
spinner.succeed('Authentication successful');
// Save configuration
this.configService.set('coolify_url', coolifyUrl);
this.configService.set('api_token', apiToken);
this.configService.set('authenticated', true);
this.configService.set('server_ip', '128.140.35.238');
this.configService.set('environment', 'production');
console.log(chalk_1.default.green('✓ Logged in successfully'));
console.log(chalk_1.default.blue(`📡 Connected to: ${coolifyUrl}`));
console.log(chalk_1.default.blue(`🏠 Server IP: 128.140.35.238`));
// Show available projects
if (response.data && Array.isArray(response.data)) {
console.log(chalk_1.default.green(`\n📋 Available projects: ${response.data.length}`));
response.data.forEach((project, index) => {
console.log(chalk_1.default.gray(` ${index + 1}. ${project.name} (${project.uuid})`));
});
}
return true;
}
}
catch (error) {
spinner.fail('Authentication failed');
if (error.response) {
const status = error.response.status;
const message = error.response.data?.message || error.response.statusText;
switch (status) {
case 401:
throw new Error('Invalid API token. Please check your token and try again.');
case 403:
throw new Error('Access forbidden. Your token may not have sufficient permissions.');
case 404:
throw new Error('API endpoint not found. Please check your Coolify URL.');
case 500:
throw new Error('Server error. Please try again later.');
default:
throw new Error(`API Error: ${message} (Status: ${status})`);
}
}
else if (error.code === 'ECONNREFUSED') {
throw new Error('Connection refused. Please check your Coolify URL and network connection.');
}
else if (error.code === 'ENOTFOUND') {
throw new Error('Host not found. Please check your Coolify URL.');
}
else if (error.code === 'ETIMEDOUT') {
throw new Error('Connection timeout. Please check your network connection.');
}
else {
throw new Error(`Network error: ${error.message}`);
}
}
}
async authenticateWithRootToken(rootToken) {
const spinner = (0, ora_1.default)('Authenticating with root token...').start();
try {
const coolifyUrl = 'https://coolify.aerocorpindustries.org';
const response = await axios_1.default.get(`${coolifyUrl}/api/v1/projects`, {
headers: {
'Authorization': `Bearer ${rootToken}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
timeout: 10000
});
if (response.status === 200) {
spinner.succeed('Root authentication successful');
// Save configuration
this.configService.set('coolify_url', coolifyUrl);
this.configService.set('api_token', rootToken);
this.configService.set('authenticated', true);
this.configService.set('server_ip', '128.140.35.238');
this.configService.set('environment', 'production');
this.configService.set('root_access', true);
console.log(chalk_1.default.green('✓ Root access granted'));
console.log(chalk_1.default.blue(`📡 Connected to: ${coolifyUrl}`));
console.log(chalk_1.default.blue(`🏠 Server IP: 128.140.35.238`));
console.log(chalk_1.default.red('🔑 ROOT ACCESS ENABLED'));
return true;
}
}
catch (error) {
spinner.fail('Root authentication failed');
throw error;
}
}
isAuthenticated() {
const token = this.configService.get('api_token') || process.env.AEROCORP_CLI_ROOT_API_TOKEN;
const authenticated = this.configService.get('authenticated');
return !!(token && authenticated);
}
getAuthHeaders() {
const token = this.configService.get('api_token') || process.env.AEROCORP_CLI_ROOT_API_TOKEN;
if (!token) {
throw new Error('Not authenticated. Run "aerocorp login" first.');
}
return {
'Authorization': `Bearer ${token}`,
'Accept': 'application/json',
'Content-Type': 'application/json'
};
}
getCoolifyUrl() {
return this.configService.get('coolify_url') || 'https://coolify.aerocorpindustries.org';
}
logout() {
this.configService.clear();
console.log(chalk_1.default.green('✓ Logged out successfully'));
}
}
exports.AuthService = AuthService;
//# sourceMappingURL=auth.js.map