(apiUrl: string)
| 40 | }); |
| 41 | |
| 42 | export async function login(apiUrl: string) { |
| 43 | await readline |
| 44 | .createInterface({ |
| 45 | input: process.stdin, |
| 46 | output: process.stdout, |
| 47 | }) |
| 48 | .question( |
| 49 | ` |
| 50 | Press Enter to open the browser for authentication. |
| 51 | |
| 52 | --- |
| 53 | |
| 54 | Having issues? Put LINGO_API_KEY in your .env file instead. |
| 55 | `.trim() + "\n", |
| 56 | ); |
| 57 | |
| 58 | const verifier = randomBytes(32).toString("base64url"); |
| 59 | |
| 60 | const createSpinner = Ora().start("Starting authentication session"); |
| 61 | const session = await createCliSession(apiUrl, verifier); |
| 62 | createSpinner.succeed("Authentication session started"); |
| 63 | |
| 64 | await open(session.verifyUrl, { wait: false }); |
| 65 | |
| 66 | const pollSpinner = Ora().start("Waiting for browser confirmation"); |
| 67 | try { |
| 68 | const apiKey = await pollForApiKey(apiUrl, session.id, verifier); |
| 69 | pollSpinner.succeed("API key received"); |
| 70 | return apiKey; |
| 71 | } catch (error) { |
| 72 | pollSpinner.fail(); |
| 73 | throw error; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | type CreateSessionResponse = { |
| 78 | id: string; |
no test coverage detected