@@ -123,18 +123,10 @@ class Twitch extends Service {
123123 }
124124 }
125125
126- @onLoad ( [ 'broadcasterRefreshToken' , 'botRefreshToken' , 'tokenService' , 'tokenServiceCustomClientId' , 'tokenServiceCustomClientSecret' ] )
127- @onChange ( [ 'broadcasterRefreshToken' , 'botRefreshToken' , 'tokenService' , 'tokenServiceCustomClientId' , 'tokenServiceCustomClientSecret' ] )
128- async onChangeRefreshTokens ( key : string ) {
129- this . botTokenValid = false ;
130- this . broadcasterTokenValid = false ;
131-
132- loadedKeys . add ( key ) ;
133- if ( loadedKeys . size < 5 ) {
134- return ;
135- }
126+ @onLoad ( [ 'tokenService' , 'tokenServiceCustomClientId' , 'tokenServiceCustomClientSecret' ] )
127+ @onChange ( [ 'tokenService' , 'tokenServiceCustomClientId' , 'tokenServiceCustomClientSecret' ] )
128+ onTokenServiceChange ( ) {
136129 let clientId ;
137-
138130 switch ( this . tokenService ) {
139131 case 'SogeBot Token Generator' :
140132 clientId = 't8cney2xkc7j4cu6zpv9ijfa27w027' ;
@@ -150,6 +142,19 @@ class Twitch extends Service {
150142 clientSecret : this . tokenServiceCustomClientSecret , // we don't care if we have generator
151143 } ) ;
152144 this . apiClient = new ApiClient ( { authProvider : this . authProvider } ) ;
145+ }
146+
147+ @onLoad ( [ 'broadcasterRefreshToken' , 'botRefreshToken' , 'tokenService' , 'tokenServiceCustomClientId' , 'tokenServiceCustomClientSecret' ] )
148+ async onChangeRefreshTokens ( key : string ) {
149+ this . botTokenValid = false ;
150+ this . broadcasterTokenValid = false ;
151+
152+ loadedKeys . add ( key ) ;
153+ if ( loadedKeys . size < 5 || ! this . authProvider || ! this . apiClient ) {
154+ console . debug ( 'eventsub' , 'Not yet loaded' ) ;
155+ return ;
156+ }
157+ console . debug ( 'eventsub' , 'Adding tokens to authProvider' ) ;
153158 if ( this . botRefreshToken . length > 0 ) {
154159 const userId = await this . authProvider . addUserForToken ( {
155160 expiresIn : 0 ,
@@ -332,7 +337,7 @@ class Twitch extends Service {
332337 adminEndpoint ( '/services/twitch' , 'twitch::token' , async ( { accessToken, refreshToken, accountType } , cb ) => {
333338 emitter . emit ( 'set' , '/services/twitch' , `tokenService` , 'SogeBot Token Generator v2' ) ;
334339 emitter . emit ( 'set' , '/services/twitch' , `${ accountType } RefreshToken` , refreshToken ) ;
335- emitter . emit ( 'set' , '/services/twitch' , `${ accountType } TokenValid` , true ) ;
340+ this . onChangeRefreshTokens ( `${ accountType } RefreshToken` ) ;
336341 setTimeout ( async ( ) => {
337342 cb ( null ) ;
338343 } , 1000 ) ;
@@ -342,7 +347,7 @@ class Twitch extends Service {
342347 emitter . emit ( 'set' , '/services/twitch' , `${ accountType } RefreshToken` , refreshToken ) ;
343348 emitter . emit ( 'set' , '/services/twitch' , `tokenServiceCustomClientId` , clientId ) ;
344349 emitter . emit ( 'set' , '/services/twitch' , `tokenServiceCustomClientSecret` , clientSecret ) ;
345- emitter . emit ( 'set' , '/services/twitch' , `${ accountType } TokenValid` , true ) ;
350+ this . onChangeRefreshTokens ( `${ accountType } RefreshToken` ) ;
346351 setTimeout ( async ( ) => {
347352 cb ( null ) ;
348353 } , 1000 ) ;
0 commit comments