| 1 | import chalk from "chalk"; |
| 2 | |
| 3 | import { isAuthenticated, login } from "./workos.js"; |
| 4 | |
| 5 | /** |
| 6 | * Ensures the user is authenticated before proceeding |
| 7 | * Returns true if authentication is successful, false otherwise |
| 8 | */ |
| 9 | export async function ensureAuthenticated( |
| 10 | requireAuth: boolean = true, |
| 11 | ): Promise<boolean> { |
| 12 | if (isAuthenticated()) { |
| 13 | return true; |
| 14 | } |
| 15 | |
| 16 | if (!requireAuth) { |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | console.info(chalk.yellow("Authentication required.")); |
| 21 | |
| 22 | try { |
| 23 | await login(); |
| 24 | return true; |
| 25 | } catch (error) { |
| 26 | console.error(chalk.red("Failed to authenticate."), error); |
| 27 | return false; |
| 28 | } |
| 29 | } |