Skip to content

Commit a5bc769

Browse files
committedDec 23, 2024
Better error handling
1 parent a6f51fd commit a5bc769

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed
 

‎src/auth.ts

+25-17
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ async function exchangeCodeForIdAndAuthToken(code: string): Promise<IdTokenRespo
9898
// return response.data;
9999
// }
100100

101-
async function getElawayToken(accessToken: string, idToken: string): Promise<ElawayTokenResponse | null> {
101+
async function getElawayToken(accessToken: string, idToken: string): Promise<ElawayTokenResponse> {
102102
try {
103+
console.info("Requesting Elaway token with access token and ID token.");
103104
const response = await axios.post(ampecoApiUrl, {
104105
token: JSON.stringify({
105106
accessToken: accessToken,
@@ -119,12 +120,17 @@ async function getElawayToken(accessToken: string, idToken: string): Promise<Ela
119120
}
120121
});
121122

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+
}
123127

128+
console.info("Successfully obtained Elaway token.");
124129
return response.data;
125130
} 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;
128134
}
129135
}
130136

@@ -153,7 +159,7 @@ function loadTokens(): StoredElawayToken | null {
153159
return null;
154160
}
155161

156-
async function startOauth(): Promise<ElawayTokenResponse | null> {
162+
async function startOauth(): Promise<ElawayTokenResponse> {
157163
let tokenResponse: ElawayTokenResponse | null = null;
158164
let accessIdResponse: null | IdTokenResponse = null;
159165
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> {
194200
} finally {
195201
await browser.close();
196202
}
197-
return tokenResponse
203+
if (!tokenResponse) {
204+
throw new Error("Failed to obtain token response");
205+
}
206+
return tokenResponse;
198207
}
199208

200-
async function getValidCredentials(): Promise<StoredElawayToken | null> {
209+
async function getValidCredentials(): Promise<StoredElawayToken> {
201210
const storedToken = loadTokens();
202211

203212
const validBearerToken = storedToken && storedToken.expires_at > Date.now();
@@ -215,19 +224,18 @@ async function getValidCredentials(): Promise<StoredElawayToken | null> {
215224
// }
216225
// }
217226

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;
226230
}
227231

228-
console.info("Using stored bearer token");
232+
console.info("No valid bearer token found. Starting OAuth flow.");
233+
const newToken = await startOauth();
229234

230-
return storedToken;
235+
if (!newToken) {
236+
throw new Error("Could not get valid credentials");
237+
}
238+
return saveTokens(newToken);
231239
}
232240

233241
export { getValidCredentials };

0 commit comments

Comments
 (0)
Please sign in to comment.