@@ -270,7 +270,8 @@ <h4>Advanced Controls</h4>
270270 var YT_ADMIN_SCOPES = YT_READONLY_SCOPES . concat ( [
271271 'https://www.googleapis.com/auth/youtube.force-ssl'
272272 ] ) ;
273- var AUTH_BASE_URL = 'https://ytauth.socialstream.ninja' ;
273+ var AUTH_BASE_URL = 'https://sso.socialstream.ninja/youtube' ;
274+ var LEGACY_AUTH_BASE_URL = 'https://ytauth.socialstream.ninja' ;
274275 var DEFAULT_YT_CLIENT_ID = '689627108309-isbjas8fmbc7sucmbm7gkqjapk7btbsi.apps.googleusercontent.com' ;
275276 var YT_ACCESS_LEVEL_KEY = 'youtubeOAuthAccessLevel' ;
276277 var YT_AUTH_SOURCE_KEY = 'youtubeOAuthSource' ;
@@ -4318,6 +4319,37 @@ <h4>Advanced Controls</h4>
43184319 ? bridge
43194320 : null ;
43204321 }
4322+ function shouldFallbackToLegacyYouTubeAuth ( error ) {
4323+ const status = Number ( error && error . status ) ;
4324+ return ! status || status === 405 || status >= 500 ;
4325+ }
4326+ async function postHostedYouTubeAuthJson ( path , payload ) {
4327+ async function postToBase ( baseUrl ) {
4328+ const response = await fetch ( baseUrl + path , {
4329+ method : 'POST' ,
4330+ headers : { 'Content-Type' : 'application/json' } ,
4331+ body : JSON . stringify ( payload )
4332+ } ) ;
4333+ const data = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
4334+ if ( ! response . ok ) {
4335+ const msg = data ?. error_description || data ?. error || `HTTP error! status: ${ response . status } ` ;
4336+ const error = new Error ( msg ) ;
4337+ error . status = response . status ;
4338+ error . payload = data ;
4339+ throw error ;
4340+ }
4341+ return data ;
4342+ }
4343+ try {
4344+ return await postToBase ( AUTH_BASE_URL ) ;
4345+ } catch ( error ) {
4346+ if ( AUTH_BASE_URL !== LEGACY_AUTH_BASE_URL && shouldFallbackToLegacyYouTubeAuth ( error ) ) {
4347+ console . warn ( 'YouTube SSO bridge failed; falling back to legacy ytauth bridge.' , error ) ;
4348+ return postToBase ( LEGACY_AUTH_BASE_URL ) ;
4349+ }
4350+ throw error ;
4351+ }
4352+ }
43214353 async function refreshAccessToken ( refreshTokenValue ) {
43224354 if ( ! refreshTokenValue ) {
43234355 console . log ( 'No refresh token available for refreshAccessToken.' ) ;
@@ -4344,16 +4376,10 @@ <h4>Advanced Controls</h4>
43444376 clientSecret : credentials . clientSecret
43454377 } ) ;
43464378 } else {
4347- const response = await fetch ( AUTH_BASE_URL + '/refresh' , {
4348- method : 'POST' ,
4349- headers : { 'Content-Type' : 'application/json' } ,
4350- body : JSON . stringify ( { refresh_token : refreshTokenValue , client_id : DEFAULT_YT_CLIENT_ID } )
4379+ data = await postHostedYouTubeAuthJson ( '/refresh' , {
4380+ refresh_token : refreshTokenValue ,
4381+ client_id : DEFAULT_YT_CLIENT_ID
43514382 } ) ;
4352- data = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
4353- if ( ! response . ok ) {
4354- const msg = data ?. error_description || data ?. error || `HTTP error! status: ${ response . status } ` ;
4355- throw new Error ( msg ) ;
4356- }
43574383 }
43584384 if ( ! data || ! data . access_token ) {
43594385 throw new Error ( 'Token refresh response did not include access_token.' ) ;
@@ -4401,16 +4427,11 @@ <h4>Advanced Controls</h4>
44014427 clientSecret : credentials . clientSecret
44024428 } ) ;
44034429 } else {
4404- const response = await fetch ( AUTH_BASE_URL + '/token' , {
4405- method : 'POST' ,
4406- headers : { 'Content-Type' : 'application/json' } ,
4407- body : JSON . stringify ( { code : code , redirect_uri : redirectTarget , client_id : DEFAULT_YT_CLIENT_ID } )
4430+ data = await postHostedYouTubeAuthJson ( '/token' , {
4431+ code : code ,
4432+ redirect_uri : redirectTarget ,
4433+ client_id : DEFAULT_YT_CLIENT_ID
44084434 } ) ;
4409- data = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
4410- if ( ! response . ok ) {
4411- const msg = data ?. error_description || data ?. error || `HTTP error! status: ${ response . status } ` ;
4412- throw new Error ( msg ) ;
4413- }
44144435 }
44154436 if ( ! data . access_token ) {
44164437 throw new Error ( 'Token exchange response did not include access_token.' ) ;
0 commit comments