Skip to content

Commit

Permalink
simplify things a bit more...
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed Dec 16, 2024
1 parent 7161ede commit 8c3eb5f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
20 changes: 13 additions & 7 deletions src/Utils/request/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,30 @@ const ensurePathNotMissingReplacements = (path: string) => {
}
};

export function getJWTAccessToken() {
return localStorage.getItem(LocalStorageKeys.accessToken);
}

export function makeHeaders(noAuth: boolean, additionalHeaders?: HeadersInit) {
const headers = new Headers(additionalHeaders);

headers.set("Content-Type", "application/json");
headers.append("Accept", "application/json");

const jwtAccessToken = getJWTAccessToken();
if (jwtAccessToken && !noAuth) {
headers.append("Authorization", `Bearer ${jwtAccessToken}`);
const authorizationHeader = getAuthorizationHeader();
if (authorizationHeader && !noAuth) {
headers.append("Authorization", authorizationHeader);
}

return headers;
}

export function getAuthorizationHeader() {
const accessToken = localStorage.getItem(LocalStorageKeys.accessToken);

if (accessToken) {
return `Bearer ${accessToken}`;
}

return null;
}

export function mergeRequestOptions<TData>(
options: RequestOptions<TData>,
overrides: RequestOptions<TData>,
Expand Down
4 changes: 2 additions & 2 deletions src/components/Facility/FacilityHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import uploadFile from "@/Utils/request/uploadFile";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { getJWTAccessToken } from "@/Utils/request/utils";
import { getAuthorizationHeader } from "@/Utils/request/utils";
import { sleep } from "@/Utils/utils";

import { patientRegisterAuth } from "../Patient/PatientRegister";
Expand Down Expand Up @@ -118,7 +118,7 @@ export const FacilityHome = ({ facilityId }: Props) => {
url,
formData,
"POST",
{ Authorization: `Bearer ${getJWTAccessToken()}` },
{ Authorization: getAuthorizationHeader() },
async (xhr: XMLHttpRequest) => {
if (xhr.status === 200) {
await sleep(1000);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Users/UserAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import uploadFile from "@/Utils/request/uploadFile";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { getJWTAccessToken } from "@/Utils/request/utils";
import { getAuthorizationHeader } from "@/Utils/request/utils";
import { formatDisplayName, sleep } from "@/Utils/utils";

export default function UserAvatar({ username }: { username: string }) {
Expand Down Expand Up @@ -46,7 +46,7 @@ export default function UserAvatar({ username }: { username: string }) {
url,
formData,
"POST",
{ Authorization: `Bearer ${getJWTAccessToken()}` },
{ Authorization: getAuthorizationHeader() },
async (xhr: XMLHttpRequest) => {
if (xhr.status === 200) {
await sleep(1000);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Users/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import uploadFile from "@/Utils/request/uploadFile";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import { getJWTAccessToken } from "@/Utils/request/utils";
import { getAuthorizationHeader } from "@/Utils/request/utils";
import {
dateQueryString,
formatDate,
Expand Down Expand Up @@ -508,7 +508,7 @@ export default function UserProfile() {
url,
formData,
"POST",
{ Authorization: `Bearer ${getJWTAccessToken()}` },
{ Authorization: getAuthorizationHeader() },
async (xhr: XMLHttpRequest) => {
if (xhr.status === 200) {
await sleep(1000);
Expand Down

0 comments on commit 8c3eb5f

Please sign in to comment.