@@ -98,8 +98,9 @@ async function exchangeCodeForIdAndAuthToken(code: string): Promise<IdTokenRespo
98
98
// return response.data;
99
99
// }
100
100
101
- async function getElawayToken ( accessToken : string , idToken : string ) : Promise < ElawayTokenResponse | null > {
101
+ async function getElawayToken ( accessToken : string , idToken : string ) : Promise < ElawayTokenResponse > {
102
102
try {
103
+ console . info ( "Requesting Elaway token with access token and ID token." ) ;
103
104
const response = await axios . post ( ampecoApiUrl , {
104
105
token : JSON . stringify ( {
105
106
accessToken : accessToken ,
@@ -119,12 +120,17 @@ async function getElawayToken(accessToken: string, idToken: string): Promise<Ela
119
120
}
120
121
} ) ;
121
122
122
- saveTokens ( response . data ) ;
123
+ if ( response . status !== 200 ) {
124
+ console . error ( "Error during Elaway token request:" , response . data ) ;
125
+ throw new Error ( `Failed to get Elaway token: ${ response . statusText } ` ) ;
126
+ }
123
127
128
+ console . info ( "Successfully obtained Elaway token." ) ;
124
129
return response . data ;
125
130
} catch ( error ) {
126
- console . error ( error . message ) ;
127
- return null ;
131
+ console . error ( "Error in getElawayToken:" , error . message ) ;
132
+ console . error ( "You likely have the wrong ELAWAY_CLIENT_ID or ELAWAY_CLIENT_SECRET" ) ;
133
+ throw error ;
128
134
}
129
135
}
130
136
@@ -153,7 +159,7 @@ function loadTokens(): StoredElawayToken | null {
153
159
return null ;
154
160
}
155
161
156
- async function startOauth ( ) : Promise < ElawayTokenResponse | null > {
162
+ async function startOauth ( ) : Promise < ElawayTokenResponse > {
157
163
let tokenResponse : ElawayTokenResponse | null = null ;
158
164
let accessIdResponse : null | IdTokenResponse = null ;
159
165
const authUrl = `${ elawayAuthorizationUrl } ?response_type=code&client_id=${ encodeURIComponent ( config . clientId ) } &redirect_uri=${ encodeURIComponent ( redirectUri ) } &scope=${ encodeURIComponent ( oauthScope ) } &state=${ encodeURIComponent ( state ) } ` ;
@@ -194,10 +200,13 @@ async function startOauth(): Promise<ElawayTokenResponse | null> {
194
200
} finally {
195
201
await browser . close ( ) ;
196
202
}
197
- return tokenResponse
203
+ if ( ! tokenResponse ) {
204
+ throw new Error ( "Failed to obtain token response" ) ;
205
+ }
206
+ return tokenResponse ;
198
207
}
199
208
200
- async function getValidCredentials ( ) : Promise < StoredElawayToken | null > {
209
+ async function getValidCredentials ( ) : Promise < StoredElawayToken > {
201
210
const storedToken = loadTokens ( ) ;
202
211
203
212
const validBearerToken = storedToken && storedToken . expires_at > Date . now ( ) ;
@@ -215,19 +224,18 @@ async function getValidCredentials(): Promise<StoredElawayToken | null> {
215
224
// }
216
225
// }
217
226
218
- if ( ! validBearerToken ) {
219
- console . info ( "No existing bearer token found. Attempting to get new token." ) ;
220
- const newToken = await startOauth ( ) ;
221
-
222
- if ( ! newToken ) {
223
- throw new Error ( "Could not get valid credentials" ) ;
224
- }
225
- return loadTokens ( ) ;
227
+ if ( validBearerToken ) {
228
+ console . info ( "Existing bearer token found and is valid." ) ;
229
+ return storedToken ;
226
230
}
227
231
228
- console . info ( "Using stored bearer token" ) ;
232
+ console . info ( "No valid bearer token found. Starting OAuth flow." ) ;
233
+ const newToken = await startOauth ( ) ;
229
234
230
- return storedToken ;
235
+ if ( ! newToken ) {
236
+ throw new Error ( "Could not get valid credentials" ) ;
237
+ }
238
+ return saveTokens ( newToken ) ;
231
239
}
232
240
233
241
export { getValidCredentials } ;
0 commit comments