|
1 | | -import { FernUserToken, storeToken } from "@fern-api/auth"; |
| 1 | +import { FernUserToken, isLoggedIn, storeToken } from "@fern-api/auth"; |
2 | 2 | import { getPosthogManager } from "@fern-api/posthog-manager"; |
3 | 3 | import { TaskContext } from "@fern-api/task-context"; |
4 | 4 | import chalk from "chalk"; |
5 | | - |
| 5 | +import inquirer from "inquirer"; |
6 | 6 | import { doAuth0DeviceAuthorizationFlow } from "./auth0-login/doAuth0DeviceAuthorizationFlow.js"; |
7 | 7 | import { type Auth0TokenResponse, doAuth0LoginFlow } from "./auth0-login/doAuth0LoginFlow.js"; |
8 | 8 | import { resolveSsoConnection } from "./auth0-login/resolveSsoConnection.js"; |
9 | | -import { AUTH0_CLIENT_ID, AUTH0_DOMAIN, getDashboardBaseUrl, VENUS_AUDIENCE } from "./constants.js"; |
| 9 | +import { AUTH0_CLIENT_ID, AUTH0_DOMAIN, getDashboardBaseUrl, LOGIN_OPTIONS, VENUS_AUDIENCE } from "./constants.js"; |
10 | 10 |
|
11 | 11 | export type { Auth0TokenResponse } from "./auth0-login/doAuth0LoginFlow.js"; |
12 | 12 |
|
@@ -63,20 +63,97 @@ export async function getTokenFromAuth0( |
63 | 63 | }); |
64 | 64 | } |
65 | 65 |
|
| 66 | + // In TTY mode, check for a locally stored token to decide the flow: |
| 67 | + // - If already logged in → open the browser directly (Auth0 session handles it) |
| 68 | + // - If not logged in → show an interactive prompt so the user stays in the CLI |
| 69 | + if (!forceReauth && process.stdout.isTTY && !(await isLoggedIn())) { |
| 70 | + return await promptAndLogin(context); |
| 71 | + } |
| 72 | + |
| 73 | + return await loginWithDeviceCodeFallback(context, { forceReauth }); |
| 74 | +} |
| 75 | + |
| 76 | +async function loginWithDeviceCodeFallback( |
| 77 | + context: TaskContext, |
| 78 | + { forceReauth = false, connection }: { forceReauth?: boolean; connection?: string } = {} |
| 79 | +): Promise<Auth0TokenResponse> { |
66 | 80 | try { |
67 | 81 | return await doAuth0LoginFlow({ |
68 | 82 | context, |
69 | 83 | auth0Domain: AUTH0_DOMAIN, |
70 | 84 | auth0ClientId: AUTH0_CLIENT_ID, |
71 | 85 | audience: VENUS_AUDIENCE, |
72 | | - forceReauth |
| 86 | + forceReauth, |
| 87 | + connection |
73 | 88 | }); |
74 | 89 | } catch { |
75 | 90 | return await doAuth0DeviceAuthorizationFlow({ |
76 | 91 | auth0Domain: AUTH0_DOMAIN, |
77 | 92 | auth0ClientId: AUTH0_CLIENT_ID, |
78 | 93 | audience: VENUS_AUDIENCE, |
79 | | - context |
| 94 | + context, |
| 95 | + connection |
| 96 | + }); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +async function promptAndLogin(context: TaskContext): Promise<Auth0TokenResponse> { |
| 101 | + const choices = LOGIN_OPTIONS.map((opt) => ({ name: opt.label, value: opt.connection })); |
| 102 | + |
| 103 | + const selectedConnection = await promptForConnection(context, choices); |
| 104 | + |
| 105 | + // SSO requires resolving the enterprise connection from the user's email |
| 106 | + if (selectedConnection === "enterprise-sso") { |
| 107 | + const ssoEmail = await promptForEmail(context); |
| 108 | + |
| 109 | + const resolvedConnection = await resolveSsoConnection({ |
| 110 | + dashboardBaseUrl: getDashboardBaseUrl(), |
| 111 | + email: ssoEmail |
80 | 112 | }); |
| 113 | + |
| 114 | + return await loginWithDeviceCodeFallback(context, { connection: resolvedConnection }); |
81 | 115 | } |
| 116 | + |
| 117 | + return await loginWithDeviceCodeFallback(context, { connection: selectedConnection }); |
| 118 | +} |
| 119 | + |
| 120 | +async function promptForConnection( |
| 121 | + context: TaskContext, |
| 122 | + choices: Array<{ name: string; value: string }> |
| 123 | +): Promise<string> { |
| 124 | + let result = ""; |
| 125 | + await context.takeOverTerminal(async () => { |
| 126 | + const { connection } = await inquirer.prompt<{ connection: string }>([ |
| 127 | + { |
| 128 | + type: "list", |
| 129 | + name: "connection", |
| 130 | + message: "How would you like to log in?", |
| 131 | + choices |
| 132 | + } |
| 133 | + ]); |
| 134 | + result = connection; |
| 135 | + }); |
| 136 | + return result; |
| 137 | +} |
| 138 | + |
| 139 | +async function promptForEmail(context: TaskContext): Promise<string> { |
| 140 | + let result = ""; |
| 141 | + await context.takeOverTerminal(async () => { |
| 142 | + const { email } = await inquirer.prompt<{ email: string }>([ |
| 143 | + { |
| 144 | + type: "input", |
| 145 | + name: "email", |
| 146 | + message: "Enter your email address:", |
| 147 | + validate: (input: string) => { |
| 148 | + const trimmed = input.trim(); |
| 149 | + if (trimmed.length === 0 || !trimmed.includes("@")) { |
| 150 | + return "Please enter a valid email address."; |
| 151 | + } |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + ]); |
| 156 | + result = email.trim(); |
| 157 | + }); |
| 158 | + return result; |
82 | 159 | } |
0 commit comments