@@ -16,7 +16,21 @@ export default async function handler(
1616 // The authorization code returned by AuthKit
1717 const code = req . query . code as string ;
1818 const state = req . query . state as string ; // This contains our returnTo URL
19- const returnTo = state && ! state . startsWith ( "/api" ) ? state : "/" ;
19+
20+ const { resource_id, path, url } = req . query ;
21+
22+ let returnTo = state && ! state . startsWith ( "/api" ) ? state : "/" ;
23+
24+ // url is a query parameter that is only set by the Vercel auth flow
25+ // if it is set, and looks like a redirect to the device-auth flow,
26+ // we redirect to the device-auth flow.
27+ if ( typeof url === "string" && url . startsWith ( "https://auth.convex.dev" ) ) {
28+ returnTo = url ;
29+ } else if ( typeof path === "string" || typeof resource_id === "string" ) {
30+ const key = typeof path === "string" ? "vercelPath" : "projectId" ;
31+ const value = typeof path === "string" ? path : resource_id ;
32+ returnTo = addQueryParam ( returnTo , key , value as string ) ;
33+ }
2034
2135 if ( ! code ) {
2236 return res . status ( 400 ) . send ( "No code provided" ) ;
@@ -70,3 +84,8 @@ export default async function handler(
7084 // );
7185 }
7286}
87+
88+ function addQueryParam ( url : string , key : string , value : string ) {
89+ const symbol = url . includes ( "?" ) ? "&" : "?" ;
90+ return `${ url } ${ symbol } ${ key } =${ value } ` ;
91+ }
0 commit comments