Skip to content

Commit

Permalink
auth
Browse files Browse the repository at this point in the history
  • Loading branch information
nmsimons committed Sep 19, 2024
1 parent 9422c6b commit d6366d2
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/utils/auth_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ export async function getFunctionToken(account: AccountInfo, noRetry?: boolean):
throw new Error("Account is required for acquiring function token");
}

const response = await axios.post(process.env.TOKEN_PROVIDER_URL + "/.auth/login/aad", {
access_token: account.idToken,
});
const response = await axios
.post(process.env.TOKEN_PROVIDER_URL + "/.auth/login/aad", {
access_token: account.idToken,
})
.catch(async (error) => {
if (error.response && error.response.status === 401) {
// refresh token and retry
await axios.get(process.env.TOKEN_PROVIDER_URL + "/.auth/refresh");
return getFunctionToken(account, true);
} else {
throw new Error("Failed to get function token");
}
});

if (response.status === 401 && !noRetry) {
// refresh token and retry
axios.get(process.env.TOKEN_PROVIDER_URL + "/.auth/refresh");
getFunctionToken(account, true);
}

if (response.status !== 200) {
if (typeof response === "string") {
throw new Error("Failed to get function token");
}

return response.data.authenticationToken;
}

Expand Down

0 comments on commit d6366d2

Please sign in to comment.