UNPKG

anomaly-cli

Version:

A command-line interface tool for anomaly detection and management

189 lines (188 loc) • 7.6 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { auth } from "../firebase/clientApp.js"; import { signInWithEmailAndPassword, createUserWithEmailAndPassword, onAuthStateChanged, signOut, } from "firebase/auth"; import prompts from "prompts"; import chalk from "chalk"; import ora from "ora"; export class AuthManager { constructor() { this.currentUser = null; // Listen for auth state changes onAuthStateChanged(auth, (user) => { this.currentUser = user; }); } getCurrentUser() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => { const unsubscribe = onAuthStateChanged(auth, (user) => { unsubscribe(); if (user) { resolve({ uid: user.uid, email: user.email, displayName: user.displayName, user: user, }); } else { resolve(null); } }); }); }); } signIn(email, password) { return __awaiter(this, void 0, void 0, function* () { const spinner = ora("Signing in...").start(); try { const userCredential = yield signInWithEmailAndPassword(auth, email, password); spinner.succeed(chalk.green("Successfully signed in!")); return { uid: userCredential.user.uid, email: userCredential.user.email, displayName: userCredential.user.displayName, user: userCredential.user, }; } catch (error) { spinner.fail(chalk.red("Sign in failed")); throw new Error(this.getAuthErrorMessage(error.code)); } }); } signUp(email, password) { return __awaiter(this, void 0, void 0, function* () { const spinner = ora("Creating account...").start(); try { const userCredential = yield createUserWithEmailAndPassword(auth, email, password); spinner.succeed(chalk.green("Account created successfully!")); return { uid: userCredential.user.uid, email: userCredential.user.email, displayName: userCredential.user.displayName, user: userCredential.user, }; } catch (error) { spinner.fail(chalk.red("Account creation failed")); throw new Error(this.getAuthErrorMessage(error.code)); } }); } signOut() { return __awaiter(this, void 0, void 0, function* () { const spinner = ora("Signing out...").start(); try { yield signOut(auth); spinner.succeed(chalk.green("Successfully signed out!")); } catch (error) { spinner.fail(chalk.red("Sign out failed")); throw error; } }); } getAuthErrorMessage(errorCode) { switch (errorCode) { case "auth/user-not-found": return "No account found with this email address."; case "auth/wrong-password": return "Incorrect password."; case "auth/email-already-in-use": return "An account with this email already exists."; case "auth/weak-password": return "Password should be at least 6 characters."; case "auth/invalid-email": return "Invalid email address."; case "auth/too-many-requests": return "Too many failed attempts. Please try again later."; default: return "Authentication failed. Please try again."; } } promptAuthentication() { return __awaiter(this, void 0, void 0, function* () { console.log(chalk.blue.bold("\nšŸ” Authentication Required\n")); const { action } = yield prompts({ type: "select", name: "action", message: "Choose an option:", choices: [ { title: "šŸ”‘ Sign In", value: "signin" }, { title: "šŸ“ Create Account", value: "signup" }, { title: "āŒ Exit", value: "exit" }, ], initial: 0, }); if (action === "exit") { console.log(chalk.yellow("\nšŸ‘‹ Goodbye!")); process.exit(0); } const credentials = yield this.promptCredentials(); try { if (action === "signin") { return yield this.signIn(credentials.email, credentials.password); } else { return yield this.signUp(credentials.email, credentials.password); } } catch (error) { console.log(chalk.red(`\nāŒ ${error.message}\n`)); const { retry } = yield prompts({ type: "confirm", name: "retry", message: "Would you like to try again?", initial: true, }); if (retry) { return this.promptAuthentication(); } else { console.log(chalk.yellow("\nšŸ‘‹ Goodbye!")); process.exit(0); } } }); } promptCredentials() { return __awaiter(this, void 0, void 0, function* () { const { email } = yield prompts({ type: "text", name: "email", message: "Email address:", validate: (value) => { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; return emailRegex.test(value) || "Please enter a valid email address"; }, }); if (!email) { console.log(chalk.yellow("\nšŸ‘‹ Goodbye!")); process.exit(0); } const { password } = yield prompts({ type: "password", name: "password", message: "Password:", validate: (value) => { return value.length >= 6 || "Password must be at least 6 characters"; }, }); if (!password) { console.log(chalk.yellow("\nšŸ‘‹ Goodbye!")); process.exit(0); } return { email, password }; }); } } export const authManager = new AuthManager();