Skip to content

Commit

Permalink
Merge pull request #27 from bento-platform/fix/fetch-resource-permiss…
Browse files Browse the repository at this point in the history
…ions-header

fix: bad requests with `undefined` bearer token
  • Loading branch information
davidlougheed authored Apr 18, 2024
2 parents 04b657a + 8081adb commit d2a12e8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/redux/authSlice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { JWTPayload, decodeJwt } from "jose";

import { buildUrlEncodedData } from "../utils";
import { buildUrlEncodedData, makeAuthorizationHeader } from "../utils";
import { LS_BENTO_WAS_SIGNED_IN, setLSNotSignedIn } from "../performAuth";
import { Resource, makeResourceKey } from "../resources";
import { RootState } from "./store";
Expand Down Expand Up @@ -116,7 +116,7 @@ export const fetchResourcePermissions = createAsyncThunk<FetchPermissionPayload,
const { auth } = getState() as RootState;
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: `Bearer ${auth.accessToken}` },
headers: { "Content-Type": "application/json", ...makeAuthorizationHeader(auth.accessToken) },
body: JSON.stringify({resources: [resource]}),
});
return await response.json();
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const buildUrlEncodedData = (obj: object) =>
export const getIsAuthenticated = (idTokenContents: JWTPayload | null | undefined) =>
!!idTokenContents && idTokenContents.exp && Math.round(new Date().getTime() / 1000) < idTokenContents.exp;

export const makeAuthorizationHeader = (token: string | null | undefined) =>
export const makeAuthorizationHeader = (token: string | null | undefined): HeadersInit | Record<string, never> =>
(token ? { Authorization: `Bearer ${token}` } : {});

export const recursiveOrderedObject = (x: Resource): unknown => {
Expand Down

0 comments on commit d2a12e8

Please sign in to comment.