|
| 1 | +import open from 'open' |
| 2 | +import { cyan, green, red, yellow, bold } from 'picocolors' |
| 3 | + |
| 4 | +import { WEBSITE_URL } from './constants' |
| 5 | +import { generateLoginUrl, pollLoginStatus } from './login-flow' |
| 6 | +import { generateFingerprintId } from './utils' |
| 7 | +import { saveUserCredentials } from '../utils/auth' |
| 8 | +import { logger } from '../utils/logger' |
| 9 | + |
| 10 | +import type { User } from '../utils/auth' |
| 11 | + |
| 12 | +/** |
| 13 | + * Plain-text login flow that runs outside the TUI. |
| 14 | + * Prints the login URL as plain text so the user can select and copy it |
| 15 | + * using normal terminal text selection (Cmd+C / Ctrl+Shift+C). |
| 16 | + * |
| 17 | + * This is the escape hatch for remote/SSH environments where the TUI's |
| 18 | + * clipboard and browser integration don't work. |
| 19 | + */ |
| 20 | +export async function runPlainLogin(): Promise<void> { |
| 21 | + const fingerprintId = generateFingerprintId() |
| 22 | + |
| 23 | + console.log() |
| 24 | + console.log(bold('Codebuff Login')) |
| 25 | + console.log() |
| 26 | + console.log('Generating login URL...') |
| 27 | + |
| 28 | + let loginData |
| 29 | + try { |
| 30 | + loginData = await generateLoginUrl( |
| 31 | + { logger }, |
| 32 | + { baseUrl: WEBSITE_URL, fingerprintId }, |
| 33 | + ) |
| 34 | + } catch (error) { |
| 35 | + console.error( |
| 36 | + red( |
| 37 | + `Failed to generate login URL: ${ |
| 38 | + error instanceof Error ? error.message : String(error) |
| 39 | + }`, |
| 40 | + ), |
| 41 | + ) |
| 42 | + process.exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + console.log() |
| 46 | + console.log('Open this URL in your browser to log in:') |
| 47 | + console.log() |
| 48 | + console.log(cyan(loginData.loginUrl)) |
| 49 | + console.log() |
| 50 | + |
| 51 | + // Try to open browser, silently ignore failure (expected on remote servers) |
| 52 | + try { |
| 53 | + await open(loginData.loginUrl) |
| 54 | + console.log(green('Browser opened. Waiting for login...')) |
| 55 | + } catch { |
| 56 | + console.log(yellow('Could not open browser — please open the URL above manually.')) |
| 57 | + } |
| 58 | + |
| 59 | + console.log() |
| 60 | + console.log('Waiting for login...') |
| 61 | + |
| 62 | + const sleep = (ms: number) => |
| 63 | + new Promise<void>((resolve) => { |
| 64 | + setTimeout(resolve, ms) |
| 65 | + }) |
| 66 | + |
| 67 | + const result = await pollLoginStatus( |
| 68 | + { sleep, logger }, |
| 69 | + { |
| 70 | + baseUrl: WEBSITE_URL, |
| 71 | + fingerprintId, |
| 72 | + fingerprintHash: loginData.fingerprintHash, |
| 73 | + expiresAt: loginData.expiresAt, |
| 74 | + }, |
| 75 | + ) |
| 76 | + |
| 77 | + if (result.status === 'success') { |
| 78 | + const user = result.user as User |
| 79 | + saveUserCredentials(user) |
| 80 | + console.log() |
| 81 | + console.log(green(`✓ Logged in as ${user.name} (${user.email})`)) |
| 82 | + console.log() |
| 83 | + console.log('You can now run ' + cyan('codebuff') + ' to start.') |
| 84 | + process.exit(0) |
| 85 | + } else if (result.status === 'timeout') { |
| 86 | + console.error(red('Login timed out. Please try again.')) |
| 87 | + process.exit(1) |
| 88 | + } else { |
| 89 | + console.error(red('Login was aborted.')) |
| 90 | + process.exit(1) |
| 91 | + } |
| 92 | +} |
0 commit comments