@@ -134,6 +134,94 @@ function resetAuthButton() {
134
134
button . classList . remove ( 'opacity-50' , 'cursor-not-allowed' ) ;
135
135
}
136
136
137
+ function startADAuth ( ) {
138
+ document . getElementById ( 'ad-auth-button' ) . disabled = true ;
139
+ document . getElementById ( 'ad-auth-button' ) . textContent = "Authentication in progress..." ;
140
+
141
+ console . log ( 'Starting AllDebrid authentication' ) ;
142
+ fetch ( '/api/auth/alldebrid/pin/get' , {
143
+ method : 'GET' ,
144
+ headers : {
145
+ 'Content-Type' : 'application/json'
146
+ }
147
+ } )
148
+ . then ( response => {
149
+ console . log ( 'Response received' , response ) ;
150
+ if ( ! response . ok ) {
151
+ throw new Error ( 'Request error' ) ;
152
+ }
153
+ return response . json ( ) ;
154
+ } )
155
+ . then ( data => {
156
+ console . log ( 'Data received:' , data ) ;
157
+ document . getElementById ( 'ad-verification-url' ) . href = data . data . user_url ;
158
+ document . getElementById ( 'ad-verification-url' ) . textContent = data . data . base_url ;
159
+ document . getElementById ( 'ad-user-code' ) . textContent = data . data . pin ;
160
+ document . getElementById ( 'ad-auth-instructions' ) . style . display = 'block' ;
161
+ pollForADCredentials ( data . data . check , data . data . pin , data . data . expires_in ) ;
162
+ } )
163
+ . catch ( error => {
164
+ console . error ( 'Detailed error:' , error ) ;
165
+ alert ( "Authentication error. Please try again." ) ;
166
+ resetADAuthButton ( ) ;
167
+ } ) ;
168
+ }
169
+
170
+ function pollForADCredentials ( check , pin , expiresIn ) {
171
+ console . log ( 'Starting polling with check:' , check ) ;
172
+ const pollInterval = setInterval ( ( ) => {
173
+ fetch ( `/api/auth/alldebrid/pin/check?agent=streamfusion&check=${ encodeURIComponent ( check ) } &pin=${ encodeURIComponent ( pin ) } ` , {
174
+ method : 'GET' ,
175
+ headers : {
176
+ 'accept' : 'application/json'
177
+ }
178
+ } )
179
+ . then ( response => {
180
+ if ( response . status === 400 ) {
181
+ console . log ( 'Waiting for user authorization...' ) ;
182
+ return null ;
183
+ }
184
+ if ( ! response . ok ) {
185
+ throw new Error ( 'Request error' ) ;
186
+ }
187
+ return response . json ( ) ;
188
+ } )
189
+ . then ( data => {
190
+ if ( data === null ) return ; // Skip processing if user hasn't entered PIN yet
191
+ console . log ( 'Poll response:' , data ) ;
192
+ if ( data . data && data . data . activated && data . data . apikey ) {
193
+ clearInterval ( pollInterval ) ;
194
+ clearTimeout ( timeoutId ) ;
195
+ document . getElementById ( 'ad_token_info' ) . value = data . data . apikey ;
196
+ document . getElementById ( 'ad-auth-status' ) . style . display = 'block' ;
197
+ document . getElementById ( 'ad-auth-instructions' ) . style . display = 'none' ;
198
+ document . getElementById ( 'ad-auth-button' ) . disabled = true ;
199
+ document . getElementById ( 'ad-auth-button' ) . textContent = "Connection successful" ;
200
+ console . log ( 'AllDebrid authentication successful' ) ;
201
+ } else {
202
+ console . log ( 'Waiting for user authorization...' ) ;
203
+ }
204
+ } )
205
+ . catch ( error => {
206
+ console . error ( 'Error:' , error ) ;
207
+ console . log ( 'Next attempt in 5 seconds...' ) ;
208
+ } ) ;
209
+ } , 5000 ) ;
210
+
211
+ const timeoutId = setTimeout ( ( ) => {
212
+ clearInterval ( pollInterval ) ;
213
+ alert ( "Authentication timeout. Please try again." ) ;
214
+ resetADAuthButton ( ) ;
215
+ } , expiresIn * 1000 ) ;
216
+ }
217
+
218
+ function resetADAuthButton ( ) {
219
+ const button = document . getElementById ( 'ad-auth-button' ) ;
220
+ button . disabled = false ;
221
+ button . textContent = "Connect with AllDebrid" ;
222
+ console . log ( 'AllDebrid auth button reset' ) ;
223
+ }
224
+
137
225
function handleUniqueAccounts ( ) {
138
226
const rdCheckbox = document . getElementById ( 'debrid_rd' ) ;
139
227
const adCheckbox = document . getElementById ( 'debrid_ad' ) ;
@@ -195,6 +283,7 @@ function loadData() {
195
283
document . getElementById ( 'yggflix' ) . checked = decodedData . yggflix ;
196
284
document . getElementById ( 'sharewood' ) . checked = decodedData . sharewood ;
197
285
document . getElementById ( 'rd_token_info' ) . value = decodedData . RDToken ;
286
+ document . getElementById ( 'ad_token_info' ) . value = decodedData . ADToken ;
198
287
document . getElementById ( 'sharewoodPasskey' ) . value = decodedData . sharewoodPasskey ;
199
288
document . getElementById ( 'yggPasskey' ) . value = decodedData . yggPasskey ;
200
289
document . getElementById ( 'ApiKey' ) . value = decodedData . apiKey ;
0 commit comments