@@ -304,6 +304,9 @@ const handleValidationChange = (isValid: boolean, missingFields: string[]) => {
304304// OAuth popup reference
305305const oauthPopup = ref <Window | null >(null )
306306
307+ // BroadcastChannel for cross-tab OAuth communication (fallback when window.opener is null)
308+ let oauthBroadcastChannel: BroadcastChannel | null = null
309+
307310// Handle OAuth authorization
308311const handleOAuthAuthorization = async () => {
309312 try {
@@ -366,17 +369,9 @@ const handleOAuthAuthorization = async () => {
366369 }
367370}
368371
369- // Handle OAuth popup messages
370- const handleOAuthMessage = (event : MessageEvent ) => {
371- const backendUrl = new URL (import .meta .env .VITE_DEPLOYSTACK_BACKEND_URL || ' http://localhost:3000' )
372- const allowedOrigins = [window .location .origin , backendUrl .origin ]
373-
374- if (! allowedOrigins .includes (event .origin )) {
375- console .warn (' Rejected postMessage from unauthorized origin:' , event .origin )
376- return
377- }
378-
379- if (event .data .type === ' oauth_success' ) {
372+ // Process OAuth result data (shared between postMessage and BroadcastChannel)
373+ const processOAuthResult = (data : any ) => {
374+ if (data .type === ' oauth_success' ) {
380375 if (oauthPopup .value && ! oauthPopup .value .closed ) {
381376 oauthPopup .value .close ()
382377 }
@@ -387,23 +382,44 @@ const handleOAuthMessage = (event: MessageEvent) => {
387382 })
388383
389384 eventBus .emit (' mcp-installations-updated' )
390- router .push (' /mcp-server' )
391- }
392385
393- else if (event .data .type === ' oauth_error' ) {
394- const { error } = event .data
386+ const installationId = data .installation_id
387+ if (installationId ) {
388+ router .push (` /mcp-server/installation/${installationId }/general ` )
389+ } else {
390+ router .push (' /mcp-server' )
391+ }
392+ }
395393
394+ else if (data .type === ' oauth_error' ) {
396395 if (oauthPopup .value && ! oauthPopup .value .closed ) {
397396 oauthPopup .value .close ()
398397 }
399398 oauthPopup .value = null
400399
401400 toast .error (' Authentication failed' , {
402- description: error || ' OAuth authorization failed. Please try again.'
401+ description: data . error || ' OAuth authorization failed. Please try again.'
403402 })
404403 }
405404}
406405
406+ // Handle OAuth popup messages via postMessage
407+ const handleOAuthMessage = (event : MessageEvent ) => {
408+ if (! event .data ?.type || ! [' oauth_success' , ' oauth_error' , ' oauth_reauth_success' ].includes (event .data .type )) {
409+ return
410+ }
411+
412+ const backendUrl = new URL (import .meta .env .VITE_DEPLOYSTACK_BACKEND_URL || ' http://localhost:3000' )
413+ const allowedOrigins = [window .location .origin , backendUrl .origin ]
414+
415+ if (! allowedOrigins .includes (event .origin )) {
416+ console .warn (' Rejected postMessage from unauthorized origin:' , event .origin )
417+ return
418+ }
419+
420+ processOAuthResult (event .data )
421+ }
422+
407423// Watch for serverData changes and reinitialize form
408424watch (() => props .serverData , () => {
409425 initializeEnvironmentForm ()
@@ -423,11 +439,29 @@ onMounted(async () => {
423439 }
424440
425441 window .addEventListener (' message' , handleOAuthMessage )
442+
443+ // BroadcastChannel fallback for when window.opener is null (cross-origin redirect chain)
444+ try {
445+ oauthBroadcastChannel = new BroadcastChannel (' deploystack_oauth' )
446+ oauthBroadcastChannel .onmessage = (event : MessageEvent ) => {
447+ if (! event .data ?.type || ! [' oauth_success' , ' oauth_error' , ' oauth_reauth_success' ].includes (event .data .type )) {
448+ return
449+ }
450+ processOAuthResult (event .data )
451+ }
452+ } catch {
453+ // BroadcastChannel not supported - postMessage is the only fallback
454+ }
426455})
427456
428457onUnmounted (() => {
429458 window .removeEventListener (' message' , handleOAuthMessage )
430459
460+ if (oauthBroadcastChannel ) {
461+ oauthBroadcastChannel .close ()
462+ oauthBroadcastChannel = null
463+ }
464+
431465 if (oauthPopup .value && ! oauthPopup .value .closed ) {
432466 oauthPopup .value .close ()
433467 }
0 commit comments