@@ -39,6 +39,11 @@ export default class QuidRequests {
3939 return this . _request < T > ( url , "post" ) ;
4040 }
4141
42+ async login ( username : string , password : string ) {
43+ await this . getRefreshToken ( { username : username , password : password } as QuidLoginParams ) ;
44+ await this . checkTokens ( ) ;
45+ }
46+
4247 async getRefreshToken ( { username, password, refreshTokenTtl = "24h" } : QuidLoginParams ) {
4348 const uri = this . quidUri + "/token/refresh/" + refreshTokenTtl ;
4449 const payload = {
@@ -58,6 +63,9 @@ export default class QuidRequests {
5863 throw new Error ( response . statusText )
5964 }
6065 const t = await response . json ( ) ;
66+ if ( this . verbose ) {
67+ console . log ( "Setting refresh token" )
68+ }
6169 this . refreshToken = t . token ;
6270 } catch ( e ) {
6371 throw new Error ( e ) ;
@@ -94,8 +102,7 @@ export default class QuidRequests {
94102 if ( this . verbose ) {
95103 console . log ( method + " request to " + url )
96104 }
97- const status = await this . checkTokens ( ) ;
98- console . log ( "STATUS" , status , this . accessToken ) ;
105+ await this . checkTokens ( ) ;
99106 let opts : RequestInit ;
100107 if ( method === "post" ) {
101108 //console.log("GET", this.#accessToken, uri);
@@ -120,6 +127,18 @@ export default class QuidRequests {
120127 //console.log(JSON.stringify(opts, null, " "))
121128 const response = await fetch ( this . serverUri + url , opts ) ;
122129 if ( ! response . ok ) {
130+ if ( response . status === 401 ) {
131+ this . accessToken = null ;
132+ this . checkTokens ( ) ;
133+ retry ++ ;
134+ if ( retry > 2 ) {
135+ throw new Error ( "Too many retries" )
136+ }
137+ if ( this . verbose ) {
138+ console . log ( "Request retry" , retry )
139+ }
140+ return this . _request < T > ( url , method , payload , retry ) ;
141+ }
123142 console . log ( "RESP NOT OK" , response ) ;
124143 throw new Error ( response . statusText )
125144 }
0 commit comments