ssh-bridge-ai
Version:
One Command Magic SSH with Invisible Analytics - Connect to any server instantly with 'sshbridge user@server'. Zero setup, zero friction, pure magic. Industry-standard security with behind-the-scenes business intelligence.
241 lines (205 loc) • 6.54 kB
JavaScript
const inquirer = require('inquirer');
const chalk = require('chalk');
const { APIClient } = require('./api');
const { Config } = require('./config');
class AuthManager {
constructor() {
this.api = new APIClient();
this.config = new Config();
}
/**
* Handle complete user authentication flow
* Supports both new user registration and existing user login
*/
async authenticateUser() {
try {
// Check if user already has a valid session
const existingSession = await this.config.getUserSession();
if (existingSession) {
console.log(chalk.green(`Welcome back! You're on the ${existingSession.tier} tier`));
return existingSession;
}
// No session - start authentication flow
const { email } = await inquirer.prompt([
{
type: 'input',
name: 'email',
message: 'Email:',
validate: (input) => input.includes('@') || 'Invalid email'
}
]);
// Check if user exists and send verification code
const userExists = await this.checkUserExists(email);
if (userExists) {
return await this.loginExistingUser(email);
} else {
return await this.registerNewUser(email);
}
} catch (error) {
console.error(chalk.red(`Authentication failed: ${error.message}`));
throw error;
}
}
/**
* Check if user already exists in the system
*/
async checkUserExists(email) {
try {
const result = await this.api.checkUserExists(email);
return result.exists;
} catch (error) {
// If API call fails, assume user doesn't exist
return false;
}
}
/**
* Handle login for existing users
*/
async loginExistingUser(email) {
console.log(chalk.blue('Sending verification code...'));
try {
// Send verification code
const result = await this.api.sendVerificationCode(email);
console.log(chalk.blue('Verification code sent to your email'));
// For development: show the code (remove in production)
if (result && result.code) {
console.log(chalk.yellow(`🔑 Development Code: ${result.code}`));
}
// Get verification code from user
const { code } = await inquirer.prompt([
{
type: 'input',
name: 'code',
message: 'Verification code:',
validate: (input) => input.length === 6 || 'Code must be 6 digits'
}
]);
// Verify code and get user data
const user = await this.api.verifyEmail(email, code);
// Save session
await this.config.saveUserSession(user);
console.log(chalk.green(`Welcome back! You're on the ${user.tier} tier`));
return user;
} catch (error) {
console.error(chalk.red(`Login failed: ${error.message}`));
throw error;
}
}
/**
* Handle registration for new users
*/
async registerNewUser(email) {
console.log(chalk.blue('Creating new account...'));
try {
// Get user details
const { name } = await inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Full name:',
validate: (input) => input.length > 0 || 'Name is required'
}
]);
// Send verification code
const result = await this.api.sendVerificationCode(email);
console.log(chalk.blue('Verification code sent to your email'));
// For development: show the code (remove in production)
if (result && result.code) {
console.log(chalk.yellow(`🔑 Development Code: ${result.code}`));
}
// Get verification code from user
const { code } = await inquirer.prompt([
{
type: 'input',
name: 'code',
message: 'Verification code:',
validate: (input) => input.length === 6 || 'Code must be 6 digits'
}
]);
// Create account and verify
const user = await this.api.createAccount({ email, name, code });
// Save session
await this.config.saveUserSession(user);
console.log(chalk.green(`Account created! You're on the ${user.tier} tier`));
return user;
} catch (error) {
console.error(chalk.red(`Registration failed: ${error.message}`));
throw error;
}
}
/**
* Check if user is currently authenticated
*/
async isAuthenticated() {
try {
const session = await this.config.getUserSession();
return session !== null;
} catch (error) {
return false;
}
}
/**
* Get current user session
*/
async getCurrentUser() {
try {
return await this.config.getUserSession();
} catch (error) {
return null;
}
}
/**
* Logout user (clear session)
*/
async logout() {
try {
await this.config.clearUserSession();
console.log(chalk.green('Logged out successfully'));
} catch (error) {
console.error(chalk.red(`Logout failed: ${error.message}`));
}
}
/**
* Get user tier information
*/
async getUserTier() {
try {
const user = await this.getCurrentUser();
if (!user) {
return { tier: 'free', serversUsed: 0, commandsUsed: 0, serversLimit: 5, commandsLimit: 50 };
}
const usage = await this.api.getUserStatus(user.email);
return {
tier: usage.plan || 'free',
serversUsed: usage.servers || 0,
commandsUsed: usage.used || 0,
serversLimit: usage.maxServers || (usage.plan === 'pro' ? 999999 : 5),
commandsLimit: usage.limit || (usage.plan === 'pro' ? 999999 : 50)
};
} catch (error) {
// Return free tier defaults if API call fails
return { tier: 'free', serversUsed: 0, commandsUsed: 0, serversLimit: 5, commandsLimit: 50 };
}
}
/**
* Upgrade user account
*/
async upgradeAccount(plan) {
try {
const user = await this.getCurrentUser();
if (!user) {
throw new Error('You must be logged in to upgrade');
}
const result = await this.api.upgradeAccount(plan);
// Update local session
user.tier = plan;
await this.config.saveUserSession(user);
console.log(chalk.green(`Account upgraded to ${plan} tier!`));
return result;
} catch (error) {
console.error(chalk.red(`Upgrade failed: ${error.message}`));
throw error;
}
}
}
module.exports = { AuthManager };