@@ -61,6 +61,11 @@ export interface PocketbookCloudNote {
6161 uuid : string ; //TODO: uuid type?
6262}
6363
64+ interface PocketbookCloudShopInfo {
65+ name : string ;
66+ shop_id : string ;
67+ }
68+
6469/**
6570 * Main things to know about the API:
6671 *
@@ -144,7 +149,7 @@ export class PocketbookCloudLoginClient {
144149 ) { }
145150
146151 async login ( ) {
147- const shop_id = await fetch (
152+ const shops : PocketbookCloudShopInfo [ ] = await fetch (
148153 'https://cloud.pocketbook.digital/api/v1.0/auth/login?' +
149154 new URLSearchParams ( {
150155 username : this . username ,
@@ -156,51 +161,64 @@ export class PocketbookCloudLoginClient {
156161 . then ( data => {
157162 return data ;
158163 } )
159- . then ( data => data . providers . filter ( ( item : any ) => item . name . includes ( this . shop_name ) ) )
160- . then ( data => data [ 0 ] . shop_id ) ;
161-
162- console . log ( `shop_id: ${ shop_id } ` ) ;
163-
164- let login_response ;
165- if ( this . refresh_token ) {
166- login_response = await requestUrl ( {
167- url : 'https://cloud.pocketbook.digital/api/v1.0/auth/renew-token' ,
168- method : 'POST' ,
169- contentType : 'application/x-www-form-urlencoded' ,
170- headers : {
171- Authorization : `Bearer ${ this . access_token } ` ,
172- } ,
173- body : new URLSearchParams ( {
174- grant_type : 'refresh_token' ,
175- refresh_token : this . refresh_token ,
176- } ) . toString ( ) ,
177- } ) . then ( response => response . json ) ;
178- } else if ( this . password ) {
179- login_response = await requestUrl ( {
180- url : 'https://cloud.pocketbook.digital/api/v1.0/auth/login/knv' ,
181- method : 'POST' ,
182- contentType : 'application/x-www-form-urlencoded' ,
183- body : new URLSearchParams ( {
184- shop_id,
185- username : this . username ,
186- password : this . password ,
187- client_id : this . client_id ,
188- client_secret : this . client_secret ,
189- } ) . toString ( ) ,
190- } ) . then ( response => response . json ) ;
191- } else {
192- throw new Error ( 'No password or refresh token provided, one is necessary to login.' ) ;
164+ . then ( data => data . providers . filter ( ( item : PocketbookCloudShopInfo ) => item . name . includes ( this . shop_name ) ) ) ;
165+
166+ const login_responses = await Promise . all (
167+ shops . map ( async shop => {
168+ let result ;
169+ try {
170+ if ( this . refresh_token ) {
171+ result = await requestUrl ( {
172+ url : 'https://cloud.pocketbook.digital/api/v1.0/auth/renew-token' ,
173+ method : 'POST' ,
174+ contentType : 'application/x-www-form-urlencoded' ,
175+ headers : {
176+ Authorization : `Bearer ${ this . access_token } ` ,
177+ } ,
178+ body : new URLSearchParams ( {
179+ grant_type : 'refresh_token' ,
180+ refresh_token : this . refresh_token ,
181+ } ) . toString ( ) ,
182+ } ) . then ( response => response . json ) ;
183+ } else if ( this . password ) {
184+ result = await requestUrl ( {
185+ url : 'https://cloud.pocketbook.digital/api/v1.0/auth/login/knv' ,
186+ method : 'POST' ,
187+ contentType : 'application/x-www-form-urlencoded' ,
188+ body : new URLSearchParams ( {
189+ shop_id : shop . shop_id ,
190+ username : this . username ,
191+ password : this . password ,
192+ client_id : this . client_id ,
193+ client_secret : this . client_secret ,
194+ grant_type : 'password' ,
195+ language : 'en' ,
196+ } ) . toString ( ) ,
197+ } ) . then ( response => response . json ) ;
198+ }
199+ } catch ( error ) {
200+ result = null ;
201+ }
202+ return { shop, result } ;
203+ } )
204+ ) ;
205+
206+ // use first defined response, if any
207+ const login_response = login_responses . filter ( response => response . result ) . first ( ) ;
208+ if ( ! login_response ) {
209+ throw new Error ( 'Could not log in to Pocketbook Cloud' ) ;
193210 }
194211
195- this . access_token = login_response . access_token ;
196- this . refresh_token = login_response . refresh_token ;
212+ this . access_token = login_response . result . access_token ;
213+ this . refresh_token = login_response . result . refresh_token ;
197214
198215 // sets the access token to expire 5 minutes before it actually does
199- this . access_token_valid_until = new Date ( Date . now ( ) + login_response . expires_in * 1000 - 5 * 60 * 1000 ) ;
216+ this . access_token_valid_until = new Date ( Date . now ( ) + login_response . result . expires_in * 1000 - 5 * 60 * 1000 ) ;
200217
201218 this . plugin . settings . access_token = this . access_token ! ! ;
202219 this . plugin . settings . refresh_token = this . refresh_token ! ! ;
203220 this . plugin . settings . access_token_valid_until = this . access_token_valid_until ;
221+ this . plugin . settings . shop_name = login_response . shop . name ;
204222 await this . plugin . saveSettings ( ) ;
205223 }
206224
0 commit comments