Skip to content

Commit 4f7ebe3

Browse files
atrakhConvex, Inc.
authored andcommitted
cli: support --vercel login flow (#40883)
GitOrigin-RevId: 1d14624128207374823507f130451093061ba16a
1 parent f95a0bf commit 4f7ebe3

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

npm-packages/convex/src/cli/lib/login.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ async function performDeviceAuthorization(
8888
ctx: Context,
8989
authClient: BaseClient,
9090
shouldOpen: boolean,
91+
vercel?: boolean,
92+
vercelOverride?: string,
9193
): Promise<string> {
9294
// Device authorization flow follows this guide: https://github.com/auth0/auth0-device-flow-cli-sample/blob/9f0f3b76a6cd56ea8d99e76769187ea5102d519d/cli.js
9395
// License: MIT License
@@ -132,8 +134,14 @@ async function performDeviceAuthorization(
132134
// Device Authorization Response - https://tools.ietf.org/html/rfc8628#section-3.2
133135
// Open authentication URL
134136
const { verification_uri_complete, user_code, expires_in } = handle;
137+
138+
// Construct Vercel URL if --vercel flag is used
139+
const urlToOpen = vercel
140+
? `https://vercel.com/sso/integrations/${vercelOverride || "convex"}?url=${verification_uri_complete}`
141+
: verification_uri_complete;
142+
135143
logMessage(
136-
`Visit ${verification_uri_complete} to finish logging in.\n` +
144+
`Visit ${urlToOpen} to finish logging in.\n` +
137145
`You should see the following code which expires in ${
138146
expires_in % 60 === 0
139147
? `${expires_in / 60} minutes`
@@ -148,25 +156,19 @@ async function performDeviceAuthorization(
148156
}
149157

150158
if (shouldOpen) {
151-
showSpinner(
152-
`Opening ${verification_uri_complete} in your browser to log in...\n`,
153-
);
159+
showSpinner(`Opening ${urlToOpen} in your browser to log in...\n`);
154160
try {
155-
const p = await open(verification_uri_complete);
161+
const p = await open(urlToOpen);
156162
p.once("error", () => {
157-
changeSpinner(
158-
`Manually open ${verification_uri_complete} in your browser to log in.`,
159-
);
163+
changeSpinner(`Manually open ${urlToOpen} in your browser to log in.`);
160164
});
161165
changeSpinner("Waiting for the confirmation...");
162166
} catch {
163167
logError(chalk.red(`Unable to open browser.`));
164-
changeSpinner(
165-
`Manually open ${verification_uri_complete} in your browser to log in.`,
166-
);
168+
changeSpinner(`Manually open ${urlToOpen} in your browser to log in.`);
167169
}
168170
} else {
169-
showSpinner(`Open ${verification_uri_complete} in your browser to log in.`);
171+
showSpinner(`Open ${urlToOpen} in your browser to log in.`);
170172
}
171173

172174
// Device Access Token Request - https://tools.ietf.org/html/rfc8628#section-3.4
@@ -280,6 +282,8 @@ export async function performLogin(
280282
dumpAccessToken,
281283
deviceName: deviceNameOverride,
282284
anonymousId,
285+
vercel,
286+
vercelOverride,
283287
}: {
284288
overrideAuthUrl?: string;
285289
overrideAuthClient?: string;
@@ -294,6 +298,8 @@ export async function performLogin(
294298
dumpAccessToken?: boolean;
295299
deviceName?: string;
296300
anonymousId?: string;
301+
vercel?: boolean;
302+
vercelOverride?: string;
297303
} = {},
298304
) {
299305
loginFlow = loginFlow || "auto";
@@ -366,6 +372,8 @@ export async function performLogin(
366372
ctx,
367373
authClient,
368374
open ?? true,
375+
vercel,
376+
vercelOverride,
369377
);
370378
}
371379
}

npm-packages/convex/src/cli/login.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ export const login = new Command("login")
107107
.addOption(new Option("--dump-access-token").hideHelp())
108108
// Hidden option for tests to check if the user is logged in.
109109
.addOption(new Option("--check-login").hideHelp())
110+
// Redirect to Vercel SSO integration URL
111+
.addOption(
112+
new Option(
113+
"--vercel",
114+
"Redirect to Vercel SSO integration for login",
115+
).hideHelp(),
116+
)
117+
// Override the Vercel URL slug (defaults to 'convex')
118+
.addOption(new Option("--vercel-override <slug>").hideHelp())
110119
.addCommand(loginStatus)
111120
.addHelpCommand(false)
112121
.action(async (options, cmd: Command) => {
@@ -148,6 +157,8 @@ export const login = new Command("login")
148157
await performLogin(ctx, {
149158
...options,
150159
anonymousId: uuid,
160+
vercel: options.vercel,
161+
vercelOverride: options.vercelOverride,
151162
});
152163

153164
await handleLinkingDeployments(ctx, {

0 commit comments

Comments
 (0)