Skip to content

Commit

Permalink
Merge pull request #2292 from PolicyEngine/2263_add_aud_to_bearer_token
Browse files Browse the repository at this point in the history
Auth now specifies api as audience.
  • Loading branch information
MaxGhenis authored Jan 8, 2025
2 parents 0754e47 + 1981c90 commit 4bfece9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/__tests__/hooks/useAuthenticatedFetch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("useAuthenticatedFetch", () => {
},
});
}

test("given the user is logged in then it adds the bearer token", async () => {
givenTheUserIsLoggedIn("TEST_AUTH_TOKEN");
const requestOptions = {
Expand All @@ -60,6 +61,7 @@ describe("useAuthenticatedFetch", () => {
},
]);
});

test("given the user is not logged in then it adds nothing", async () => {
const { result } = renderHook(() => useAuthenticatedFetch());

Expand Down
1 change: 1 addition & 0 deletions src/auth/Auth0ProviderWithNavigate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default function Auth0ProviderWithNavigate({ children }) {
clientId={clientId}
authorizationParams={{
redirect_uri: redirectUri,
audience: "https://api.policyengine.org/",
}}
onRedirectCallback={onRedirectCallback}
useRefreshTokens={true}
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useAuthenticatedFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export function useAuthenticatedFetch() {
if (isAuthenticated) {
try {
//as per https://auth0.com/docs/quickstart/spa/react/02-calling-an-api
const accessToken = await getAccessTokenSilently();
const accessToken = await getAccessTokenSilently({
audience: "https://api.policyengine.org/",
});
headers["Authorization"] = `Bearer ${accessToken}`;
} catch (error) {
//IGNORE. If we can't get an access token we just call the API
Expand Down

0 comments on commit 4bfece9

Please sign in to comment.