|
1 | 1 | import { Command } from "commander"; |
2 | | -import { tasks } from "@clack/prompts"; |
3 | | -import { writeAuth } from "../../../core/config/auth.js"; |
4 | | -import { runCommand } from "../../utils/index.js"; |
| 2 | +import { log } from "@clack/prompts"; |
| 3 | +import pWaitFor from "p-wait-for"; |
| 4 | +import { writeAuth } from "@config/auth.js"; |
| 5 | +import { |
| 6 | + generateDeviceCode, |
| 7 | + getTokenFromDeviceCode, |
| 8 | + type DeviceCodeResponse, |
| 9 | + type TokenResponse, |
| 10 | +} from "@api/auth"; |
| 11 | +import { runCommand, runTask } from "../../utils/index.js"; |
5 | 12 |
|
6 | | -async function login(): Promise<void> { |
7 | | - await tasks([ |
| 13 | +async function generateAndDisplayDeviceCode(): Promise<DeviceCodeResponse> { |
| 14 | + const deviceCodeResponse = await runTask( |
| 15 | + "Generating device code...", |
| 16 | + async () => { |
| 17 | + return await generateDeviceCode(); |
| 18 | + }, |
8 | 19 | { |
9 | | - title: "Logging you in", |
10 | | - task: async () => { |
11 | | - await writeAuth({ |
12 | | - token: "stub-token-12345", |
13 | | - email: "valid@email.com", |
14 | | - name: "KfirStri", |
15 | | - }); |
16 | | - |
17 | | - return "Logged in as KfirStri"; |
| 20 | + successMessage: "Device code generated", |
| 21 | + errorMessage: "Failed to generate device code", |
| 22 | + } |
| 23 | + ); |
| 24 | + |
| 25 | + log.info( |
| 26 | + `Please visit: ${deviceCodeResponse.verificationUrl}\n` + |
| 27 | + `Enter your device code: ${deviceCodeResponse.userCode}` |
| 28 | + ); |
| 29 | + |
| 30 | + return deviceCodeResponse; |
| 31 | +} |
| 32 | + |
| 33 | +async function waitForAuthentication( |
| 34 | + deviceCode: string, |
| 35 | + expiresIn: number |
| 36 | +): Promise<TokenResponse> { |
| 37 | + let tokenResponse: TokenResponse | null = null; |
| 38 | + |
| 39 | + try { |
| 40 | + await runTask( |
| 41 | + "Waiting for you to complete authentication...", |
| 42 | + async () => { |
| 43 | + await pWaitFor( |
| 44 | + async () => { |
| 45 | + const result = await getTokenFromDeviceCode(deviceCode); |
| 46 | + if (result !== null) { |
| 47 | + tokenResponse = result; |
| 48 | + return true; |
| 49 | + } |
| 50 | + return false; |
| 51 | + }, |
| 52 | + { |
| 53 | + interval: 2000, |
| 54 | + timeout: expiresIn * 1000, |
| 55 | + } |
| 56 | + ); |
18 | 57 | }, |
19 | | - }, |
20 | | - ]); |
| 58 | + { |
| 59 | + successMessage: "Authentication completed!", |
| 60 | + errorMessage: "Authentication failed", |
| 61 | + } |
| 62 | + ); |
| 63 | + } catch (error) { |
| 64 | + if (error instanceof Error && error.message.includes("timed out")) { |
| 65 | + throw new Error("Authentication timed out. Please try again."); |
| 66 | + } |
| 67 | + throw error; |
| 68 | + } |
| 69 | + |
| 70 | + if (!tokenResponse) { |
| 71 | + throw new Error("Failed to retrieve authentication token."); |
| 72 | + } |
| 73 | + |
| 74 | + return tokenResponse; |
| 75 | +} |
| 76 | + |
| 77 | +async function saveAuthData(token: TokenResponse): Promise<void> { |
| 78 | + await writeAuth({ |
| 79 | + token: token.token, |
| 80 | + email: token.email, |
| 81 | + name: token.name, |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +async function login(): Promise<void> { |
| 86 | + const deviceCodeResponse = await generateAndDisplayDeviceCode(); |
| 87 | + |
| 88 | + const token = await waitForAuthentication( |
| 89 | + deviceCodeResponse.deviceCode, |
| 90 | + deviceCodeResponse.expiresIn |
| 91 | + ); |
| 92 | + |
| 93 | + await saveAuthData(token); |
| 94 | + |
| 95 | + log.success(`Logged in as ${token.name}`); |
21 | 96 | } |
22 | 97 |
|
23 | 98 | export const loginCommand = new Command("login") |
24 | 99 | .description("Authenticate with Base44") |
25 | 100 | .action(async () => { |
26 | 101 | await runCommand(login); |
27 | 102 | }); |
28 | | - |
|
0 commit comments