@@ -13,31 +13,35 @@ import { headers } from 'next/headers';
1313export async function getRedirectUri ( ) : Promise < string > {
1414 try {
1515 const headersList = await headers ( ) ;
16- const host = headersList . get ( 'host' ) ;
16+ const host = headersList . get ( 'x-forwarded-host' ) || headersList . get ( ' host') ;
1717 if ( host ) {
18- const protocol = host . startsWith ( 'localhost' ) || host . startsWith ( '127.0.0.1' ) ? 'http' : 'https' ;
19- return `${ protocol } ://${ host } /api/skyfi/callback` ;
18+ const proto = headersList . get ( 'x-forwarded-proto' ) || ( host . startsWith ( 'localhost' ) || host . startsWith ( '127.0.0.1' ) ? 'http' : 'https' ) ;
19+ return `${ proto } ://${ host } /api/skyfi/callback` ;
2020 }
2121 } catch ( e ) {
22- console . warn ( '[Skyfi] Failed to get host from headers, falling back to ENV :' , e ) ;
22+ console . warn ( '[Skyfi] Failed to get host from headers, falling back to NEXT_PUBLIC_APP_URL :' , e ) ;
2323 }
24- const baseUrl = process . env . NEXT_PUBLIC_APP_URL || 'http://localhost:3000' ;
25- return `${ baseUrl } /api/skyfi/callback` ;
24+ if ( process . env . NEXT_PUBLIC_APP_URL ) {
25+ const baseUrl = process . env . NEXT_PUBLIC_APP_URL . replace ( / \/ $ / , '' ) ;
26+ return `${ baseUrl } /api/skyfi/callback` ;
27+ }
28+ return 'http://localhost:3000/api/skyfi/callback' ;
2629}
2730
2831/**
2932 * Ensures the client is registered dynamically with the SkyFi MCP server.
3033 * Uses an AbortController with a 10s timeout to prevent hanging.
3134 */
3235async function ensureClientRegistered ( provider : SkyfiOAuthProvider , forceRegister : boolean = false ) : Promise < string > {
36+ const currentUri = provider . redirectUrl ?. toString ( ) ;
3337 if ( ! forceRegister ) {
3438 const currentInfo = await provider . clientInformation ( ) ;
35- if ( currentInfo ?. client_id ) {
39+ if ( currentInfo ?. client_id && currentInfo ?. redirect_uri === currentUri ) {
3640 return currentInfo . client_id ;
3741 }
3842 }
3943
40- console . log ( '[SkyFiAction] Client not registered or force-register active . Registering dynamically...' ) ;
44+ console . log ( '[SkyFiAction] Client not registered or redirect URI changed . Registering dynamically...' ) ;
4145
4246 const controller = new AbortController ( ) ;
4347 const timeoutId = setTimeout ( ( ) => controller . abort ( ) , 10000 ) ;
@@ -91,7 +95,7 @@ export async function startSkyfiConnection(): Promise<{ url?: string; error?: st
9195 const redirectUri = await getRedirectUri ( ) ;
9296 const provider = new SkyfiOAuthProvider ( userId , redirectUri ) ;
9397
94- const clientId = await ensureClientRegistered ( provider , true ) ;
98+ const clientId = await ensureClientRegistered ( provider , false ) ;
9599
96100 const verifier = generateCodeVerifier ( ) ;
97101 const challenge = generateCodeChallenge ( verifier ) ;
@@ -144,7 +148,7 @@ export async function getSkyfiConnectionStatus(): Promise<{ connected: boolean;
144148 method : 'POST' ,
145149 headers : {
146150 'Content-Type' : 'application/json' ,
147- 'X-Skyfi-Api-Key ' : tokens . access_token ,
151+ 'Authorization ' : `Bearer ${ tokens . access_token } ` ,
148152 } ,
149153 body : JSON . stringify ( {
150154 jsonrpc : '2.0' ,
0 commit comments